Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a093cd8

Browse files
agnersballoob
authored andcommitted
Use microsecond precision for datetime values on MariaDB/MySQL (home-assistant#48749)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
1 parent 322458e commit a093cd8

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

‎homeassistant/components/recorder/migration.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,20 @@ def _apply_update(engine, new_version, old_version):
363363
if engine.dialect.name == "mysql":
364364
_modify_columns(engine, "events", ["event_data LONGTEXT"])
365365
_modify_columns(engine, "states", ["attributes LONGTEXT"])
366+
elif new_version == 13:
367+
if engine.dialect.name == "mysql":
368+
_modify_columns(
369+
engine, "events", ["time_fired DATETIME(6)", "created DATETIME(6)"]
370+
)
371+
_modify_columns(
372+
engine,
373+
"states",
374+
[
375+
"last_changed DATETIME(6)",
376+
"last_updated DATETIME(6)",
377+
"created DATETIME(6)",
378+
],
379+
)
366380
else:
367381
raise ValueError(f"No schema migration defined for version {new_version}")
368382

‎homeassistant/components/recorder/models.py‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
# pylint: disable=invalid-name
2727
Base = declarative_base()
2828

29-
SCHEMA_VERSION = 12
29+
SCHEMA_VERSION = 13
3030

3131
_LOGGER = logging.getLogger(__name__)
3232

@@ -39,6 +39,10 @@
3939

4040
ALL_TABLES = [TABLE_STATES, TABLE_EVENTS, TABLE_RECORDER_RUNS, TABLE_SCHEMA_CHANGES]
4141

42+
DATETIME_TYPE = DateTime(timezone=True).with_variant(
43+
mysql.DATETIME(timezone=True, fsp=6), "mysql"
44+
)
45+
4246

4347
class Events(Base): # type: ignore
4448
"""Event history data."""
@@ -52,8 +56,8 @@ class Events(Base): # type: ignore
5256
event_type = Column(String(32))
5357
event_data = Column(Text().with_variant(mysql.LONGTEXT, "mysql"))
5458
origin = Column(String(32))
55-
time_fired = Column(DateTime(timezone=True), index=True)
56-
created = Column(DateTime(timezone=True), default=dt_util.utcnow)
59+
time_fired = Column(DATETIME_TYPE, index=True)
60+
created = Column(DATETIME_TYPE, default=dt_util.utcnow)
5761
context_id = Column(String(36), index=True)
5862
context_user_id = Column(String(36), index=True)
5963
context_parent_id = Column(String(36), index=True)
@@ -123,9 +127,9 @@ class States(Base): # type: ignore
123127
event_id = Column(
124128
Integer, ForeignKey("events.event_id", ondelete="CASCADE"), index=True
125129
)
126-
last_changed = Column(DateTime(timezone=True), default=dt_util.utcnow)
127-
last_updated = Column(DateTime(timezone=True), default=dt_util.utcnow, index=True)
128-
created = Column(DateTime(timezone=True), default=dt_util.utcnow)
130+
last_changed = Column(DATETIME_TYPE, default=dt_util.utcnow)
131+
last_updated = Column(DATETIME_TYPE, default=dt_util.utcnow, index=True)
132+
created = Column(DATETIME_TYPE, default=dt_util.utcnow)
129133
old_state_id = Column(
130134
Integer, ForeignKey("states.state_id", ondelete="NO ACTION"), index=True
131135
)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /