Intelligent timeout handler for setup/bootstrap (#38329)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com> Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
21 files changed
+901
-89
No files matched your search
@@ -35,14 +35,12 @@ from homeassistant.helpers.typing import ConfigType
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from . import migration, purge
|
||||
from .const import DATA_INSTANCE, SQLITE_URL_PREFIX
|
||||
from .const import DATA_INSTANCE, DOMAIN, SQLITE_URL_PREFIX
|
||||
from .models import Base, Events, RecorderRuns, States
|
||||
from .util import session_scope, validate_or_move_away_sqlite_database
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DOMAIN = "recorder"
|
||||
|
||||
SERVICE_PURGE = "purge"
|
||||
|
||||
ATTR_KEEP_DAYS = "keep_days"
|
||||
|
||||
@@ -2,3 +2,4 @@
|
||||
|
||||
DATA_INSTANCE = "recorder_instance"
|
||||
SQLITE_URL_PREFIX = "sqlite://"
|
||||
DOMAIN = "recorder"
|
||||
@@ -1,22 +1,19 @@
|
||||
"""Schema migration helpers."""
|
||||
import logging
|
||||
import os
|
||||
|
||||
from sqlalchemy import Table, text
|
||||
from sqlalchemy.engine import reflection
|
||||
from sqlalchemy.exc import InternalError, OperationalError, SQLAlchemyError
|
||||
|
||||
from .const import DOMAIN
|
||||
from .models import SCHEMA_VERSION, Base, SchemaChanges
|
||||
from .util import session_scope
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
PROGRESS_FILE = ".migration_progress"
|
||||
|
||||
|
||||
def migrate_schema(instance):
|
||||
"""Check if the schema needs to be upgraded."""
|
||||
progress_path = instance.hass.config.path(PROGRESS_FILE)
|
||||
|
||||
with session_scope(session=instance.get_session()) as session:
|
||||
res = (
|
||||
session.query(SchemaChanges)
|
||||
@@ -32,20 +29,13 @@ def migrate_schema(instance):
|
||||
)
|
||||
|
||||
if current_version == SCHEMA_VERSION:
|
||||
# Clean up if old migration left file
|
||||
if os.path.isfile(progress_path):
|
||||
_LOGGER.warning("Found existing migration file, cleaning up")
|
||||
os.remove(instance.hass.config.path(PROGRESS_FILE))
|
||||
return
|
||||
|
||||
with open(progress_path, "w"):
|
||||
pass
|
||||
|
||||
_LOGGER.warning(
|
||||
"Database is about to upgrade. Schema version: %s", current_version
|
||||
)
|
||||
|
||||
try:
|
||||
with instance.hass.timeout.freeze(DOMAIN):
|
||||
for version in range(current_version, SCHEMA_VERSION):
|
||||
new_version = version + 1
|
||||
_LOGGER.info("Upgrading recorder db schema to version %s", new_version)
|
||||
@@ -53,8 +43,6 @@ def migrate_schema(instance):
|
||||
session.add(SchemaChanges(schema_version=new_version))
|
||||
|
||||
_LOGGER.info("Upgrade to version %s done", new_version)
|
||||
finally:
|
||||
os.remove(instance.hass.config.path(PROGRESS_FILE))
|
||||
|
||||
|
||||
def _create_index(engine, table_name, index_name):
|
||||
|
||||
Reference in new issue
Block a user