Skip to content

Commit

Permalink
Ensure all tables have the default table args in the db_schema (#114895)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored and frenck committed Apr 5, 2024
1 parent 71877fd commit 87ffd5a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 11 additions & 1 deletion homeassistant/components/recorder/db_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,7 @@ class Statistics(Base, StatisticsBase):
"start_ts",
unique=True,
),
_DEFAULT_TABLE_ARGS,
)
__tablename__ = TABLE_STATISTICS

Expand All @@ -732,6 +733,7 @@ class StatisticsShortTerm(Base, StatisticsBase):
"start_ts",
unique=True,
),
_DEFAULT_TABLE_ARGS,
)
__tablename__ = TABLE_STATISTICS_SHORT_TERM

Expand Down Expand Up @@ -760,7 +762,10 @@ def from_meta(meta: StatisticMetaData) -> StatisticsMeta:
class RecorderRuns(Base):
"""Representation of recorder run."""

__table_args__ = (Index("ix_recorder_runs_start_end", "start", "end"),)
__table_args__ = (
Index("ix_recorder_runs_start_end", "start", "end"),
_DEFAULT_TABLE_ARGS,
)
__tablename__ = TABLE_RECORDER_RUNS
run_id: Mapped[int] = mapped_column(Integer, Identity(), primary_key=True)
start: Mapped[datetime] = mapped_column(DATETIME_TYPE, default=dt_util.utcnow)
Expand Down Expand Up @@ -789,6 +794,7 @@ class MigrationChanges(Base):
"""Representation of migration changes."""

__tablename__ = TABLE_MIGRATION_CHANGES
__table_args__ = (_DEFAULT_TABLE_ARGS,)

migration_id: Mapped[str] = mapped_column(String(255), primary_key=True)
version: Mapped[int] = mapped_column(SmallInteger)
Expand All @@ -798,6 +804,8 @@ class SchemaChanges(Base):
"""Representation of schema version changes."""

__tablename__ = TABLE_SCHEMA_CHANGES
__table_args__ = (_DEFAULT_TABLE_ARGS,)

change_id: Mapped[int] = mapped_column(Integer, Identity(), primary_key=True)
schema_version: Mapped[int | None] = mapped_column(Integer)
changed: Mapped[datetime] = mapped_column(DATETIME_TYPE, default=dt_util.utcnow)
Expand All @@ -816,6 +824,8 @@ class StatisticsRuns(Base):
"""Representation of statistics run."""

__tablename__ = TABLE_STATISTICS_RUNS
__table_args__ = (_DEFAULT_TABLE_ARGS,)

run_id: Mapped[int] = mapped_column(Integer, Identity(), primary_key=True)
start: Mapped[datetime] = mapped_column(DATETIME_TYPE, index=True)

Expand Down
7 changes: 7 additions & 0 deletions tests/components/recorder/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
DOMAIN,
SQLITE_URL_PREFIX,
Recorder,
db_schema,
get_instance,
migration,
pool,
Expand Down Expand Up @@ -2598,3 +2599,9 @@ def run(self, instance: Recorder) -> None:

await verify_states_in_queue_future
await verify_session_commit_future


def test_all_tables_use_default_table_args(hass: HomeAssistant) -> None:
"""Test that all tables use the default table args."""
for table in db_schema.Base.metadata.tables.values():
assert table.kwargs.items() >= db_schema._DEFAULT_TABLE_ARGS.items()

0 comments on commit 87ffd5a

Please sign in to comment.