Skip to content

Commit 01ff3cf

Browse files
authored
Start recorder data migration after schema migration (home-assistant#153471)
1 parent d66da0c commit 01ff3cf

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

homeassistant/components/recorder/core.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,10 @@ def _run(self) -> None:
806806

807807
# Catch up with missed statistics
808808
self._schedule_compile_missing_statistics()
809+
810+
# Kick off live migrations
811+
migration.migrate_data_live(self, self.get_session, schema_status)
812+
809813
_LOGGER.debug("Recorder processing the queue")
810814
self._adjust_lru_size()
811815
self.hass.add_job(self._async_set_recorder_ready_migration_done)
@@ -822,8 +826,6 @@ def _activate_and_set_db_ready(
822826
# there are a lot of statistics graphs on the frontend.
823827
self.statistics_meta_manager.load(session)
824828

825-
migration.migrate_data_live(self, self.get_session, schema_status)
826-
827829
# We must only set the db ready after we have set the table managers
828830
# to active if there is no data to migrate.
829831
#

tests/components/recorder/test_v32_migration.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -845,11 +845,28 @@ def _add_data():
845845
instrumented_migration.live_migration_done.wait
846846
)
847847

848+
# The states.event_id foreign key constraint was removed when
849+
# migration to schema version 46
850+
assert (
851+
await instance.async_add_executor_job(_get_event_id_foreign_keys)
852+
is None
853+
)
854+
855+
# Re-add the foreign key constraint to simulate failure to remove it during
856+
# schema migration
857+
with patch.object(migration, "Base", old_db_schema.Base):
858+
await instance.async_add_executor_job(
859+
migration._restore_foreign_key_constraints,
860+
instance.get_session,
861+
instance.engine,
862+
[("states", "event_id", "events", "event_id")],
863+
)
864+
848865
# Simulate out of disk space while removing the foreign key from the states table by
849866
# - patching DropConstraint to raise InternalError for MySQL and PostgreSQL
850867
with (
851868
patch(
852-
"homeassistant.components.recorder.migration.sqlalchemy.inspect",
869+
"homeassistant.components.recorder.migration.DropConstraint.__init__",
853870
side_effect=OperationalError(
854871
None, None, OSError("No space left on device")
855872
),
@@ -867,14 +884,6 @@ def _add_data():
867884
)
868885
states_index_names = {index["name"] for index in states_indexes}
869886
assert instance.use_legacy_events_index is True
870-
# The states.event_id foreign key constraint was removed when
871-
# migration to schema version 46
872-
assert (
873-
await instance.async_add_executor_job(
874-
_get_event_id_foreign_keys
875-
)
876-
is None
877-
)
878887

879888
await hass.async_stop()
880889

0 commit comments

Comments
 (0)