From 88dbc6373f8d1608944785a2327fa0ef41c43e54 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Thu, 9 Sep 2021 19:26:28 +0200 Subject: [PATCH] Make sure character set of events, states tables is utf8 (#56012) * Make sure character set of events, states tables is utf8 * Pylint * Apply suggestions from code review --- homeassistant/components/recorder/migration.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/recorder/migration.py b/homeassistant/components/recorder/migration.py index 24220a27eee..74d95bb6c9c 100644 --- a/homeassistant/components/recorder/migration.py +++ b/homeassistant/components/recorder/migration.py @@ -1,4 +1,5 @@ """Schema migration helpers.""" +import contextlib from datetime import timedelta import logging @@ -509,15 +510,14 @@ def _apply_update(engine, session, new_version, old_version): # noqa: C901 ) # Try to change the character set of the statistic_meta table if engine.dialect.name == "mysql": - try: - connection.execute( - text( - "ALTER TABLE statistics_meta CONVERT TO " - "CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci" + for table in ("events", "states", "statistics_meta"): + with contextlib.suppress(SQLAlchemyError): + connection.execute( + text( + f"ALTER TABLE {table} CONVERT TO " + "CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci" + ) ) - ) - except SQLAlchemyError: - pass else: raise ValueError(f"No schema migration defined for version {new_version}")