Home Assistant Unofficial Reference 2024.12.1
schema.py
Go to the documentation of this file.
1 """States schema repairs."""
2 
3 from __future__ import annotations
4 
5 from typing import TYPE_CHECKING
6 
7 from ...db_schema import StateAttributes, States
8 from ..schema import (
9  correct_db_schema_precision,
10  correct_db_schema_utf8,
11  validate_db_schema_precision,
12  validate_table_schema_has_correct_collation,
13  validate_table_schema_supports_utf8,
14 )
15 
16 if TYPE_CHECKING:
17  from ... import Recorder
18 
19 TABLE_UTF8_COLUMNS = {
20  States: (States.state,),
21  StateAttributes: (StateAttributes.shared_attrs,),
22 }
23 
24 
25 def validate_db_schema(instance: Recorder) -> set[str]:
26  """Do some basic checks for common schema errors caused by manual migration."""
27  schema_errors: set[str] = set()
28  for table, columns in TABLE_UTF8_COLUMNS.items():
29  schema_errors |= validate_table_schema_supports_utf8(instance, table, columns)
30  schema_errors |= validate_db_schema_precision(instance, States)
31  for table in (States, StateAttributes):
32  schema_errors |= validate_table_schema_has_correct_collation(instance, table)
33  return schema_errors
34 
35 
37  instance: Recorder,
38  schema_errors: set[str],
39 ) -> None:
40  """Correct issues detected by validate_db_schema."""
41  for table in (States, StateAttributes):
42  correct_db_schema_utf8(instance, table, schema_errors)
43  correct_db_schema_precision(instance, States, schema_errors)
None correct_db_schema_precision(Recorder instance, type[DeclarativeBase] table_object, set[str] schema_errors)
Definition: schema.py:256
set[str] validate_table_schema_has_correct_collation(Recorder instance, type[DeclarativeBase] table_object)
Definition: schema.py:68
None correct_db_schema_utf8(Recorder instance, type[DeclarativeBase] table_object, set[str] schema_errors)
Definition: schema.py:238
set[str] validate_db_schema_precision(Recorder instance, type[DeclarativeBase] table_object)
Definition: schema.py:150
set[str] validate_table_schema_supports_utf8(Recorder instance, type[DeclarativeBase] table_object, tuple[InstrumentedAttribute,...] columns)
Definition: schema.py:47
None correct_db_schema(Recorder instance, set[str] schema_errors)
Definition: schema.py:39