mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Use microsecond precision for datetime values on MariaDB/MySQL (#48749)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
parent
191c01a611
commit
815db999da
@ -363,6 +363,20 @@ def _apply_update(engine, new_version, old_version):
|
|||||||
if engine.dialect.name == "mysql":
|
if engine.dialect.name == "mysql":
|
||||||
_modify_columns(engine, "events", ["event_data LONGTEXT"])
|
_modify_columns(engine, "events", ["event_data LONGTEXT"])
|
||||||
_modify_columns(engine, "states", ["attributes LONGTEXT"])
|
_modify_columns(engine, "states", ["attributes LONGTEXT"])
|
||||||
|
elif new_version == 13:
|
||||||
|
if engine.dialect.name == "mysql":
|
||||||
|
_modify_columns(
|
||||||
|
engine, "events", ["time_fired DATETIME(6)", "created DATETIME(6)"]
|
||||||
|
)
|
||||||
|
_modify_columns(
|
||||||
|
engine,
|
||||||
|
"states",
|
||||||
|
[
|
||||||
|
"last_changed DATETIME(6)",
|
||||||
|
"last_updated DATETIME(6)",
|
||||||
|
"created DATETIME(6)",
|
||||||
|
],
|
||||||
|
)
|
||||||
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 = 12
|
SCHEMA_VERSION = 13
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -39,6 +39,10 @@ TABLE_SCHEMA_CHANGES = "schema_changes"
|
|||||||
|
|
||||||
ALL_TABLES = [TABLE_STATES, TABLE_EVENTS, TABLE_RECORDER_RUNS, TABLE_SCHEMA_CHANGES]
|
ALL_TABLES = [TABLE_STATES, TABLE_EVENTS, TABLE_RECORDER_RUNS, TABLE_SCHEMA_CHANGES]
|
||||||
|
|
||||||
|
DATETIME_TYPE = DateTime(timezone=True).with_variant(
|
||||||
|
mysql.DATETIME(timezone=True, fsp=6), "mysql"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class Events(Base): # type: ignore
|
class Events(Base): # type: ignore
|
||||||
"""Event history data."""
|
"""Event history data."""
|
||||||
@ -52,8 +56,8 @@ class Events(Base): # type: ignore
|
|||||||
event_type = Column(String(32))
|
event_type = Column(String(32))
|
||||||
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(timezone=True), index=True)
|
time_fired = Column(DATETIME_TYPE, index=True)
|
||||||
created = Column(DateTime(timezone=True), default=dt_util.utcnow)
|
created = Column(DATETIME_TYPE, default=dt_util.utcnow)
|
||||||
context_id = Column(String(36), index=True)
|
context_id = Column(String(36), index=True)
|
||||||
context_user_id = Column(String(36), index=True)
|
context_user_id = Column(String(36), index=True)
|
||||||
context_parent_id = Column(String(36), index=True)
|
context_parent_id = Column(String(36), index=True)
|
||||||
@ -123,9 +127,9 @@ class States(Base): # type: ignore
|
|||||||
event_id = Column(
|
event_id = Column(
|
||||||
Integer, ForeignKey("events.event_id", ondelete="CASCADE"), index=True
|
Integer, ForeignKey("events.event_id", ondelete="CASCADE"), index=True
|
||||||
)
|
)
|
||||||
last_changed = Column(DateTime(timezone=True), default=dt_util.utcnow)
|
last_changed = Column(DATETIME_TYPE, default=dt_util.utcnow)
|
||||||
last_updated = Column(DateTime(timezone=True), default=dt_util.utcnow, index=True)
|
last_updated = Column(DATETIME_TYPE, default=dt_util.utcnow, index=True)
|
||||||
created = Column(DateTime(timezone=True), default=dt_util.utcnow)
|
created = Column(DATETIME_TYPE, default=dt_util.utcnow)
|
||||||
old_state_id = Column(
|
old_state_id = Column(
|
||||||
Integer, ForeignKey("states.state_id", ondelete="NO ACTION"), index=True
|
Integer, ForeignKey("states.state_id", ondelete="NO ACTION"), index=True
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user