Chunk purging attributes and data ids for old SQLite versions (#104296)

This commit is contained in:
J. Nick Koston
2023-11-24 11:46:02 +01:00
committed by GitHub
parent b41b56e54c
commit 9ed745638d
3 changed files with 75 additions and 24 deletions

View File

@@ -1,7 +1,7 @@
"""SQLAlchemy util functions."""
from __future__ import annotations
from collections.abc import Callable, Generator, Iterable, Sequence
from collections.abc import Callable, Collection, Generator, Iterable, Sequence
from contextlib import contextmanager
from datetime import date, datetime, timedelta
import functools
@@ -857,6 +857,20 @@ def chunked(iterable: Iterable, chunked_num: int) -> Iterable[Any]:
return iter(partial(take, chunked_num, iter(iterable)), [])
def chunked_or_all(iterable: Collection[Any], chunked_num: int) -> Iterable[Any]:
"""Break *collection* into iterables of length *n*.
Returns the collection if its length is less than *n*.
Unlike chunked, this function requires a collection so it can
determine the length of the collection and return the collection
if it is less than *n*.
"""
if len(iterable) <= chunked_num:
return (iterable,)
return chunked(iterable, chunked_num)
def get_index_by_name(session: Session, table_name: str, index_name: str) -> str | None:
"""Get an index by name."""
connection = session.connection()