mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Let recorder deal with event names longer than 32 chars (#47748)
This commit is contained in:
parent
1dafea705d
commit
c7e4857d2c
@ -206,6 +206,16 @@ def _add_columns(engine, table_name, columns_def):
|
|||||||
|
|
||||||
def _modify_columns(engine, table_name, columns_def):
|
def _modify_columns(engine, table_name, columns_def):
|
||||||
"""Modify columns in a table."""
|
"""Modify columns in a table."""
|
||||||
|
if engine.dialect.name == "sqlite":
|
||||||
|
_LOGGER.debug(
|
||||||
|
"Skipping to modify columns %s in table %s; "
|
||||||
|
"Modifying column length in SQLite is unnecessary, "
|
||||||
|
"it does not impose any length restrictions",
|
||||||
|
", ".join(column.split(" ")[0] for column in columns_def),
|
||||||
|
table_name,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"Modifying columns %s in table %s. Note: this can take several "
|
"Modifying columns %s in table %s. Note: this can take several "
|
||||||
"minutes on large databases and slow computers. Please "
|
"minutes on large databases and slow computers. Please "
|
||||||
@ -213,7 +223,18 @@ def _modify_columns(engine, table_name, columns_def):
|
|||||||
", ".join(column.split(" ")[0] for column in columns_def),
|
", ".join(column.split(" ")[0] for column in columns_def),
|
||||||
table_name,
|
table_name,
|
||||||
)
|
)
|
||||||
columns_def = [f"MODIFY {col_def}" for col_def in columns_def]
|
|
||||||
|
if engine.dialect.name == "postgresql":
|
||||||
|
columns_def = [
|
||||||
|
"ALTER {column} TYPE {type}".format(
|
||||||
|
**dict(zip(["column", "type"], col_def.split(" ", 1)))
|
||||||
|
)
|
||||||
|
for col_def in columns_def
|
||||||
|
]
|
||||||
|
elif engine.dialect.name == "mssql":
|
||||||
|
columns_def = [f"ALTER COLUMN {col_def}" for col_def in columns_def]
|
||||||
|
else:
|
||||||
|
columns_def = [f"MODIFY {col_def}" for col_def in columns_def]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
engine.execute(
|
engine.execute(
|
||||||
@ -377,6 +398,8 @@ def _apply_update(engine, new_version, old_version):
|
|||||||
"created DATETIME(6)",
|
"created DATETIME(6)",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
elif new_version == 14:
|
||||||
|
_modify_columns(engine, "events", ["event_type VARCHAR(64)"])
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"No schema migration defined for version {new_version}")
|
raise ValueError(f"No schema migration defined for version {new_version}")
|
||||||
|
|
||||||
|
@ -26,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 = 13
|
SCHEMA_VERSION = 14
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -53,7 +53,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(64))
|
||||||
event_data = Column(Text().with_variant(mysql.LONGTEXT, "mysql"))
|
event_data = Column(Text().with_variant(mysql.LONGTEXT, "mysql"))
|
||||||
origin = Column(String(32))
|
origin = Column(String(32))
|
||||||
time_fired = Column(DATETIME_TYPE, index=True)
|
time_fired = Column(DATETIME_TYPE, index=True)
|
||||||
|
@ -76,6 +76,26 @@ def test_invalid_update():
|
|||||||
migration._apply_update(None, -1, 0)
|
migration._apply_update(None, -1, 0)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
["engine_type", "substr"],
|
||||||
|
[
|
||||||
|
("postgresql", "ALTER event_type TYPE VARCHAR(64)"),
|
||||||
|
("mssql", "ALTER COLUMN event_type VARCHAR(64)"),
|
||||||
|
("mysql", "MODIFY event_type VARCHAR(64)"),
|
||||||
|
("sqlite", None),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_modify_column(engine_type, substr):
|
||||||
|
"""Test that modify column generates the expected query."""
|
||||||
|
engine = Mock()
|
||||||
|
engine.dialect.name = engine_type
|
||||||
|
migration._modify_columns(engine, "events", ["event_type VARCHAR(64)"])
|
||||||
|
if substr:
|
||||||
|
assert substr in engine.execute.call_args[0][0].text
|
||||||
|
else:
|
||||||
|
assert not engine.execute.called
|
||||||
|
|
||||||
|
|
||||||
def test_forgiving_add_column():
|
def test_forgiving_add_column():
|
||||||
"""Test that add column will continue if column exists."""
|
"""Test that add column will continue if column exists."""
|
||||||
engine = create_engine("sqlite://", poolclass=StaticPool)
|
engine = create_engine("sqlite://", poolclass=StaticPool)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user