Improve migration to recorder schema version 21 (#56204)

This commit is contained in:
Erik Montnemery 2021-09-27 19:31:40 +02:00 committed by GitHub
parent e5642a8648
commit fe66d6295c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -502,14 +502,24 @@ def _apply_update(instance, session, new_version, old_version): # noqa: C901
], ],
) )
elif new_version == 21: elif new_version == 21:
if engine.dialect.name in ["mysql", "oracle", "postgresql"]:
data_type = "DOUBLE PRECISION"
else:
data_type = "FLOAT"
_add_columns( _add_columns(
connection, connection,
"statistics", "statistics",
["sum_increase DOUBLE PRECISION"], [f"sum_increase {data_type}"],
) )
# Try to change the character set of the statistic_meta table # Try to change the character set of the statistic_meta table
if engine.dialect.name == "mysql": if engine.dialect.name == "mysql":
for table in ("events", "states", "statistics_meta"): for table in ("events", "states", "statistics_meta"):
_LOGGER.warning(
"Updating character set and collation of table %s to utf8mb4. "
"Note: this can take several minutes on large databases and slow "
"computers. Please be patient!",
table,
)
with contextlib.suppress(SQLAlchemyError): with contextlib.suppress(SQLAlchemyError):
connection.execute( connection.execute(
text( text(