From 74029a094821b6368c8e08afe320b73fe9d0ef65 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Mon, 5 Jul 2021 13:34:40 +0200 Subject: [PATCH] Fix Statistics recorder migration path by dropping in pairs (#52453) --- .../components/recorder/migration.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/recorder/migration.py b/homeassistant/components/recorder/migration.py index 4cb42c61097..2ed676bfdb9 100644 --- a/homeassistant/components/recorder/migration.py +++ b/homeassistant/components/recorder/migration.py @@ -461,14 +461,19 @@ def _apply_update(engine, session, new_version, old_version): # This dropped the statistics table, done again in version 18. pass elif new_version == 18: - # Recreate the statisticsmeta tables - if sqlalchemy.inspect(engine).has_table(StatisticsMeta.__tablename__): - StatisticsMeta.__table__.drop(engine) - StatisticsMeta.__table__.create(engine) + # Recreate the statistics and statistics meta tables. + # + # Order matters! Statistics has a relation with StatisticsMeta, + # so statistics need to be deleted before meta (or in pair depending + # on the SQL backend); and meta needs to be created before statistics. + if sqlalchemy.inspect(engine).has_table( + StatisticsMeta.__tablename__ + ) or sqlalchemy.inspect(engine).has_table(Statistics.__tablename__): + Base.metadata.drop_all( + bind=engine, tables=[Statistics.__table__, StatisticsMeta.__table__] + ) - # Recreate the statistics table - if sqlalchemy.inspect(engine).has_table(Statistics.__tablename__): - Statistics.__table__.drop(engine) + StatisticsMeta.__table__.create(engine) Statistics.__table__.create(engine) else: raise ValueError(f"No schema migration defined for version {new_version}")