Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(migrations): run every migration in one transaction

Josh Corbett374e2f6666a3
diff --git a/invokeai/app/services/shared/sqlite_migrator/sqlite_migrator_impl.py b/invokeai/app/services/shared/sqlite_migrator/sqlite_migrator_impl.py
index 6842813a8e..27c0921c73 100644
--- a/invokeai/app/services/shared/sqlite_migrator/sqlite_migrator_impl.py
+++ b/invokeai/app/services/shared/sqlite_migrator/sqlite_migrator_impl.py
@@ -96,6 +96,17 @@ class SqliteMigrator:
# exception is raised.
with self._db._conn as conn:
cursor = conn.cursor()
+ # Begun explicitly, because the context manager above only commits or rolls back a
+ # transaction that is already open — it does not start one. The connection runs in
+ # Python's legacy implicit-transaction mode, which opens a transaction before DML
+ # and never before DDL, so a callback that issues nothing but `CREATE`/`DROP`/
+ # `ALTER` would commit statement by statement in autocommit with nothing for the
+ # rollback to undo. A migration that dropped a table and died before recreating it
+ # would leave the database wedged and the migration unrecorded, so every restart
+ # would re-run it and fail again. Beginning here makes atomicity a property of the
+ # migrator rather than an accident of whether a given migration happens to write a
+ # row.
+ cursor.execute("BEGIN;")
self._create_applied_migrations_table(cursor)
if migration.from_version is not None and self._get_current_version(cursor) != migration.from_version:
raise MigrationError(
diff --git a/tests/test_sqlite_migrator.py b/tests/test_sqlite_migrator.py
index d99f2b7dcf..a737689ce0 100644
--- a/tests/test_sqlite_migrator.py
+++ b/tests/test_sqlite_migrator.py
@@ -582,6 +582,61 @@ def test_migrator_makes_no_changes_on_failed_migration(
assert [row[0] for row in cursor.fetchall()] == ["migration_1"]
+def test_migrator_rolls_back_ddl_a_failed_migration_already_issued(
+ migrator: SqliteMigrator, migration_no_op: Migration
+) -> None:
+ """A migration that only ever issues DDL must still be all-or-nothing.
+
+ Python's sqlite3 opens an implicit transaction before DML and never before DDL, so without an
+ explicit `BEGIN` every `CREATE`/`DROP`/`ALTER` here would commit on its own and survive the
+ failure. That is not a theoretical tidiness point: a table rebuild that drops the original and
+ dies before renaming the replacement leaves the database with no such table *and* the migration
+ unrecorded, so every subsequent start re-runs it and fails again.
+ """
+
+ def migrate(cursor: sqlite3.Cursor, **kwargs) -> None:
+ cursor.execute("CREATE TABLE ddl_only (id INTEGER PRIMARY KEY);")
+ cursor.execute("DROP TABLE ddl_only;")
+ cursor.execute("CREATE TABLE ddl_only_replacement (id INTEGER PRIMARY KEY);")
+ raise Exception("Bad migration")
+
+ cursor = migrator._db._conn.cursor()
+ migrator.register_migration(migration_no_op)
+ migrator.run_migrations()
+ migrator.register_migration(Migration(from_version=1, to_version=2, callback=migrate))
+
+ with pytest.raises(MigrationError, match="Bad migration"):
+ migrator.run_migrations()
+
+ cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='ddl_only_replacement';")
+ assert cursor.fetchone() is None
+ assert migrator._get_current_version(cursor) == 1
+ cursor.execute("SELECT migration_id FROM applied_migrations ORDER BY migration_id;")
+ assert [row[0] for row in cursor.fetchall()] == ["migration_1"]
+
+
+def test_migrator_rolls_back_a_failed_migration_that_mixed_ddl_and_dml(
+ migrator: SqliteMigrator, migration_no_op: Migration
+) -> None:
+ """The same guarantee where the DDL precedes the first row written."""
+
+ def migrate(cursor: sqlite3.Cursor, **kwargs) -> None:
+ cursor.execute("CREATE TABLE mixed (id INTEGER PRIMARY KEY);")
+ cursor.execute("INSERT INTO mixed (id) VALUES (1);")
+ raise Exception("Bad migration")
+
+ cursor = migrator._db._conn.cursor()
+ migrator.register_migration(migration_no_op)
+ migrator.run_migrations()
+ migrator.register_migration(Migration(from_version=1, to_version=2, callback=migrate))
+
+ with pytest.raises(MigrationError, match="Bad migration"):
+ migrator.run_migrations()
+
+ cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='mixed';")
+ assert cursor.fetchone() is None
+
+
def test_idempotent_migrations(migrator: SqliteMigrator, migration_create_test_table: Migration) -> None:
cursor = migrator._db._conn.cursor()
migrator.register_migration(migration_create_test_table)