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
pull/56029/head
Erik Montnemery 2021-09-09 19:26:28 +02:00 committed by GitHub
parent 1b0e014783
commit 88dbc6373f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 8 deletions

View File

@ -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:
for table in ("events", "states", "statistics_meta"):
with contextlib.suppress(SQLAlchemyError):
connection.execute(
text(
"ALTER TABLE statistics_meta CONVERT TO "
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}")