From 68e170284fc64797742275126e68504d2512f540 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 22 Mar 2024 07:29:46 -1000 Subject: [PATCH] Speed up recorder startup by making schema query read only (#113987) --- homeassistant/components/recorder/migration.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/recorder/migration.py b/homeassistant/components/recorder/migration.py index 8395b88837c..6021c7b5f5b 100644 --- a/homeassistant/components/recorder/migration.py +++ b/homeassistant/components/recorder/migration.py @@ -181,7 +181,7 @@ def _get_schema_version(session: Session) -> int | None: def get_schema_version(session_maker: Callable[[], Session]) -> int | None: """Get the schema version.""" try: - with session_scope(session=session_maker()) as session: + with session_scope(session=session_maker(), read_only=True) as session: return _get_schema_version(session) except Exception as err: # pylint: disable=broad-except _LOGGER.exception("Error when determining DB schema version: %s", err) @@ -1771,9 +1771,11 @@ def _initialize_database(session: Session) -> bool: def initialize_database(session_maker: Callable[[], Session]) -> bool: """Initialize a new database.""" try: - with session_scope(session=session_maker()) as session: + with session_scope(session=session_maker(), read_only=True) as session: if _get_schema_version(session) is not None: return True + + with session_scope(session=session_maker()) as session: return _initialize_database(session) except Exception as err: # pylint: disable=broad-except