Upgrade SQLAlchemy to 2.0.2 (#86436)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Erik Montnemery 2023-02-08 15:17:32 +01:00 committed by GitHub
parent 93dafefd96
commit 94519de8dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
37 changed files with 583 additions and 430 deletions

View file

@ -1,7 +1,7 @@
"""SQLAlchemy util functions."""
from __future__ import annotations
from collections.abc import Callable, Generator
from collections.abc import Callable, Generator, Sequence
from contextlib import contextmanager
from datetime import date, datetime, timedelta
import functools
@ -17,8 +17,7 @@ from awesomeversion import (
)
import ciso8601
from sqlalchemy import text
from sqlalchemy.engine.cursor import CursorFetchStrategy
from sqlalchemy.engine.row import Row
from sqlalchemy.engine import Result, Row
from sqlalchemy.exc import OperationalError, SQLAlchemyError
from sqlalchemy.orm.query import Query
from sqlalchemy.orm.session import Session
@ -45,6 +44,8 @@ from .models import (
)
if TYPE_CHECKING:
from sqlite3.dbapi2 import Cursor as SQLiteCursor
from . import Recorder
_RecorderT = TypeVar("_RecorderT", bound="Recorder")
@ -202,8 +203,8 @@ def execute_stmt_lambda_element(
stmt: StatementLambdaElement,
start_time: datetime | None = None,
end_time: datetime | None = None,
yield_per: int | None = DEFAULT_YIELD_STATES_ROWS,
) -> list[Row]:
yield_per: int = DEFAULT_YIELD_STATES_ROWS,
) -> Sequence[Row] | Result:
"""Execute a StatementLambdaElement.
If the time window passed is greater than one day
@ -220,8 +221,8 @@ def execute_stmt_lambda_element(
for tryno in range(RETRIES):
try:
if use_all:
return executed.all() # type: ignore[no-any-return]
return executed.yield_per(yield_per) # type: ignore[no-any-return]
return executed.all()
return executed.yield_per(yield_per)
except SQLAlchemyError as err:
_LOGGER.error("Error executing query: %s", err)
if tryno == RETRIES - 1:
@ -252,7 +253,7 @@ def dburl_to_path(dburl: str) -> str:
return dburl.removeprefix(SQLITE_URL_PREFIX)
def last_run_was_recently_clean(cursor: CursorFetchStrategy) -> bool:
def last_run_was_recently_clean(cursor: SQLiteCursor) -> bool:
"""Verify the last recorder run was recently clean."""
cursor.execute("SELECT end FROM recorder_runs ORDER BY start DESC LIMIT 1;")
@ -273,7 +274,7 @@ def last_run_was_recently_clean(cursor: CursorFetchStrategy) -> bool:
return True
def basic_sanity_check(cursor: CursorFetchStrategy) -> bool:
def basic_sanity_check(cursor: SQLiteCursor) -> bool:
"""Check tables to make sure select does not fail."""
for table in TABLES_TO_CHECK:
@ -300,7 +301,7 @@ def validate_sqlite_database(dbpath: str) -> bool:
return True
def run_checks_on_open_db(dbpath: str, cursor: CursorFetchStrategy) -> None:
def run_checks_on_open_db(dbpath: str, cursor: SQLiteCursor) -> None:
"""Run checks that will generate a sqlite3 exception if there is corruption."""
sanity_check_passed = basic_sanity_check(cursor)
last_run_was_clean = last_run_was_recently_clean(cursor)