Skip to content
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
13 changes: 10 additions & 3 deletions homeassistant/components/recorder/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,11 @@
TABLE_STATISTICS_RUNS = "statistics_runs"
TABLE_STATISTICS_SHORT_TERM = "statistics_short_term"

# Only add TABLE_STATE_ATTRIBUTES and TABLE_EVENT_DATA
# to the below list once we want to check for their
# instance in the sanity check.
ALL_TABLES = [
TABLE_STATES,
TABLE_STATE_ATTRIBUTES,
TABLE_EVENTS,
TABLE_EVENT_DATA,
TABLE_RECORDER_RUNS,
TABLE_SCHEMA_CHANGES,
TABLE_STATISTICS,
Expand All @@ -75,6 +74,14 @@
TABLE_STATISTICS_SHORT_TERM,
]

TABLES_TO_CHECK = [
TABLE_STATES,
TABLE_EVENTS,
TABLE_RECORDER_RUNS,
TABLE_SCHEMA_CHANGES,
]


EMPTY_JSON_OBJECT = "{}"


Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/recorder/repack.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from sqlalchemy import text

from .const import SupportedDialect
from .models import ALL_TABLES

if TYPE_CHECKING:
from . import Recorder
Expand Down Expand Up @@ -41,6 +42,6 @@ def repack_database(instance: Recorder) -> None:
if dialect_name == SupportedDialect.MYSQL:
_LOGGER.debug("Optimizing SQL DB to free space")
with instance.engine.connect() as conn:
conn.execute(text("OPTIMIZE TABLE states, events, recorder_runs"))
conn.execute(text(f"OPTIMIZE TABLE {','.join(ALL_TABLES)}"))
conn.commit()
return
16 changes: 2 additions & 14 deletions homeassistant/components/recorder/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,9 @@

from .const import DATA_INSTANCE, SQLITE_URL_PREFIX, SupportedDialect
from .models import (
ALL_TABLES,
TABLE_RECORDER_RUNS,
TABLE_SCHEMA_CHANGES,
TABLE_STATISTICS,
TABLE_STATISTICS_META,
TABLE_STATISTICS_RUNS,
TABLE_STATISTICS_SHORT_TERM,
TABLES_TO_CHECK,
RecorderRuns,
process_timestamp,
)
Expand Down Expand Up @@ -213,15 +209,7 @@ def last_run_was_recently_clean(cursor: CursorFetchStrategy) -> bool:
def basic_sanity_check(cursor: CursorFetchStrategy) -> bool:
"""Check tables to make sure select does not fail."""

for table in ALL_TABLES:
# The statistics tables may not be present in old databases
if table in [
TABLE_STATISTICS,
TABLE_STATISTICS_META,
TABLE_STATISTICS_RUNS,
TABLE_STATISTICS_SHORT_TERM,
]:
continue
for table in TABLES_TO_CHECK:
if table in (TABLE_RECORDER_RUNS, TABLE_SCHEMA_CHANGES):
cursor.execute(f"SELECT * FROM {table};") # nosec # not injection
else:
Expand Down