From 457fc0c7c198cf91f7bf7312749ad289b47653d2 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 19 Aug 2018 18:07:15 +0200 Subject: [PATCH 1/3] Add a file if migration in progress --- .../components/recorder/migration.py | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/recorder/migration.py b/homeassistant/components/recorder/migration.py index 5633830dc518cd..13bad55aca99a1 100644 --- a/homeassistant/components/recorder/migration.py +++ b/homeassistant/components/recorder/migration.py @@ -1,23 +1,33 @@ """Schema migration helpers.""" import logging +import os 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.""" from .models import SchemaChanges, SCHEMA_VERSION + progress_path = instance.hass.config.path(PROGRESS_FILE) + with session_scope(session=instance.get_session()) as session: res = session.query(SchemaChanges).order_by( SchemaChanges.change_id.desc()).first() current_version = getattr(res, 'schema_version', None) if current_version == SCHEMA_VERSION: + # Clean up if old migration left file + if os.path.isfile(progress_path): + os.remove(instance.hass.config.path(PROGRESS_FILE)) return + with open(progress_path, 'w'): + pass + _LOGGER.debug("Database requires upgrade. Schema version: %s", current_version) @@ -26,14 +36,17 @@ def migrate_schema(instance): _LOGGER.debug("No schema version found. Inspected version: %s", current_version) - for version in range(current_version, SCHEMA_VERSION): - new_version = version + 1 - _LOGGER.info("Upgrading recorder db schema to version %s", - new_version) - _apply_update(instance.engine, new_version, current_version) - session.add(SchemaChanges(schema_version=new_version)) + try: + for version in range(current_version, SCHEMA_VERSION): + new_version = version + 1 + _LOGGER.info("Upgrading recorder db schema to version %s", + new_version) + _apply_update(instance.engine, new_version, current_version) + session.add(SchemaChanges(schema_version=new_version)) - _LOGGER.info("Upgrade to version %s done", 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): @@ -125,7 +138,7 @@ def _add_columns(engine, table_name, columns_def): ', '.join(column.split(' ')[0] for column in columns_def), table_name) - columns_def = ['ADD COLUMN {}'.format(col_def) for col_def in columns_def] + columns_def = ['ADD {}'.format(col_def) for col_def in columns_def] try: engine.execute(text("ALTER TABLE {table} {columns_def}".format( @@ -147,7 +160,7 @@ def _add_columns(engine, table_name, columns_def): raise _LOGGER.warning('Column %s already exists on %s, continueing', - column_def.split(' ')[0], table_name) + column_def.split(' ')[1], table_name) def _apply_update(engine, new_version, old_version): From f5805f92e93fe9dafd9e264cd09a8fafc7392ae2 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 19 Aug 2018 18:28:23 +0200 Subject: [PATCH 2/3] Warning --- homeassistant/components/recorder/migration.py | 1 + 1 file changed, 1 insertion(+) diff --git a/homeassistant/components/recorder/migration.py b/homeassistant/components/recorder/migration.py index 13bad55aca99a1..1f63457b3cab8b 100644 --- a/homeassistant/components/recorder/migration.py +++ b/homeassistant/components/recorder/migration.py @@ -22,6 +22,7 @@ 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 From 9a6714a2dc1b02884f4b8eb9f5c6f43f6932b344 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 19 Aug 2018 18:55:53 +0200 Subject: [PATCH 3/3] Convert message for migration to warning --- homeassistant/components/recorder/migration.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/recorder/migration.py b/homeassistant/components/recorder/migration.py index 1f63457b3cab8b..0dff21a5986a4e 100644 --- a/homeassistant/components/recorder/migration.py +++ b/homeassistant/components/recorder/migration.py @@ -29,8 +29,8 @@ def migrate_schema(instance): with open(progress_path, 'w'): pass - _LOGGER.debug("Database requires upgrade. Schema version: %s", - current_version) + _LOGGER.warning("Database requires upgrade. Schema version: %s", + current_version) if current_version is None: current_version = _inspect_schema_version(instance.engine, session)