From dd4f1a0d0f3babd2720c112343aaad05741ec035 Mon Sep 17 00:00:00 2001
From: Erik Montnemery <erik@montnemery.com>
Date: Tue, 10 Sep 2024 21:00:06 +0200
Subject: [PATCH] Simplify recorder statistics_meta_manager (#125648)

---
 .../recorder/auto_repairs/statistics/duplicates.py           | 5 ++---
 homeassistant/components/recorder/const.py                   | 1 -
 homeassistant/components/recorder/core.py                    | 5 +----
 3 files changed, 3 insertions(+), 8 deletions(-)

diff --git a/homeassistant/components/recorder/auto_repairs/statistics/duplicates.py b/homeassistant/components/recorder/auto_repairs/statistics/duplicates.py
index 06a5c5258f1..b73744ef0d1 100644
--- a/homeassistant/components/recorder/auto_repairs/statistics/duplicates.py
+++ b/homeassistant/components/recorder/auto_repairs/statistics/duplicates.py
@@ -247,12 +247,11 @@ def delete_statistics_meta_duplicates(instance: Recorder, session: Session) -> N
     """Identify and delete duplicated statistics_meta.
 
     This is used when migrating from schema version 28 to schema version 29.
+    Note: If this needs to be called during live schema migration it needs to
+    be modified to reload the statistics_meta_manager.
     """
     deleted_statistics_rows = _delete_statistics_meta_duplicates(session)
     if deleted_statistics_rows:
-        statistics_meta_manager = instance.statistics_meta_manager
-        statistics_meta_manager.reset()
-        statistics_meta_manager.load(session)
         _LOGGER.info(
             "Deleted %s duplicated statistics_meta rows", deleted_statistics_rows
         )
diff --git a/homeassistant/components/recorder/const.py b/homeassistant/components/recorder/const.py
index 066ae938971..bc909448317 100644
--- a/homeassistant/components/recorder/const.py
+++ b/homeassistant/components/recorder/const.py
@@ -54,7 +54,6 @@ ATTR_APPLY_FILTER = "apply_filter"
 
 KEEPALIVE_TIME = 30
 
-STATISTICS_ROWS_SCHEMA_VERSION = 23
 CONTEXT_ID_AS_BINARY_SCHEMA_VERSION = 36
 EVENT_TYPE_IDS_SCHEMA_VERSION = 37
 STATES_META_SCHEMA_VERSION = 38
diff --git a/homeassistant/components/recorder/core.py b/homeassistant/components/recorder/core.py
index 002d8937e3a..0c80d979268 100644
--- a/homeassistant/components/recorder/core.py
+++ b/homeassistant/components/recorder/core.py
@@ -63,7 +63,6 @@ from .const import (
     MYSQLDB_URL_PREFIX,
     SQLITE_MAX_BIND_VARS,
     SQLITE_URL_PREFIX,
-    STATISTICS_ROWS_SCHEMA_VERSION,
     SupportedDialect,
 )
 from .db_schema import (
@@ -797,9 +796,7 @@ class Recorder(threading.Thread):
             # since we want the frontend queries to avoid a thundering
             # herd of queries to find the statistics meta data if
             # there are a lot of statistics graphs on the frontend.
-            schema_version = self.schema_version
-            if schema_version >= STATISTICS_ROWS_SCHEMA_VERSION:
-                self.statistics_meta_manager.load(session)
+            self.statistics_meta_manager.load(session)
 
             migration_changes: dict[str, int] = {
                 row[0]: row[1]
-- 
GitLab