feat(db): only reinit db if migrations occurred

This commit is contained in:
psychedelicious 2023-12-11 01:09:41 +11:00
parent e46dc9b34e
commit 56966d6d05
2 changed files with 6 additions and 7 deletions

View File

@ -87,9 +87,9 @@ class ApiDependencies:
migration_2.register_post_callback(partial(migrate_embedded_workflows, logger=logger, image_files=image_files))
migrator.register_migration(migration_1)
migrator.register_migration(migration_2)
migrator.run_migrations()
did_migrate = migrator.run_migrations()
if not db.is_memory:
if not db.is_memory and did_migrate:
db.reinitialize()
configuration = config

View File

@ -168,7 +168,7 @@ class SQLiteMigrator:
self._migrations.register(migration)
self._logger.debug(f"Registered migration {migration.from_version} -> {migration.to_version}")
def run_migrations(self) -> None:
def run_migrations(self) -> bool:
"""Migrates the database to the latest version."""
with self._lock:
# This throws if there is a problem.
@ -177,11 +177,11 @@ class SQLiteMigrator:
if self._migrations.count == 0:
self._logger.debug("No migrations registered")
return
return False
if self._get_current_version(self._cursor) == self._migrations.latest_version:
self._logger.debug("Database is up to date, no migrations to run")
return
return False
self._logger.info("Database update needed")
@ -204,10 +204,9 @@ class SQLiteMigrator:
else:
# We are using a memory database. No special backup or special handling needed.
self._run_migrations(self._cursor)
return
self._logger.info("Database updated successfully")
return
return True
def _run_migrations(self, temp_db_cursor: sqlite3.Cursor) -> None:
next_migration = self._migrations.get(from_version=self._get_current_version(temp_db_cursor))