diff --git a/homeassistant/components/recorder/migration.py b/homeassistant/components/recorder/migration.py index c694aa678f0..24220a27eee 100644 --- a/homeassistant/components/recorder/migration.py +++ b/homeassistant/components/recorder/migration.py @@ -507,6 +507,17 @@ def _apply_update(engine, session, new_version, old_version): # noqa: C901 "statistics", ["sum_increase DOUBLE PRECISION"], ) + # 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" + ) + ) + except SQLAlchemyError: + pass else: raise ValueError(f"No schema migration defined for version {new_version}") diff --git a/homeassistant/components/recorder/models.py b/homeassistant/components/recorder/models.py index 11c614c9ea1..0a4362ba68c 100644 --- a/homeassistant/components/recorder/models.py +++ b/homeassistant/components/recorder/models.py @@ -238,6 +238,7 @@ class Statistics(Base): # type: ignore __table_args__ = ( # Used for fetching statistics for a certain entity at a specific time Index("ix_statistics_statistic_id_start", "metadata_id", "start"), + {"mysql_default_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"}, ) __tablename__ = TABLE_STATISTICS id = Column(Integer, primary_key=True)