Add index to old_state_id column for postgres and older databases (#44757)

* Add index to old_state_id column for older databases

The schema was updated in #43610 but the index was not
added on migration.

* Handle postgresql missing ondelete

* create index first
This commit is contained in:
J. Nick Koston 2021-01-03 23:51:44 -10:00 committed by GitHub
parent 3c62c21991
commit 12af87bc6e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 2 deletions

View file

@ -211,7 +211,13 @@ def _update_states_table_with_foreign_key_options(engine):
inspector = reflection.Inspector.from_engine(engine)
alters = []
for foreign_key in inspector.get_foreign_keys(TABLE_STATES):
if foreign_key["name"] and not foreign_key["options"]:
if foreign_key["name"] and (
# MySQL/MariaDB will have empty options
not foreign_key["options"]
or
# Postgres will have ondelete set to None
foreign_key["options"].get("ondelete") is None
):
alters.append(
{
"old_fk": ForeignKeyConstraint((), (), name=foreign_key["name"]),
@ -312,6 +318,10 @@ def _apply_update(engine, new_version, old_version):
_create_index(engine, "events", "ix_events_event_type_time_fired")
_drop_index(engine, "events", "ix_events_event_type")
elif new_version == 10:
# Now done in step 11
pass
elif new_version == 11:
_create_index(engine, "states", "ix_states_old_state_id")
_update_states_table_with_foreign_key_options(engine)
else:
raise ValueError(f"No schema migration defined for version {new_version}")

View file

@ -25,7 +25,7 @@ import homeassistant.util.dt as dt_util
# pylint: disable=invalid-name
Base = declarative_base()
SCHEMA_VERSION = 10
SCHEMA_VERSION = 11
_LOGGER = logging.getLogger(__name__)