From 81c17edadf6c920163d8001b83170ed01089df9f Mon Sep 17 00:00:00 2001 From: Marcin Ciupak Date: Tue, 4 May 2021 19:11:00 +0200 Subject: [PATCH 1/6] Use explicit types for literals in logbook --- homeassistant/components/logbook/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/logbook/__init__.py b/homeassistant/components/logbook/__init__.py index 8d216b5c6f0ac..a9afc044a62e9 100644 --- a/homeassistant/components/logbook/__init__.py +++ b/homeassistant/components/logbook/__init__.py @@ -500,10 +500,10 @@ def _generate_events_query(session): def _generate_events_query_without_states(session): return session.query( *EVENT_COLUMNS, - literal(None).label("state"), - literal(None).label("entity_id"), - literal(None).label("domain"), - literal(None).label("attributes"), + literal(value=None, type_=sqlalchemy.String).label("state"), + literal(value=None, type_=sqlalchemy.String).label("entity_id"), + literal(value=None, type_=sqlalchemy.String).label("domain"), + literal(value=None, type_=sqlalchemy.Text).label("attributes"), ) From c0602fe95f77d8edf5b13dcad094c0d31adf6ab0 Mon Sep 17 00:00:00 2001 From: Marcin Ciupak Date: Tue, 4 May 2021 19:11:00 +0200 Subject: [PATCH 2/6] Update models for Oracel DB --- homeassistant/components/recorder/models.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/recorder/models.py b/homeassistant/components/recorder/models.py index 3459da309eebf..1b2675a4ec723 100644 --- a/homeassistant/components/recorder/models.py +++ b/homeassistant/components/recorder/models.py @@ -7,6 +7,7 @@ Column, DateTime, ForeignKey, + Identity, Index, Integer, String, @@ -53,7 +54,7 @@ class Events(Base): # type: ignore "mysql_collate": "utf8mb4_unicode_ci", } __tablename__ = TABLE_EVENTS - event_id = Column(Integer, primary_key=True) + event_id = Column(Integer, Identity(), primary_key=True) event_type = Column(String(MAX_LENGTH_EVENT_TYPE)) event_data = Column(Text().with_variant(mysql.LONGTEXT, "mysql")) origin = Column(String(32)) @@ -120,7 +121,7 @@ class States(Base): # type: ignore "mysql_collate": "utf8mb4_unicode_ci", } __tablename__ = TABLE_STATES - state_id = Column(Integer, primary_key=True) + state_id = Column(Integer, Identity(), primary_key=True) domain = Column(String(64)) entity_id = Column(String(255)) state = Column(String(255)) @@ -131,9 +132,7 @@ class States(Base): # type: ignore last_changed = Column(DATETIME_TYPE, default=dt_util.utcnow) last_updated = Column(DATETIME_TYPE, default=dt_util.utcnow, index=True) created = Column(DATETIME_TYPE, default=dt_util.utcnow) - old_state_id = Column( - Integer, ForeignKey("states.state_id", ondelete="NO ACTION"), index=True - ) + old_state_id = Column(Integer, ForeignKey("states.state_id"), index=True) event = relationship("Events", uselist=False) old_state = relationship("States", remote_side=[state_id]) @@ -202,7 +201,7 @@ class RecorderRuns(Base): # type: ignore """Representation of recorder run.""" __tablename__ = TABLE_RECORDER_RUNS - run_id = Column(Integer, primary_key=True) + run_id = Column(Integer, Identity(), primary_key=True) start = Column(DateTime(timezone=True), default=dt_util.utcnow) end = Column(DateTime(timezone=True)) closed_incorrect = Column(Boolean, default=False) @@ -253,7 +252,7 @@ class SchemaChanges(Base): # type: ignore """Representation of schema version changes.""" __tablename__ = TABLE_SCHEMA_CHANGES - change_id = Column(Integer, primary_key=True) + change_id = Column(Integer, Identity(), primary_key=True) schema_version = Column(Integer) changed = Column(DateTime(timezone=True), default=dt_util.utcnow) From 0b37293713c8f46bc48641b615b44d1e1edd8606 Mon Sep 17 00:00:00 2001 From: Marcin Ciupak Date: Tue, 4 May 2021 20:35:40 +0200 Subject: [PATCH 3/6] Update sqlalchemy to 1.4.12 --- homeassistant/components/recorder/manifest.json | 2 +- homeassistant/components/sql/manifest.json | 2 +- homeassistant/package_constraints.txt | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/recorder/manifest.json b/homeassistant/components/recorder/manifest.json index a79a79fbc4a7f..b202d68f59553 100644 --- a/homeassistant/components/recorder/manifest.json +++ b/homeassistant/components/recorder/manifest.json @@ -2,7 +2,7 @@ "domain": "recorder", "name": "Recorder", "documentation": "https://www.home-assistant.io/integrations/recorder", - "requirements": ["sqlalchemy==1.4.11"], + "requirements": ["sqlalchemy==1.4.12"], "codeowners": [], "quality_scale": "internal", "iot_class": "local_push" diff --git a/homeassistant/components/sql/manifest.json b/homeassistant/components/sql/manifest.json index 1716664c12990..5291a8553da0e 100644 --- a/homeassistant/components/sql/manifest.json +++ b/homeassistant/components/sql/manifest.json @@ -2,7 +2,7 @@ "domain": "sql", "name": "SQL", "documentation": "https://www.home-assistant.io/integrations/sql", - "requirements": ["sqlalchemy==1.4.11"], + "requirements": ["sqlalchemy==1.4.12"], "codeowners": ["@dgomes"], "iot_class": "local_polling" } diff --git a/homeassistant/package_constraints.txt b/homeassistant/package_constraints.txt index 0d9cbe7990b5b..1ed12cd321da8 100644 --- a/homeassistant/package_constraints.txt +++ b/homeassistant/package_constraints.txt @@ -30,7 +30,7 @@ pyyaml==5.4.1 requests==2.25.1 ruamel.yaml==0.15.100 scapy==2.4.5 -sqlalchemy==1.4.11 +sqlalchemy==1.4.12 voluptuous-serialize==2.4.0 voluptuous==0.12.1 yarl==1.6.3 diff --git a/requirements_all.txt b/requirements_all.txt index c377e04a40b21..028e3cef87bd9 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2142,7 +2142,7 @@ spotipy==2.18.0 # homeassistant.components.recorder # homeassistant.components.sql -sqlalchemy==1.4.11 +sqlalchemy==1.4.12 # homeassistant.components.srp_energy srpenergy==1.3.2 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 3a87915695150..ebbd610cbe741 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1144,7 +1144,7 @@ spotipy==2.18.0 # homeassistant.components.recorder # homeassistant.components.sql -sqlalchemy==1.4.11 +sqlalchemy==1.4.12 # homeassistant.components.srp_energy srpenergy==1.3.2 From 9765986b51ecdb84696cbd3ded1f124d808bcb33 Mon Sep 17 00:00:00 2001 From: Marcin Ciupak Date: Fri, 7 May 2021 20:39:59 +0200 Subject: [PATCH 4/6] Revert "Update sqlalchemy to 1.4.12" This reverts commit 0b37293713c8f46bc48641b615b44d1e1edd8606. --- homeassistant/components/recorder/manifest.json | 2 +- homeassistant/components/sql/manifest.json | 2 +- homeassistant/package_constraints.txt | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/recorder/manifest.json b/homeassistant/components/recorder/manifest.json index b202d68f59553..a79a79fbc4a7f 100644 --- a/homeassistant/components/recorder/manifest.json +++ b/homeassistant/components/recorder/manifest.json @@ -2,7 +2,7 @@ "domain": "recorder", "name": "Recorder", "documentation": "https://www.home-assistant.io/integrations/recorder", - "requirements": ["sqlalchemy==1.4.12"], + "requirements": ["sqlalchemy==1.4.11"], "codeowners": [], "quality_scale": "internal", "iot_class": "local_push" diff --git a/homeassistant/components/sql/manifest.json b/homeassistant/components/sql/manifest.json index 5291a8553da0e..1716664c12990 100644 --- a/homeassistant/components/sql/manifest.json +++ b/homeassistant/components/sql/manifest.json @@ -2,7 +2,7 @@ "domain": "sql", "name": "SQL", "documentation": "https://www.home-assistant.io/integrations/sql", - "requirements": ["sqlalchemy==1.4.12"], + "requirements": ["sqlalchemy==1.4.11"], "codeowners": ["@dgomes"], "iot_class": "local_polling" } diff --git a/homeassistant/package_constraints.txt b/homeassistant/package_constraints.txt index 1ed12cd321da8..0d9cbe7990b5b 100644 --- a/homeassistant/package_constraints.txt +++ b/homeassistant/package_constraints.txt @@ -30,7 +30,7 @@ pyyaml==5.4.1 requests==2.25.1 ruamel.yaml==0.15.100 scapy==2.4.5 -sqlalchemy==1.4.12 +sqlalchemy==1.4.11 voluptuous-serialize==2.4.0 voluptuous==0.12.1 yarl==1.6.3 diff --git a/requirements_all.txt b/requirements_all.txt index 028e3cef87bd9..c377e04a40b21 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2142,7 +2142,7 @@ spotipy==2.18.0 # homeassistant.components.recorder # homeassistant.components.sql -sqlalchemy==1.4.12 +sqlalchemy==1.4.11 # homeassistant.components.srp_energy srpenergy==1.3.2 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index ebbd610cbe741..3a87915695150 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1144,7 +1144,7 @@ spotipy==2.18.0 # homeassistant.components.recorder # homeassistant.components.sql -sqlalchemy==1.4.12 +sqlalchemy==1.4.11 # homeassistant.components.srp_energy srpenergy==1.3.2 From 45733e748306f5ab8e95d27acca3a0380e39c37e Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 12 May 2021 05:30:53 -1000 Subject: [PATCH 5/6] Drop the constraint on old_state_id --- .../components/recorder/migration.py | 32 +++++++++++++++++++ homeassistant/components/recorder/models.py | 2 +- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/recorder/migration.py b/homeassistant/components/recorder/migration.py index 17b6e277614fd..868ae43607188 100644 --- a/homeassistant/components/recorder/migration.py +++ b/homeassistant/components/recorder/migration.py @@ -312,6 +312,34 @@ def _update_states_table_with_foreign_key_options(connection, engine): ) +def _drop_foreign_key_constraints(connection, engine, table, columns): + """Drop foreign key constraints for a table on specific columns.""" + inspector = sqlalchemy.inspect(engine) + drops = [] + for foreign_key in inspector.get_foreign_keys(table): + if ( + foreign_key["name"] + and foreign_key["options"].get("ondelete") + and foreign_key["constrained_columns"] == columns + ): + drops.append(ForeignKeyConstraint((), (), name=foreign_key["name"])) + + # Bind the ForeignKeyConstraints to the table + old_table = Table( # noqa: F841 pylint: disable=unused-variable + table, MetaData(), *drops + ) + + for drop in drops: + try: + connection.execute(DropConstraint(drop)) + except (InternalError, OperationalError): + _LOGGER.exception( + "Could not drop foreign constraints in %s table on %s", + TABLE_STATES, + columns, + ) + + def _apply_update(engine, session, new_version, old_version): """Perform operations to bring schema up to date.""" connection = session.connection() @@ -415,6 +443,10 @@ def _apply_update(engine, session, new_version, old_version): ) elif new_version == 14: _modify_columns(connection, engine, "events", ["event_type VARCHAR(64)"]) + elif new_version == 15: + _drop_foreign_key_constraints( + connection, engine, TABLE_STATES, ["old_state_id"] + ) else: raise ValueError(f"No schema migration defined for version {new_version}") diff --git a/homeassistant/components/recorder/models.py b/homeassistant/components/recorder/models.py index 1b2675a4ec723..da39c656b21da 100644 --- a/homeassistant/components/recorder/models.py +++ b/homeassistant/components/recorder/models.py @@ -28,7 +28,7 @@ # pylint: disable=invalid-name Base = declarative_base() -SCHEMA_VERSION = 14 +SCHEMA_VERSION = 15 _LOGGER = logging.getLogger(__name__) From 520304a69dc0836d7fed5695342aa0bb9a71fcb4 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 20 May 2021 05:12:06 -1000 Subject: [PATCH 6/6] bump schema version to fix conflict --- homeassistant/components/recorder/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/recorder/models.py b/homeassistant/components/recorder/models.py index ab17fbf6b027c..4fefcaa19e367 100644 --- a/homeassistant/components/recorder/models.py +++ b/homeassistant/components/recorder/models.py @@ -29,7 +29,7 @@ # pylint: disable=invalid-name Base = declarative_base() -SCHEMA_VERSION = 15 +SCHEMA_VERSION = 16 _LOGGER = logging.getLogger(__name__)