Skip to content

Conversation

@trevor-e
Copy link
Member

@trevor-e trevor-e commented Jan 30, 2026

Fixes https://sentry.sentry.io/issues/7229263157/?query=is%3Aunresolved%20assigned%3A%23emerge-tools&referrer=issue-stream&sort=date by adding a unique constraint.

Basically due to race conditions of this particular user sending a high volume of writes to us, we saw cases where multiple objects are created despite the CommitComparison.objects.get_or_create() call within the transaction. This can cause some downstream issues that assume only one CommitComparison exists.

@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Jan 30, 2026
Copy link
Contributor

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

@github-actions
Copy link
Contributor

This PR has a migration; here is the generated SQL for src/sentry/migrations/1023_add_commitcomparison_unique_constraints.py

for 1023_add_commitcomparison_unique_constraints in sentry

--
-- Raw Python operation
--
-- THIS OPERATION CANNOT BE WRITTEN AS SQL
--
-- Create constraint sentry_commitcomparison_org_comparison_uniq on model commitcomparison
--
CREATE UNIQUE INDEX CONCURRENTLY "sentry_commitcomparison_org_comparison_uniq" ON "sentry_commitcomparison" ("organization_id", "head_repo_name", "head_sha", "base_sha") WHERE "base_sha" IS NOT NULL;
--
-- Create constraint sentry_commitcomparison_org_commit_uniq on model commitcomparison
--
CREATE UNIQUE INDEX CONCURRENTLY "sentry_commitcomparison_org_commit_uniq" ON "sentry_commitcomparison" ("organization_id", "head_repo_name", "head_sha") WHERE "base_sha" IS NULL;
Tests were creating CommitComparison records with the same unique key
values as the setUp fixture. Changed head_sha values to be unique
per test to avoid IntegrityError.
@trevor-e trevor-e marked this pull request as ready for review January 30, 2026 23:32
@trevor-e trevor-e requested review from a team as code owners January 30, 2026 23:32
PreprodArtifact.objects.filter(commit_comparison_id=duplicate_id).update(
commit_comparison_id=kept_id
)
CommitComparison.objects.filter(id__in=duplicate_to_kept.keys()).delete()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how many rows there are to delete here, but it could be safer to just delete individually so that you don't hit any transaction timeouts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

3 participants