From a7bca9bceace548494a8fb0ad1a3d51d274cc56c Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Sat, 17 Aug 2024 11:01:14 +0200 Subject: [PATCH] Use BIGINT SQL type for ID columns (#123973) Redo recorder ID migration from INT to BIGINT --- .../components/recorder/db_schema.py | 2 +- .../components/recorder/migration.py | 26 ++++++++++++++++--- tests/components/recorder/test_migrate.py | 2 +- .../components/recorder/test_v32_migration.py | 4 +-- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/recorder/db_schema.py b/homeassistant/components/recorder/db_schema.py index dd293ed6bc2..f84459675ae 100644 --- a/homeassistant/components/recorder/db_schema.py +++ b/homeassistant/components/recorder/db_schema.py @@ -77,7 +77,7 @@ class LegacyBase(DeclarativeBase): """Base class for tables, used for schema migration.""" -SCHEMA_VERSION = 45 +SCHEMA_VERSION = 47 _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/recorder/migration.py b/homeassistant/components/recorder/migration.py index 185be02e9aa..a4a5fa87466 100644 --- a/homeassistant/components/recorder/migration.py +++ b/homeassistant/components/recorder/migration.py @@ -1112,7 +1112,7 @@ class _SchemaVersion16Migrator(_SchemaVersionMigrator, target_version=16): # Version 16 changes settings for the foreign key constraint on # states.old_state_id. Dropping the constraint is not really correct # we should have recreated it instead. Recreating the constraint now - # happens in the migration to schema version 45. + # happens in the migration to schema version 47. _drop_foreign_key_constraints( self.session_maker, self.engine, TABLE_STATES, "old_state_id" ) @@ -1637,6 +1637,24 @@ class _SchemaVersion43Migrator(_SchemaVersionMigrator, target_version=43): ) +class _SchemaVersion44Migrator(_SchemaVersionMigrator, target_version=44): + def _apply_update(self) -> None: + """Version specific update method.""" + # The changes in this version are identical to the changes in version + # 46. We apply the same changes again because the migration code previously + # swallowed errors which caused some users' databases to end up in an + # undefined state after the migration. + + +class _SchemaVersion45Migrator(_SchemaVersionMigrator, target_version=45): + def _apply_update(self) -> None: + """Version specific update method.""" + # The changes in this version are identical to the changes in version + # 47. We apply the same changes again because the migration code previously + # swallowed errors which caused some users' databases to end up in an + # undefined state after the migration. + + FOREIGN_COLUMNS = ( ( "events", @@ -1669,7 +1687,7 @@ FOREIGN_COLUMNS = ( ) -class _SchemaVersion44Migrator(_SchemaVersionMigrator, target_version=44): +class _SchemaVersion46Migrator(_SchemaVersionMigrator, target_version=46): def _apply_update(self) -> None: """Version specific update method.""" # We skip this step for SQLITE, it doesn't have differently sized integers @@ -1720,14 +1738,14 @@ class _SchemaVersion44Migrator(_SchemaVersionMigrator, target_version=44): ) -class _SchemaVersion45Migrator(_SchemaVersionMigrator, target_version=45): +class _SchemaVersion47Migrator(_SchemaVersionMigrator, target_version=47): def _apply_update(self) -> None: """Version specific update method.""" # We skip this step for SQLITE, it doesn't have differently sized integers if self.engine.dialect.name == SupportedDialect.SQLITE: return - # Restore constraints dropped in migration to schema version 44 + # Restore constraints dropped in migration to schema version 46 _restore_foreign_key_constraints( self.session_maker, self.engine, diff --git a/tests/components/recorder/test_migrate.py b/tests/components/recorder/test_migrate.py index b56dfe3e189..1edbac3c566 100644 --- a/tests/components/recorder/test_migrate.py +++ b/tests/components/recorder/test_migrate.py @@ -215,7 +215,7 @@ async def test_database_migration_failed( # Test error handling in _modify_columns (12, "sqlalchemy.engine.base.Connection.execute", False, 1, 0), # Test error handling in _drop_foreign_key_constraints - (44, "homeassistant.components.recorder.migration.DropConstraint", False, 2, 1), + (46, "homeassistant.components.recorder.migration.DropConstraint", False, 2, 1), ], ) @pytest.mark.skip_on_db_engine(["sqlite"]) diff --git a/tests/components/recorder/test_v32_migration.py b/tests/components/recorder/test_v32_migration.py index c9ba330b758..1006a03f4ec 100644 --- a/tests/components/recorder/test_v32_migration.py +++ b/tests/components/recorder/test_v32_migration.py @@ -640,7 +640,7 @@ async def test_out_of_disk_space_while_removing_foreign_key( ix_states_event_id is removed from the states table. Note that the test is somewhat forced; the states.event_id foreign key constraint is - removed when migrating to schema version 44, inspecting the schema in + removed when migrating to schema version 46, inspecting the schema in cleanup_legacy_states_event_ids is not likely to fail. """ importlib.import_module(SCHEMA_MODULE) @@ -779,7 +779,7 @@ async def test_out_of_disk_space_while_removing_foreign_key( states_index_names = {index["name"] for index in states_indexes} assert instance.use_legacy_events_index is True # The states.event_id foreign key constraint was removed when - # migration to schema version 44 + # migration to schema version 46 assert ( await instance.async_add_executor_job( _get_event_id_foreign_keys