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(projects): name the real conflict when a create is repeated

Josh Corbett6b7cabe80fd3
diff --git a/invokeai/app/services/project_records/project_records_sqlite.py b/invokeai/app/services/project_records/project_records_sqlite.py
index 668a9208ae..5573b793bc 100644
--- a/invokeai/app/services/project_records/project_records_sqlite.py
+++ b/invokeai/app/services/project_records/project_records_sqlite.py
@@ -67,7 +67,7 @@ class ProjectRecordsSqlite(ProjectRecordsStorageBase):
if board_id is None:
resolved_board_id = self._insert_board(cursor, user_id=user_id, name=name)
else:
- self._claim_board(cursor, user_id=user_id, board_id=board_id, name=name)
+ self._claim_board(cursor, user_id=user_id, board_id=board_id, name=name, project_id=project_id)
resolved_board_id = board_id
cursor.execute(
@@ -298,12 +298,20 @@ class ProjectRecordsSqlite(ProjectRecordsStorageBase):
)
return board_id
- def _claim_board(self, cursor: sqlite3.Cursor, *, user_id: str, board_id: str, name: str) -> None:
+ def _claim_board(self, cursor: sqlite3.Cursor, *, user_id: str, board_id: str, name: str, project_id: str) -> None:
"""Take ownership of an existing private board, or explain why it cannot be taken.
One query answers every way a claim can fail. The `UNIQUE` constraint on `projects.board_id`
settles the race this check cannot see: two imports claiming the same board both pass here,
and exactly one of them survives the insert.
+
+ The project being created is excluded from the "already claimed" test, so a client that
+ re-sends a create it never got an answer for reaches the project insert and is refused there,
+ as `ProjectRecordExistsError` — the truth. Without the exclusion it would be told its board
+ was unavailable, which reads as "somebody else took it" and is the opposite of what happened.
+ A repeated create is not an edge case: it is how a client establishes whether a request whose
+ response was lost actually landed, and the answer decides whether it deletes the media it
+ uploaded.
"""
cursor.execute(
"""--sql
@@ -311,11 +319,14 @@ class ProjectRecordsSqlite(ProjectRecordsStorageBase):
b.user_id,
b.board_visibility,
EXISTS(SELECT 1 FROM shared_boards s WHERE s.board_id = b.board_id),
- EXISTS(SELECT 1 FROM projects p WHERE p.board_id = b.board_id)
+ EXISTS(
+ SELECT 1 FROM projects p
+ WHERE p.board_id = b.board_id AND NOT (p.user_id = ? AND p.project_id = ?)
+ )
FROM boards b
WHERE b.board_id = ?;
""",
- (board_id,),
+ (user_id, project_id, board_id),
)
row = cursor.fetchone()
diff --git a/tests/app/services/project_records/test_project_records.py b/tests/app/services/project_records/test_project_records.py
index 38aacefce8..9d5f81e461 100644
--- a/tests/app/services/project_records/test_project_records.py
+++ b/tests/app/services/project_records/test_project_records.py
@@ -303,6 +303,29 @@ def test_a_rejected_claim_leaves_the_board_name_alone(
assert _board_name(db, "staging") == "Untitled"
+def test_repeating_a_create_says_the_project_exists_rather_than_blaming_the_board(
+ project_records: ProjectRecordsSqlite, db: SqliteDatabase, other_user_id: str
+) -> None:
+ """A client whose create response was lost re-sends it to find out whether the first one landed,
+ and the answer decides whether it deletes the media it already uploaded. Reporting the board as
+ unavailable would read as "somebody else took it" — the opposite of what happened, and the
+ reading that gets the uploads thrown away."""
+ _insert_board(db, "staging", name="Untitled")
+ project_records.create(SYSTEM_USER_ID, "Imported", {}, project_id="project-import", board_id="staging")
+
+ with pytest.raises(ProjectRecordExistsError):
+ project_records.create(SYSTEM_USER_ID, "Imported", {}, project_id="project-import", board_id="staging")
+
+ # A different account claiming the same board is still refused: the exclusion is for the project
+ # being created, not for board claims in general. (Foreign ownership is caught first, as a 404,
+ # so the board is re-pointed at the other account to reach the claimed check.)
+ db._conn.execute("UPDATE boards SET user_id = ? WHERE board_id = 'staging';", (other_user_id,))
+ db._conn.commit()
+
+ with pytest.raises(ProjectBoardUnavailableError):
+ project_records.create(other_user_id, "Theirs", {}, project_id="project-import", board_id="staging")
+
+
def test_rename_renames_the_board_but_only_on_a_winning_save(
project_records: ProjectRecordsSqlite, db: SqliteDatabase
) -> None: