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(migrations): quarantine orphaned projects
Josh Corbett62e6d2fa776d
diff --git a/invokeai/app/services/shared/sqlite_migrator/migrations/migration_2026_08_06_add_project_boards.py b/invokeai/app/services/shared/sqlite_migrator/migrations/migration_2026_08_06_add_project_boards.pyindex b7a9c6c08e..929407df02 100644--- a/invokeai/app/services/shared/sqlite_migrator/migrations/migration_2026_08_06_add_project_boards.py+++ b/invokeai/app/services/shared/sqlite_migrator/migrations/migration_2026_08_06_add_project_boards.py@@ -21,8 +21,9 @@ conservative direction: a project that adopts nothing merely starts with an emptproject that adopts the wrong board takes ownership of media it does not own.Nothing is moved between boards. `board_images` and `board_videos` are read but never written, so no-image or video changes hands here; only the projects table gains a column, and boards may gain rows-and a new name.+image or video changes hands here. The projects table gains a column, boards may gain rows and a new+name, and an FK-free quarantine table is created only when a legacy project refers to a user row+that no longer exists."""import json@@ -47,18 +48,20 @@ class AddProjectBoardsMigrationCallback:self._logger = loggerdef __call__(self, cursor: sqlite3.Cursor) -> None:- self._drop_orphaned_projects(cursor)+ self._quarantine_orphaned_projects(cursor)assignments = self._assign_boards(cursor)self._rebuild_projects_table(cursor, assignments)- def _drop_orphaned_projects(self, cursor: sqlite3.Cursor) -> None:- """Remove project rows whose owner no longer exists.+ def _quarantine_orphaned_projects(self, cursor: sqlite3.Cursor) -> None:+ """Preserve project rows whose owner no longer exists outside the live table.The rebuilt table re-inserts every row under `FOREIGN KEY (user_id) REFERENCES users`, with`PRAGMA foreign_keys = ON` and no way to turn it off inside the migrator's transaction. Theold table tolerated an orphan; the new one aborts on it, which would take the wholemigration — and with it the app's ability to open the database — over a row nothing can- reach. They are unreachable already: every project query filters by `user_id`.+ reach. They cannot remain live, but deleting their documents would make repair impossible.+ A deliberately FK-free quarantine keeps the complete original row for an operator to+ inspect or restore after repairing the missing account."""cursor.execute("""--sql@@ -71,9 +74,35 @@ class AddProjectBoardsMigrationCallback:if not orphans:return+ cursor.execute(+ """--sql+ CREATE TABLE IF NOT EXISTS orphaned_projects_2026_08_06 (+ project_id TEXT NOT NULL,+ user_id TEXT NOT NULL,+ name TEXT NOT NULL,+ data TEXT NOT NULL,+ revision INTEGER NOT NULL,+ created_at DATETIME NOT NULL,+ updated_at DATETIME NOT NULL,+ quarantined_at DATETIME NOT NULL DEFAULT(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')),+ PRIMARY KEY (user_id, project_id)+ );+ """+ )+ cursor.execute(+ """--sql+ INSERT OR IGNORE INTO orphaned_projects_2026_08_06+ (project_id, user_id, name, data, revision, created_at, updated_at)+ SELECT project_id, user_id, name, data, revision, created_at, updated_at+ FROM projects+ WHERE user_id NOT IN (SELECT user_id FROM users);+ """+ )+for project_id, user_id in orphans:self._logger.warning(- f"Project boards migration: dropping project {project_id}, whose owner {user_id} no longer exists."+ f"Project boards migration: quarantining project {project_id}, whose owner {user_id} no longer exists,"+ " in orphaned_projects_2026_08_06.")cursor.execute(diff --git a/tests/app/services/shared/sqlite_migrator/migrations/test_migration_2026_08_06_add_project_boards.py b/tests/app/services/shared/sqlite_migrator/migrations/test_migration_2026_08_06_add_project_boards.pyindex 610c1b9da5..96ef0e28f3 100644--- a/tests/app/services/shared/sqlite_migrator/migrations/test_migration_2026_08_06_add_project_boards.py+++ b/tests/app/services/shared/sqlite_migrator/migrations/test_migration_2026_08_06_add_project_boards.py@@ -325,7 +325,7 @@ def test_a_refused_adoption_is_logged_with_its_reason() -> None:assert "another account" in warnings[0]-def test_a_project_whose_owner_is_gone_is_dropped_rather_than_failing_the_migration() -> None:+def test_a_project_whose_owner_is_gone_is_quarantined_before_the_live_table_is_rebuilt() -> None:"""The rebuilt table enforces the users foreign key that the old one did not. An orphan wouldabort the migration, and with it the app's ability to open the database at all."""db = _make_db()@@ -339,6 +339,10 @@ def test_a_project_whose_owner_is_gone_is_dropped_rather_than_failing_the_migrat_add_project(db, "orphan", user_id="gone", name="Orphan")db.commit()db.execute("PRAGMA foreign_keys = ON;")+ orphan_before = db.execute(+ "SELECT project_id, user_id, name, data, revision, created_at, updated_at FROM projects WHERE project_id = ?;",+ ("orphan",),+ ).fetchone()logger = Logger("migration-test")warnings: list[str] = []@@ -347,6 +351,9 @@ def test_a_project_whose_owner_is_gone_is_dropped_rather_than_failing_the_migratassert _board_of(db, "kept")assert db.execute("SELECT COUNT(*) FROM projects;").fetchone()[0] == 1+ assert db.execute(+ "SELECT project_id, user_id, name, data, revision, created_at, updated_at FROM orphaned_projects_2026_08_06;"+ ).fetchall() == [orphan_before]assert any("orphan" in warning for warning in warnings)