From d33a303a83bc813c8abc35cb755e89773afb5e2d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 19 Mar 2023 18:06:37 -1000 Subject: [PATCH] =?UTF-8?q?Fix=20statistics=20schema=20=C2=B5s=20precision?= =?UTF-8?q?=20auto=20repair=20being=20ineffective=20(#89902)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If a user manually migrated their database to MySQL or PostgresSQL and incorrectly created the timestamp columns as float we would fail to correct them to double because when we migrated to use timestamps for the columns I missed that we needed to change the columns and types for µs precision --- homeassistant/components/recorder/statistics.py | 14 +++++--------- tests/components/recorder/test_statistics.py | 6 +++--- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/recorder/statistics.py b/homeassistant/components/recorder/statistics.py index b117556b0af..989dc06db57 100644 --- a/homeassistant/components/recorder/statistics.py +++ b/homeassistant/components/recorder/statistics.py @@ -2590,8 +2590,8 @@ def _validate_db_schema( for column in columns: if stored[column] != expected[column]: schema_errors.add(f"{table_name}.{supports}") - _LOGGER.debug( - "Column %s in database table %s does not support %s (%s != %s)", + _LOGGER.error( + "Column %s in database table %s does not support %s (stored=%s != expected=%s)", column, table_name, supports, @@ -2727,18 +2727,14 @@ def correct_db_schema( ], ) if f"{table.__tablename__}.µs precision" in schema_errors: - # Attempt to convert datetime columns to µs precision - if instance.dialect_name == SupportedDialect.MYSQL: - datetime_type = "DATETIME(6)" - else: - datetime_type = "TIMESTAMP(6) WITH TIME ZONE" + # Attempt to convert timestamp columns to µs precision _modify_columns( session_maker, engine, table.__tablename__, [ - f"last_reset {datetime_type}", - f"start {datetime_type}", + "last_reset_ts DOUBLE PRECISION", + "start_ts DOUBLE PRECISION", ], ) diff --git a/tests/components/recorder/test_statistics.py b/tests/components/recorder/test_statistics.py index ad4d0de410e..d783d72be2d 100644 --- a/tests/components/recorder/test_statistics.py +++ b/tests/components/recorder/test_statistics.py @@ -1687,12 +1687,12 @@ async def test_validate_db_schema_fix_float_issue( @pytest.mark.parametrize( ("db_engine", "modification"), ( - ("mysql", ["last_reset DATETIME(6)", "start DATETIME(6)"]), + ("mysql", ["last_reset_ts DOUBLE PRECISION", "start_ts DOUBLE PRECISION"]), ( "postgresql", [ - "last_reset TIMESTAMP(6) WITH TIME ZONE", - "start TIMESTAMP(6) WITH TIME ZONE", + "last_reset_ts DOUBLE PRECISION", + "start_ts DOUBLE PRECISION", ], ), ),