Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions .strict-typing
Original file line number Diff line number Diff line change
Expand Up @@ -177,23 +177,7 @@ homeassistant.components.pure_energie.*
homeassistant.components.rainmachine.*
homeassistant.components.rdw.*
homeassistant.components.recollect_waste.*
homeassistant.components.recorder
homeassistant.components.recorder.const
homeassistant.components.recorder.core
homeassistant.components.recorder.backup
homeassistant.components.recorder.executor
homeassistant.components.recorder.history
homeassistant.components.recorder.models
homeassistant.components.recorder.pool
homeassistant.components.recorder.purge
homeassistant.components.recorder.repack
homeassistant.components.recorder.run_history
homeassistant.components.recorder.services
homeassistant.components.recorder.statistics
homeassistant.components.recorder.system_health
homeassistant.components.recorder.tasks
homeassistant.components.recorder.util
homeassistant.components.recorder.websocket_api
homeassistant.components.recorder.*
homeassistant.components.remote.*
homeassistant.components.renault.*
homeassistant.components.ridwell.*
Expand Down
22 changes: 14 additions & 8 deletions homeassistant/components/recorder/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def __init__(
self._pending_event_data: dict[str, EventData] = {}
self._pending_expunge: list[States] = []
self.event_session: Session | None = None
self.get_session: Callable[[], Session] | None = None
self._get_session: Callable[[], Session] | None = None
self._completed_first_database_setup: bool | None = None
self.async_migration_event = asyncio.Event()
self.migration_in_progress = False
Expand Down Expand Up @@ -203,6 +203,12 @@ def recording(self) -> bool:
"""Return if the recorder is recording."""
return self._event_listener is not None

def get_session(self) -> Session:
"""Get a new sqlalchemy session."""
if self._get_session is None:
raise RuntimeError("The database connection has not been established")
return self._get_session()

def queue_task(self, task: RecorderTask) -> None:
"""Add a task to the recorder queue."""
self._queue.put(task)
Expand Down Expand Up @@ -457,7 +463,7 @@ def using_sqlite(self) -> bool:
@callback
def _async_setup_periodic_tasks(self) -> None:
"""Prepare periodic tasks."""
if self.hass.is_stopping or not self.get_session:
if self.hass.is_stopping or not self._get_session:
# Home Assistant is shutting down
return

Expand Down Expand Up @@ -586,7 +592,7 @@ def _setup_recorder(self) -> None | int:
while tries <= self.db_max_retries:
try:
self._setup_connection()
return migration.get_schema_version(self)
return migration.get_schema_version(self.get_session)
except Exception as err: # pylint: disable=broad-except
_LOGGER.exception(
"Error during connection setup: %s (retrying in %s seconds)",
Expand Down Expand Up @@ -614,7 +620,9 @@ def _migrate_schema_and_setup_run(self, current_version: int) -> bool:
self.hass.add_job(self._async_migration_started)

try:
migration.migrate_schema(self, current_version)
migration.migrate_schema(
self.hass, self.engine, self.get_session, current_version
)
except exc.DatabaseError as err:
if self._handle_database_error(err):
return True
Expand Down Expand Up @@ -891,7 +899,6 @@ def _reopen_event_session(self) -> None:

def _open_event_session(self) -> None:
"""Open the event session."""
assert self.get_session is not None
self.event_session = self.get_session()
self.event_session.expire_on_commit = False

Expand Down Expand Up @@ -1006,19 +1013,18 @@ def setup_recorder_connection(
sqlalchemy_event.listen(self.engine, "connect", setup_recorder_connection)

Base.metadata.create_all(self.engine)
self.get_session = scoped_session(sessionmaker(bind=self.engine, future=True))
self._get_session = scoped_session(sessionmaker(bind=self.engine, future=True))
_LOGGER.debug("Connected to recorder database")

def _close_connection(self) -> None:
"""Close the connection."""
assert self.engine is not None
self.engine.dispose()
self.engine = None
self.get_session = None
self._get_session = None

def _setup_run(self) -> None:
"""Log the start of the current run and schedule any needed jobs."""
assert self.get_session is not None
with session_scope(session=self.get_session()) as session:
end_incomplete_runs(session, self.run_history.recording_start)
self.run_history.start(session)
Expand Down
Loading