From 85d5ea2eca8be72c3ae656a315ec45ccc3d26690 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 28 Jan 2023 17:06:07 -1000 Subject: [PATCH] Fix v32 schema migration when MySQL global.time_zone is configured with non-UTC timezone (#86867) * Fix v32 schema migration when MySQL timezone is not UTC * tweak --- homeassistant/components/recorder/migration.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/recorder/migration.py b/homeassistant/components/recorder/migration.py index 746adf11c18..af1446400c2 100644 --- a/homeassistant/components/recorder/migration.py +++ b/homeassistant/components/recorder/migration.py @@ -971,7 +971,9 @@ def _migrate_columns_to_timestamp( result = session.connection().execute( text( "UPDATE events set time_fired_ts=" - "IF(time_fired is NULL,0,UNIX_TIMESTAMP(time_fired)) " + "IF(time_fired is NULL,0," + "UNIX_TIMESTAMP(CONVERT_TZ(time_fired,'+00:00',@@global.time_zone))" + ") " "where time_fired_ts is NULL " "LIMIT 250000;" ) @@ -982,8 +984,11 @@ def _migrate_columns_to_timestamp( result = session.connection().execute( text( "UPDATE states set last_updated_ts=" - "IF(last_updated is NULL,0,UNIX_TIMESTAMP(last_updated)), " - "last_changed_ts=UNIX_TIMESTAMP(last_changed) " + "IF(last_updated is NULL,0," + "UNIX_TIMESTAMP(CONVERT_TZ(last_updated,'+00:00',@@global.time_zone)) " + "), " + "last_changed_ts=" + "UNIX_TIMESTAMP(CONVERT_TZ(last_changed,'+00:00',@@global.time_zone)) " "where last_updated_ts is NULL " "LIMIT 250000;" )