Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure all tables have the default table args in the db_schema #114895

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()