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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(db): repair the projects table on upstream-originated databases
joshistoaste4135589fc4a
diff --git a/invokeai/app/services/shared/sqlite_migrator/migrations/migration_2026_07_30_repair_projects_table.py b/invokeai/app/services/shared/sqlite_migrator/migrations/migration_2026_07_30_repair_projects_table.pynew file mode 100644index 0000000000..bd101fd31f--- /dev/null+++ b/invokeai/app/services/shared/sqlite_migrator/migrations/migration_2026_07_30_repair_projects_table.py@@ -0,0 +1,74 @@+"""Repair the `projects` table on databases that skipped this fork's migration_33.++Numeric migrations are keyed by `migration_<to_version>`, and a database records the ids it has+applied. This fork's `migration_33` creates `projects`; upstream's `migration_33` creates the+image-subfolder move tables (which this fork carries as `migration_34`). The two are different+migrations sharing one id.++So a database that was ever opened by an upstream build has `migration_33` recorded, and this+fork then treats its own `projects` migration as already applied and skips it forever.+`migration_34` is no help — its DDL is all `IF NOT EXISTS`, so it quietly no-ops. The user is+left with a working install whose `projects` queries fail with `no such table: projects`.++This migration has a dated id, so it is unaffected by that collision: it runs exactly once on+every database, repairs the ones that are missing the table, and no-ops on the ones that are+not. The DDL below is a verbatim copy of `migration_33`'s, with `IF NOT EXISTS` added — keep the+two in sync if the schema ever changes.++Note for future migrations: do not add more numeric migrations to this fork. Every new migration+should use a dated id, which cannot collide with an upstream migration number.+"""++import sqlite3++from invokeai.app.services.shared.sqlite_migrator.sqlite_migrator_common import Migration+++class RepairProjectsTableMigrationCallback:+ def __call__(self, cursor: sqlite3.Cursor) -> None:+ self._create_projects_table(cursor)++ def _create_projects_table(self, cursor: sqlite3.Cursor) -> None:+ cursor.execute(+ """--sql+ CREATE TABLE IF NOT EXISTS projects (+ -- Client-generated identifier; unique per user, not globally.+ project_id TEXT NOT NULL,+ user_id TEXT NOT NULL,+ name TEXT NOT NULL,+ -- Opaque client-owned project document (JSON).+ data TEXT NOT NULL,+ -- Incremented on every update; used for optimistic concurrency.+ revision INTEGER NOT NULL DEFAULT 1,+ created_at DATETIME NOT NULL DEFAULT(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')),+ updated_at DATETIME NOT NULL DEFAULT(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')),+ PRIMARY KEY (user_id, project_id),+ FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE+ );+ """+ )+ cursor.execute(+ """--sql+ CREATE TRIGGER IF NOT EXISTS tg_projects_updated_at+ AFTER UPDATE ON projects+ FOR EACH ROW+ BEGIN+ UPDATE projects+ SET updated_at = STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')+ WHERE user_id = OLD.user_id AND project_id = OLD.project_id;+ END;+ """+ )+++def build_migration() -> Migration:+ """Build the migration that repairs a missing `projects` table."""+ return Migration(+ id="2026_07_30_repair_projects_table",+ # migration_33 is where `projects` should have been created. Depending on it is correct in+ # both cases: on a fork database it genuinely ran, and on an upstream-originated database+ # its id is recorded (by upstream's unrelated migration), so the dependency still resolves.+ # It also transitively guarantees migration_27's `users` table, which the FK references.+ depends_on="migration_33",+ callback=RepairProjectsTableMigrationCallback(),+ )diff --git a/tests/app/services/shared/sqlite_migrator/migrations/test_migration_2026_07_30_repair_projects_table.py b/tests/app/services/shared/sqlite_migrator/migrations/test_migration_2026_07_30_repair_projects_table.pynew file mode 100644index 0000000000..55ad9b2868--- /dev/null+++ b/tests/app/services/shared/sqlite_migrator/migrations/test_migration_2026_07_30_repair_projects_table.py@@ -0,0 +1,102 @@+import sqlite3+from logging import Logger+from pathlib import Path+from unittest.mock import MagicMock++from invokeai.app.services.shared.sqlite.sqlite_database import SqliteDatabase+from invokeai.app.services.shared.sqlite_migrator.migration_loader import MigrationBuildContext, build_migrations+from invokeai.app.services.shared.sqlite_migrator.migrations.migration_34 import build_migration_34+from invokeai.app.services.shared.sqlite_migrator.migrations.migration_2026_07_30_repair_projects_table import (+ RepairProjectsTableMigrationCallback,+ build_migration,+)+from invokeai.app.services.shared.sqlite_migrator.sqlite_migrator_impl import SqliteMigrator+++def _table_exists(cursor: sqlite3.Cursor, name: str) -> bool:+ cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name=?;", (name,))+ return cursor.fetchone() is not None+++def test_creates_the_projects_table_and_its_trigger() -> None:+ db = sqlite3.connect(":memory:")+ db.execute("PRAGMA foreign_keys = ON;")+ # Created by migration_27, which the projects FK references.+ db.execute("CREATE TABLE users (user_id TEXT NOT NULL PRIMARY KEY);")++ RepairProjectsTableMigrationCallback()(db.cursor())++ db.execute("INSERT INTO users VALUES ('u1');")+ db.execute("INSERT INTO projects (project_id, user_id, name, data) VALUES ('p1', 'u1', 'n', '{}');")+ cursor = db.cursor()+ cursor.execute("SELECT name FROM sqlite_master WHERE type='trigger' AND name='tg_projects_updated_at';")+ assert cursor.fetchone() is not None+ db.close()+++def test_is_idempotent_on_a_database_that_already_has_the_table() -> None:+ """The common case: a fork-originated database where migration_33 already ran. The repair must+ no-op rather than fail on an existing table/trigger, and must not disturb existing rows."""+ db = sqlite3.connect(":memory:")+ db.execute("PRAGMA foreign_keys = ON;")+ db.execute("CREATE TABLE users (user_id TEXT NOT NULL PRIMARY KEY);")+ RepairProjectsTableMigrationCallback()(db.cursor())+ db.execute("INSERT INTO users VALUES ('u1');")+ db.execute("INSERT INTO projects (project_id, user_id, name, data) VALUES ('p1', 'u1', 'kept', '{}');")++ RepairProjectsTableMigrationCallback()(db.cursor())++ cursor = db.cursor()+ cursor.execute("SELECT name FROM projects;")+ assert cursor.fetchall() == [("kept",)]+ db.close()+++def test_repairs_a_database_that_came_from_an_upstream_build(tmp_path: Path) -> None:+ """End-to-end regression guard for the migration_33 id collision.++ This fork's migration_33 creates `projects`; upstream's migration_33 creates the+ image-subfolder move tables (carried here as migration_34). They share one id, so a database+ that was ever opened by an upstream build records `migration_33` and this fork then skips its+ own projects migration permanently, leaving `no such table: projects`.++ The dated repair migration is immune to that collision and must fix such a database.+ """+ logger = Logger("test")+ context = MigrationBuildContext(app_config=MagicMock(), logger=logger, image_files=MagicMock())+ all_migrations = build_migrations(context)++ db = SqliteDatabase(db_path=tmp_path / "upstream.db", logger=logger, verbose=False)++ # 1. Bring the database to legacy version 32 using only the migrations at or below 32.+ to_v32 = SqliteMigrator(db=db)+ for migration in all_migrations:+ if migration.to_version is not None and migration.to_version <= 32:+ to_v32.register_migration(migration)+ to_v32.run_migrations()++ # 2. Simulate UPSTREAM's migration_33 having run. Its body is what this fork carries as+ # migration_34, so run that and stamp the legacy version/id upstream would have recorded.+ cursor = db._conn.cursor()+ build_migration_34().callback(cursor)+ cursor.execute("INSERT INTO migrations (version) VALUES (33);")+ cursor.execute("INSERT INTO applied_migrations (migration_id, legacy_version) VALUES ('migration_33', 33);")+ db._conn.commit()+ assert not _table_exists(cursor, "projects")++ # 3. Open it with the full fork migrator, as the app does on startup.+ full = SqliteMigrator(db=db)+ for migration in all_migrations:+ full.register_migration(migration)+ full.run_migrations()++ # Without the repair migration the fork's migration_33 stays skipped and this is still missing.+ assert _table_exists(cursor, "projects")+ cursor.execute("SELECT name FROM sqlite_master WHERE type='trigger' AND name='tg_projects_updated_at';")+ assert cursor.fetchone() is not None+++def test_migration_has_a_dated_id_so_it_cannot_collide_with_an_upstream_number() -> None:+ migration = build_migration()+ assert migration.id == "2026_07_30_repair_projects_table"+ assert migration.to_version is None