Skip to content

Commit 2f212c7

Browse files
ambvclaude
andcommitted
Use asyncpg constraint_name for duplicate upload detection
Check e.orig.constraint_name first (available on asyncpg's UniqueViolationError), falling back to string matching for backends that don't expose it (e.g. SQLite in tests). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3490416 commit 2f212c7

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

backend/app/routers/upload.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -342,14 +342,18 @@ def clean_flag(flag):
342342
}
343343

344344
except IntegrityError as e:
345-
# The only unique constraint that can fire here is
346-
# unique_commit_binary_env on the runs table.
347-
error_str = str(e).lower()
348-
if "unique_commit_binary_env" in error_str or (
349-
"commit_sha" in error_str
350-
and "binary_id" in error_str
351-
and "environment_id" in error_str
352-
):
345+
# Check for duplicate run constraint violation.
346+
# asyncpg exposes constraint_name directly on the original exception;
347+
# fall back to string matching for other backends (e.g. SQLite).
348+
constraint = getattr(getattr(e, "orig", None), "constraint_name", None)
349+
is_duplicate = (
350+
constraint == "unique_commit_binary_env"
351+
if constraint is not None
352+
else "commit_sha" in str(e).lower()
353+
and "binary_id" in str(e).lower()
354+
and "environment_id" in str(e).lower()
355+
)
356+
if is_duplicate:
353357
logger.error(
354358
f"Upload failed: Duplicate run for commit {commit_sha[:8]}, binary '{binary_id}', environment '{environment_id}'"
355359
)

0 commit comments

Comments
 (0)