Catch up with statistics after DB migration is done (#78469)

* Catch up with statistics after DB migration is done

* Don't access the database from the event loop

* Fix deadlocking test

* Fix test
This commit is contained in:
Erik Montnemery 2022-09-15 07:40:56 +02:00 committed by GitHub
parent bcf01e8873
commit 0a1fd36e03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 1 deletions

View File

@ -615,6 +615,10 @@ class Recorder(threading.Thread):
self.hass.add_job(self.async_set_db_ready)
# Catch up with missed statistics
with session_scope(session=self.get_session()) as session:
self._schedule_compile_missing_statistics(session)
_LOGGER.debug("Recorder processing the queue")
self.hass.add_job(self._async_set_recorder_ready_migration_done)
self._run_event_loop()
@ -1118,7 +1122,6 @@ class Recorder(threading.Thread):
with session_scope(session=self.get_session()) as session:
end_incomplete_runs(session, self.run_history.recording_start)
self.run_history.start(session)
self._schedule_compile_missing_statistics(session)
self._open_event_session()

View File

@ -1272,6 +1272,7 @@ async def test_database_corruption_while_running(hass, tmpdir, caplog):
sqlite3_exception = DatabaseError("statement", {}, [])
sqlite3_exception.__cause__ = sqlite3.DatabaseError()
await async_wait_recording_done(hass)
with patch.object(
get_instance(hass).event_session,
"close",

View File

@ -336,6 +336,8 @@ async def test_schema_migrate(hass, start_version, live):
), patch(
"homeassistant.components.recorder.migration._apply_update",
wraps=_instrument_apply_update,
), patch(
"homeassistant.components.recorder.Recorder._schedule_compile_missing_statistics",
):
recorder_helper.async_initialize_recorder(hass)
hass.async_create_task(