From 6bb80adbb9079e1304062bbc565a3be84a076275 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 20 Mar 2023 06:15:11 -1000 Subject: [PATCH] Rollback the session after performing stats schema validation (#89904) --- homeassistant/components/recorder/statistics.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/recorder/statistics.py b/homeassistant/components/recorder/statistics.py index 989dc06db57..34adcbddcc6 100644 --- a/homeassistant/components/recorder/statistics.py +++ b/homeassistant/components/recorder/statistics.py @@ -2504,7 +2504,9 @@ def _validate_db_schema_utf8( # Try inserting some metadata which needs utfmb4 support try: - with session_scope(session=session_maker()) as session: + # Mark the session as read_only to ensure that the test data is not committed + # to the database and we always rollback when the scope is exited + with session_scope(session=session_maker(), read_only=True) as session: old_metadata_dict = statistics_meta_manager.get_many( session, statistic_ids={statistic_id} ) @@ -2605,7 +2607,9 @@ def _validate_db_schema( StatisticsShortTerm, ) try: - with session_scope(session=session_maker()) as session: + # Mark the session as read_only to ensure that the test data is not committed + # to the database and we always rollback when the scope is exited + with session_scope(session=session_maker(), read_only=True) as session: for table in tables: _import_statistics_with_session( instance, session, metadata, (statistics,), table