Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref(auto_source_config): Multiple calls does not create new repository #87437

Merged
merged 7 commits into from
Mar 21, 2025

Conversation

armenzg
Copy link
Member

@armenzg armenzg commented Mar 19, 2025

This depends on #87498 merging first.

In #86597 I introduced a bug causing many IntegrityErrors because we called create when the repository already existed.

This change is to have a test to prevent trying to create the repository more than once.

Copy link

codecov bot commented Mar 19, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #87437   +/-   ##
=======================================
  Coverage   87.73%   87.74%           
=======================================
  Files        9892     9897    +5     
  Lines      561283   561253   -30     
  Branches    22125    22112   -13     
=======================================
+ Hits       492465   492479   +14     
+ Misses      68416    68370   -46     
- Partials      402      404    +2     

@armenzg armenzg force-pushed the fix/process_repos_and_code_mappings/armenzg branch from 3fa19fe to 43daf9a Compare March 20, 2025 15:16
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Mar 20, 2025
armenzg added 4 commits March 20, 2025 11:21
In #86597 I introduced a bug causing many [IntegrityErrors](https://sentry.sentry.io/issues/?environment=prod&groupStatsPeriod=auto&page=0&project=1&query=error.type%3AIntegrityError%20transaction%3Asentry.tasks.auto_source_code_config&referrer=issue-list&statsPeriod=90d) because we called `create` when the code mapping already existed.

This change is to have a test to prevent trying to create the repository or code mappings more than once.
@armenzg armenzg force-pushed the fix/process_repos_and_code_mappings/armenzg branch from 43daf9a to 4e86339 Compare March 20, 2025 17:27
@armenzg armenzg changed the base branch from master to tests/trees/auto_source_config/armenzg March 20, 2025 17:27
if not repository:
created = False
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm refactoring some of the logic here to make sure the metrics are called correctly.

project=project, stack_root=code_mapping.stacktrace_root
)
if existing_code_mappings.exists():
logger.warning("Investigate.", extra=extra)
Copy link
Member Author

Choose a reason for hiding this comment

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

Pass by change.

This has not been happening in production, thus, removing it.

continue

RepositoryProjectPathConfig.objects.create(
_, created = RepositoryProjectPathConfig.objects.get_or_create(
Copy link
Member Author

Choose a reason for hiding this comment

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

Changing from create to get_or_create.

if not dry_run:
repository = Repository.objects.create(
repository, created = Repository.objects.get_or_create(
Copy link
Member Author

Choose a reason for hiding this comment

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

Notice I'm changing from create to get_or_create.

@@ -91,8 +91,24 @@ def _process_and_assert_code_mapping(
),
patch("sentry.utils.metrics.incr") as mock_incr,
):
repositories_count = Repository.objects.all().count()
code_mappings_count = RepositoryProjectPathConfig.objects.all().count()
Copy link
Member Author

Choose a reason for hiding this comment

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

These two variables are used below to determining if new code mappings and repositories have been created.

@armenzg armenzg marked this pull request as ready for review March 20, 2025 17:32
@armenzg armenzg requested a review from a team as a code owner March 20, 2025 17:32
@armenzg armenzg requested a review from MichaelSun48 March 20, 2025 17:32
@armenzg armenzg changed the title tests(auto_source_config): Multiple calls does not create new repository ref(auto_source_config): Multiple calls does not create new repository Mar 20, 2025
Copy link
Member

@MichaelSun48 MichaelSun48 left a comment

Choose a reason for hiding this comment

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

Just the one comment in your tests. Otherwise lgtm.

Just curious — were there any lasting consequences here? Or did we just get a ton of Integrity errors because we tried to create repositories that already existed?

Base automatically changed from tests/trees/auto_source_config/armenzg to master March 21, 2025 11:55
@armenzg armenzg enabled auto-merge (squash) March 21, 2025 12:09
@armenzg armenzg merged commit 0b751a2 into master Mar 21, 2025
48 checks passed
@armenzg armenzg deleted the fix/process_repos_and_code_mappings/armenzg branch March 21, 2025 12:38
@armenzg
Copy link
Member Author

armenzg commented Mar 21, 2025

Just the one comment in your tests. Otherwise lgtm.

Just curious — were there any lasting consequences here? Or did we just get a ton of Integrity errors because we tried to create repositories that already existed?

We get the errors, but the DB was not changed.

Copy link

sentry-io bot commented Mar 21, 2025

Suspect Issues

This pull request was deployed and Sentry observed the following issues:

  • ‼️ IntegrityError: UniqueViolation('duplicate key value violates unique constraint "sentry_repositoryproject_project_id_stack_root_e376b891_uniq"\nDETAIL: Key (project_id, stack_root)=(1825323, /web/wp/wp-includes/) already exists.\n') sentry.tasks.auto_source_code_config View Issue
  • ‼️ IntegrityError: UniqueViolation('duplicate key value violates unique constraint "sentry_repositoryproject_project_id_stack_root_e376b891_uniq"\nDETAIL: Key (project_id, stack_root)=(6173372, ../var/h/deploy/airtable/current/) already exists.\n') sentry.tasks.auto_source_code_config View Issue
  • ‼️ IntegrityError: UniqueViolation('duplicate key value violates unique constraint "sentry_repositoryproject_project_id_stack_root_e376b891_uniq"\nDETAIL: Key (project_id, stack_root)=(4506556170764288, /go/src/github.com/squareup/go-square/) already exists.\n') sentry.tasks.auto_source_code_config View Issue
  • ‼️ IntegrityError: UniqueViolation('duplicate key value violates unique constraint "sentry_repositoryproject_project_id_stack_root_e376b891_uniq"\nDETAIL: Key (project_id, stack_root)=(4505781175255040, /var/task/src/controllers/worker/webpack:/roberto/) already exists.\n') sentry.tasks.auto_source_code_config View Issue
  • ‼️ IntegrityError: UniqueViolation('duplicate key value violates unique constraint "sentry_repositoryproject_project_id_stack_root_e376b891_uniq"\nDETAIL: Key (project_id, stack_root)=(5275517, chalicelib/) already exists.\n') sentry.tasks.auto_source_code_config View Issue

Did you find this useful? React with a 👍 or 👎

armenzg added a commit that referenced this pull request Mar 21, 2025
This fixes a bunch of integrity errors introduced in #87437.

From the
[docs](https://docs.djangoproject.com/en/5.1/ref/models/querysets/#get-or-create):

> Any keyword arguments passed to get_or_create() — except an optional
one called defaults — will be used in a get() call. If an object is
found, get_or_create() returns a tuple of that object and False.
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants