diff --git a/homeassistant/components/recorder/migration.py b/homeassistant/components/recorder/migration.py index 1ced8b73207..a3d2955e55b 100644 --- a/homeassistant/components/recorder/migration.py +++ b/homeassistant/components/recorder/migration.py @@ -470,17 +470,20 @@ def _apply_update(instance, session, new_version, old_version): # noqa: C901 elif new_version == 18: # 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__] - ) + # Order matters! Statistics and StatisticsShortTerm have 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. + Base.metadata.drop_all( + bind=engine, + tables=[ + StatisticsShortTerm.__table__, + Statistics.__table__, + StatisticsMeta.__table__, + ], + ) StatisticsMeta.__table__.create(engine) + StatisticsShortTerm.__table__.create(engine) Statistics.__table__.create(engine) elif new_version == 19: # This adds the statistic runs table, insert a fake run to prevent duplicating @@ -527,23 +530,15 @@ def _apply_update(instance, session, new_version, old_version): # noqa: C901 # 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 engine.dialect.name == "oracle": - if ( - sqlalchemy.inspect(engine).has_table(StatisticsMeta.__tablename__) - or sqlalchemy.inspect(engine).has_table(Statistics.__tablename__) - or sqlalchemy.inspect(engine).has_table(StatisticsRuns.__tablename__) - or sqlalchemy.inspect(engine).has_table( - StatisticsShortTerm.__tablename__ - ) - ): - Base.metadata.drop_all( - bind=engine, - tables=[ - StatisticsShortTerm.__table__, - Statistics.__table__, - StatisticsMeta.__table__, - StatisticsRuns.__table__, - ], - ) + Base.metadata.drop_all( + bind=engine, + tables=[ + StatisticsShortTerm.__table__, + Statistics.__table__, + StatisticsMeta.__table__, + StatisticsRuns.__table__, + ], + ) StatisticsRuns.__table__.create(engine) StatisticsMeta.__table__.create(engine)