Use BIGINT SQL type for ID columns (#123973)

Redo recorder ID migration from INT to BIGINT
This commit is contained in:
Erik Montnemery 2024-08-17 11:01:14 +02:00 committed by GitHub
parent 6c01e4b99c
commit a7bca9bcea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 26 additions and 8 deletions

View file

@ -77,7 +77,7 @@ class LegacyBase(DeclarativeBase):
"""Base class for tables, used for schema migration.""" """Base class for tables, used for schema migration."""
SCHEMA_VERSION = 45 SCHEMA_VERSION = 47
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)

View file

@ -1112,7 +1112,7 @@ class _SchemaVersion16Migrator(_SchemaVersionMigrator, target_version=16):
# Version 16 changes settings for the foreign key constraint on # Version 16 changes settings for the foreign key constraint on
# states.old_state_id. Dropping the constraint is not really correct # states.old_state_id. Dropping the constraint is not really correct
# we should have recreated it instead. Recreating the constraint now # 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( _drop_foreign_key_constraints(
self.session_maker, self.engine, TABLE_STATES, "old_state_id" 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 = ( FOREIGN_COLUMNS = (
( (
"events", "events",
@ -1669,7 +1687,7 @@ FOREIGN_COLUMNS = (
) )
class _SchemaVersion44Migrator(_SchemaVersionMigrator, target_version=44): class _SchemaVersion46Migrator(_SchemaVersionMigrator, target_version=46):
def _apply_update(self) -> None: def _apply_update(self) -> None:
"""Version specific update method.""" """Version specific update method."""
# We skip this step for SQLITE, it doesn't have differently sized integers # 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: def _apply_update(self) -> None:
"""Version specific update method.""" """Version specific update method."""
# We skip this step for SQLITE, it doesn't have differently sized integers # We skip this step for SQLITE, it doesn't have differently sized integers
if self.engine.dialect.name == SupportedDialect.SQLITE: if self.engine.dialect.name == SupportedDialect.SQLITE:
return return
# Restore constraints dropped in migration to schema version 44 # Restore constraints dropped in migration to schema version 46
_restore_foreign_key_constraints( _restore_foreign_key_constraints(
self.session_maker, self.session_maker,
self.engine, self.engine,

View file

@ -215,7 +215,7 @@ async def test_database_migration_failed(
# Test error handling in _modify_columns # Test error handling in _modify_columns
(12, "sqlalchemy.engine.base.Connection.execute", False, 1, 0), (12, "sqlalchemy.engine.base.Connection.execute", False, 1, 0),
# Test error handling in _drop_foreign_key_constraints # 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"]) @pytest.mark.skip_on_db_engine(["sqlite"])

View file

@ -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. 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 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. cleanup_legacy_states_event_ids is not likely to fail.
""" """
importlib.import_module(SCHEMA_MODULE) 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} states_index_names = {index["name"] for index in states_indexes}
assert instance.use_legacy_events_index is True assert instance.use_legacy_events_index is True
# The states.event_id foreign key constraint was removed when # The states.event_id foreign key constraint was removed when
# migration to schema version 44 # migration to schema version 46
assert ( assert (
await instance.async_add_executor_job( await instance.async_add_executor_job(
_get_event_id_foreign_keys _get_event_id_foreign_keys