From 3142a106fc59d6534879e1aa91f1f02bd20b851f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 21 Apr 2022 12:39:18 -1000 Subject: [PATCH] Additional prep work for sqlalchemy 2.0 (#70358) --- homeassistant/components/recorder/__init__.py | 4 ++-- homeassistant/components/recorder/repack.py | 13 ++++++++++--- tests/components/recorder/test_history.py | 19 +++++++++++++------ tests/components/recorder/test_purge.py | 4 ++-- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/recorder/__init__.py b/homeassistant/components/recorder/__init__.py index 1116b261995..fe5dbb2ef65 100644 --- a/homeassistant/components/recorder/__init__.py +++ b/homeassistant/components/recorder/__init__.py @@ -1436,12 +1436,12 @@ class Recorder(threading.Thread): if self._using_file_sqlite: validate_or_move_away_sqlite_database(self.db_url) - self.engine = create_engine(self.db_url, **kwargs) + self.engine = create_engine(self.db_url, **kwargs, future=True) sqlalchemy_event.listen(self.engine, "connect", setup_recorder_connection) Base.metadata.create_all(self.engine) - self.get_session = scoped_session(sessionmaker(bind=self.engine)) + self.get_session = scoped_session(sessionmaker(bind=self.engine, future=True)) _LOGGER.debug("Connected to recorder database") @property diff --git a/homeassistant/components/recorder/repack.py b/homeassistant/components/recorder/repack.py index 95df0681ddb..c272f2827a0 100644 --- a/homeassistant/components/recorder/repack.py +++ b/homeassistant/components/recorder/repack.py @@ -4,6 +4,8 @@ from __future__ import annotations import logging from typing import TYPE_CHECKING +from sqlalchemy import text + if TYPE_CHECKING: from . import Recorder @@ -18,7 +20,9 @@ def repack_database(instance: Recorder) -> None: # Execute sqlite command to free up space on disk if dialect_name == "sqlite": _LOGGER.debug("Vacuuming SQL DB to free space") - instance.engine.execute("VACUUM") + with instance.engine.connect() as conn: + conn.execute(text("VACUUM")) + conn.commit() return # Execute postgresql vacuum command to free up space on disk @@ -27,11 +31,14 @@ def repack_database(instance: Recorder) -> None: with instance.engine.connect().execution_options( isolation_level="AUTOCOMMIT" ) as conn: - conn.execute("VACUUM") + conn.execute(text("VACUUM")) + conn.commit() return # Optimize mysql / mariadb tables to free up space on disk if dialect_name == "mysql": _LOGGER.debug("Optimizing SQL DB to free space") - instance.engine.execute("OPTIMIZE TABLE states, events, recorder_runs") + with instance.engine.connect() as conn: + conn.execute(text("OPTIMIZE TABLE states, events, recorder_runs")) + conn.commit() return diff --git a/tests/components/recorder/test_history.py b/tests/components/recorder/test_history.py index 42a1a1fafb5..6e917a5b77d 100644 --- a/tests/components/recorder/test_history.py +++ b/tests/components/recorder/test_history.py @@ -6,6 +6,7 @@ import json from unittest.mock import patch, sentinel import pytest +from sqlalchemy import text from homeassistant.components import recorder from homeassistant.components.recorder import history @@ -628,8 +629,10 @@ async def test_state_changes_during_period_query_during_migration_to_schema_25( state = hist[entity_id][0] assert state.attributes == {"name": "the shared light"} - instance.engine.execute("update states set attributes_id=NULL;") - instance.engine.execute("drop table state_attributes;") + with instance.engine.connect() as conn: + conn.execute(text("update states set attributes_id=NULL;")) + conn.execute(text("drop table state_attributes;")) + conn.commit() with patch.object(instance, "migration_in_progress", True): no_attributes = True @@ -670,8 +673,10 @@ async def test_get_states_query_during_migration_to_schema_25( state = hist[0] assert state.attributes == {"name": "the shared light"} - instance.engine.execute("update states set attributes_id=NULL;") - instance.engine.execute("drop table state_attributes;") + with instance.engine.connect() as conn: + conn.execute(text("update states set attributes_id=NULL;")) + conn.execute(text("drop table state_attributes;")) + conn.commit() with patch.object(instance, "migration_in_progress", True): no_attributes = True @@ -711,8 +716,10 @@ async def test_get_states_query_during_migration_to_schema_25_multiple_entities( assert hist[0].attributes == {"name": "the shared light"} assert hist[1].attributes == {"name": "the shared light"} - instance.engine.execute("update states set attributes_id=NULL;") - instance.engine.execute("drop table state_attributes;") + with instance.engine.connect() as conn: + conn.execute(text("update states set attributes_id=NULL;")) + conn.execute(text("drop table state_attributes;")) + conn.commit() with patch.object(instance, "migration_in_progress", True): no_attributes = True diff --git a/tests/components/recorder/test_purge.py b/tests/components/recorder/test_purge.py index 43e195e3a21..0194cb0c57c 100644 --- a/tests/components/recorder/test_purge.py +++ b/tests/components/recorder/test_purge.py @@ -49,7 +49,7 @@ async def test_purge_old_states( assert states.count() == 6 assert states[0].old_state_id is None - assert states[-1].old_state_id == states[-2].state_id + assert states[5].old_state_id == states[4].state_id assert state_attributes.count() == 3 events = session.query(Events).filter(Events.event_type == "state_changed") @@ -94,7 +94,7 @@ async def test_purge_old_states( states = session.query(States) assert states.count() == 6 assert states[0].old_state_id is None - assert states[-1].old_state_id == states[-2].state_id + assert states[5].old_state_id == states[4].state_id events = session.query(Events).filter(Events.event_type == "state_changed") assert events.count() == 6