mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
Use LONGTEXT column instead of TEXT for MySQL/MariaDB and migrate existing databases (#47026)
This commit is contained in:
parent
2103335323
commit
7c8851264f
@ -204,6 +204,44 @@ def _add_columns(engine, table_name, columns_def):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _modify_columns(engine, table_name, columns_def):
|
||||||
|
"""Modify columns in a table."""
|
||||||
|
_LOGGER.warning(
|
||||||
|
"Modifying columns %s in table %s. Note: this can take several "
|
||||||
|
"minutes on large databases and slow computers. Please "
|
||||||
|
"be patient!",
|
||||||
|
", ".join(column.split(" ")[0] for column in columns_def),
|
||||||
|
table_name,
|
||||||
|
)
|
||||||
|
columns_def = [f"MODIFY {col_def}" for col_def in columns_def]
|
||||||
|
|
||||||
|
try:
|
||||||
|
engine.execute(
|
||||||
|
text(
|
||||||
|
"ALTER TABLE {table} {columns_def}".format(
|
||||||
|
table=table_name, columns_def=", ".join(columns_def)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return
|
||||||
|
except (InternalError, OperationalError):
|
||||||
|
_LOGGER.info("Unable to use quick column modify. Modifying 1 by 1")
|
||||||
|
|
||||||
|
for column_def in columns_def:
|
||||||
|
try:
|
||||||
|
engine.execute(
|
||||||
|
text(
|
||||||
|
"ALTER TABLE {table} {column_def}".format(
|
||||||
|
table=table_name, column_def=column_def
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
except (InternalError, OperationalError):
|
||||||
|
_LOGGER.exception(
|
||||||
|
"Could not modify column %s in table %s", column_def, table_name
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def _update_states_table_with_foreign_key_options(engine):
|
def _update_states_table_with_foreign_key_options(engine):
|
||||||
"""Add the options to foreign key constraints."""
|
"""Add the options to foreign key constraints."""
|
||||||
inspector = reflection.Inspector.from_engine(engine)
|
inspector = reflection.Inspector.from_engine(engine)
|
||||||
@ -321,6 +359,10 @@ def _apply_update(engine, new_version, old_version):
|
|||||||
elif new_version == 11:
|
elif new_version == 11:
|
||||||
_create_index(engine, "states", "ix_states_old_state_id")
|
_create_index(engine, "states", "ix_states_old_state_id")
|
||||||
_update_states_table_with_foreign_key_options(engine)
|
_update_states_table_with_foreign_key_options(engine)
|
||||||
|
elif new_version == 12:
|
||||||
|
if engine.dialect.name == "mysql":
|
||||||
|
_modify_columns(engine, "events", ["event_data LONGTEXT"])
|
||||||
|
_modify_columns(engine, "states", ["attributes LONGTEXT"])
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"No schema migration defined for version {new_version}")
|
raise ValueError(f"No schema migration defined for version {new_version}")
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@ from sqlalchemy import (
|
|||||||
Text,
|
Text,
|
||||||
distinct,
|
distinct,
|
||||||
)
|
)
|
||||||
|
from sqlalchemy.dialects import mysql
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
from sqlalchemy.orm import relationship
|
from sqlalchemy.orm import relationship
|
||||||
from sqlalchemy.orm.session import Session
|
from sqlalchemy.orm.session import Session
|
||||||
@ -25,7 +26,7 @@ import homeassistant.util.dt as dt_util
|
|||||||
# pylint: disable=invalid-name
|
# pylint: disable=invalid-name
|
||||||
Base = declarative_base()
|
Base = declarative_base()
|
||||||
|
|
||||||
SCHEMA_VERSION = 11
|
SCHEMA_VERSION = 12
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -49,7 +50,7 @@ class Events(Base): # type: ignore
|
|||||||
__tablename__ = TABLE_EVENTS
|
__tablename__ = TABLE_EVENTS
|
||||||
event_id = Column(Integer, primary_key=True)
|
event_id = Column(Integer, primary_key=True)
|
||||||
event_type = Column(String(32))
|
event_type = Column(String(32))
|
||||||
event_data = Column(Text)
|
event_data = Column(Text().with_variant(mysql.LONGTEXT, "mysql"))
|
||||||
origin = Column(String(32))
|
origin = Column(String(32))
|
||||||
time_fired = Column(DateTime(timezone=True), index=True)
|
time_fired = Column(DateTime(timezone=True), index=True)
|
||||||
created = Column(DateTime(timezone=True), default=dt_util.utcnow)
|
created = Column(DateTime(timezone=True), default=dt_util.utcnow)
|
||||||
@ -109,7 +110,7 @@ class States(Base): # type: ignore
|
|||||||
domain = Column(String(64))
|
domain = Column(String(64))
|
||||||
entity_id = Column(String(255))
|
entity_id = Column(String(255))
|
||||||
state = Column(String(255))
|
state = Column(String(255))
|
||||||
attributes = Column(Text)
|
attributes = Column(Text().with_variant(mysql.LONGTEXT, "mysql"))
|
||||||
event_id = Column(
|
event_id = Column(
|
||||||
Integer, ForeignKey("events.event_id", ondelete="CASCADE"), index=True
|
Integer, ForeignKey("events.event_id", ondelete="CASCADE"), index=True
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user