Skip to content

Commit

Permalink
Add index to old_state_id column for postgres and older databases (ho…
Browse files Browse the repository at this point in the history
…me-assistant#44757)

* Add index to old_state_id column for older databases

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

* Handle postgresql missing ondelete

* create index first
  • Loading branch information
bdraco authored Jan 4, 2021
1 parent 3c62c21 commit 12af87b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion homeassistant/components/recorder/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
Expand Down Expand Up @@ -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}")
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/recorder/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# pylint: disable=invalid-name
Base = declarative_base()

SCHEMA_VERSION = 10
SCHEMA_VERSION = 11

_LOGGER = logging.getLogger(__name__)

Expand Down

0 comments on commit 12af87b

Please sign in to comment.