Skip to content

feat(code-mappings): Allow multiple source roots per stack root#111704

Merged
romtsn merged 4 commits intomasterfrom
rz/feat/code-mappings-unique-constraint
Mar 31, 2026
Merged

feat(code-mappings): Allow multiple source roots per stack root#111704
romtsn merged 4 commits intomasterfrom
rz/feat/code-mappings-unique-constraint

Conversation

@romtsn
Copy link
Copy Markdown
Member

@romtsn romtsn commented Mar 27, 2026

Summary

  • Change unique constraint on RepositoryProjectPathConfig from (project, stack_root) to (project, stack_root, source_root)
  • Update single-mapping serializer validation to include source_root in duplicate check
  • Update bulk endpoint upsert key to match on (stack_root, source_root) instead of just stack_root

This allows the same stack trace root (e.g. io/sentry/opentelemetry) to map to multiple source paths in the repo (e.g. sentry-opentelemetry-core/src/... and sentry-opentelemetry-bootstrap/src/...), which is needed for monorepos with shared package prefixes across modules.

Stack trace resolution already handles this correctly — it iterates all mappings sorted by specificity and uses the first match that resolves to a real file.

Test plan

  • All 63 existing tests pass (bulk + single endpoints)
  • Updated tests to reflect new constraint semantics
  • Deploy migration and verify with real monorepo upload

🤖 Generated with Claude Code

Change the unique constraint on RepositoryProjectPathConfig from
(project, stack_root) to (project, stack_root, source_root). This
allows the same stack trace root to map to multiple source paths,
which is needed for monorepos where the same package prefix exists
in multiple modules (e.g. io/sentry/opentelemetry mapping to both
sentry-opentelemetry-core and sentry-opentelemetry-bootstrap).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@romtsn romtsn requested review from a team as code owners March 27, 2026 12:11
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Mar 27, 2026
@romtsn romtsn requested a review from armenzg March 27, 2026 12:13
Comment thread src/sentry/integrations/models/repository_project_path_config.py
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 27, 2026

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

for 1058_change_code_mapping_unique_constraint in sentry

--
-- Custom state/database change combination
--
CREATE UNIQUE INDEX CONCURRENTLY "sentry_repositoryproject_project_id_stack_root_so_c371dfa7_uniq" ON "sentry_repositoryprojectpathconfig" ("project_id", "stack_root", "source_root");
ALTER TABLE "sentry_repositoryprojectpathconfig" ADD CONSTRAINT "sentry_repositoryproject_project_id_stack_root_so_c371dfa7_uniq" UNIQUE USING INDEX "sentry_repositoryproject_project_id_stack_root_so_c371dfa7_uniq";
ALTER TABLE "sentry_repositoryprojectpathconfig" DROP CONSTRAINT "sentry_repositoryproject_project_id_stack_root_e376b891_uniq";

Copy link
Copy Markdown
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.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Comment thread src/sentry/integrations/models/repository_project_path_config.py
… lookups

Update auto-source-code-config callers to include source_root in
their lookup keys, matching the new unique constraint on
(project, stack_root, source_root). Without this, these calls
would raise MultipleObjectsReturned once multiple mappings share
the same stack_root.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown
Contributor

Backend Test Failures

Failures on e9b55a7 in this run:

tests/sentry/api/endpoints/issues/test_organization_derive_code_mappings.py::OrganizationDeriveCodeMappingsTest::test_post_existing_code_mappinglog
tests/sentry/api/endpoints/issues/test_organization_derive_code_mappings.py:324: in test_post_existing_code_mapping
    new_code_mapping = RepositoryProjectPathConfig.objects.get(
.venv/lib/python3.13/site-packages/django/db/models/manager.py:87: in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
.venv/lib/python3.13/site-packages/django/db/models/query.py:638: in get
    raise self.model.MultipleObjectsReturned(
E   sentry.integrations.models.repository_project_path_config.RepositoryProjectPathConfig.MultipleObjectsReturned: get() returned more than one RepositoryProjectPathConfig -- it returned 2!

…traint

Update test_post_existing_code_mapping to assert both mappings coexist
when stack_root matches but source_root differs, matching the new
(project, stack_root, source_root) unique constraint.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Member

@armenzg armenzg left a comment

Choose a reason for hiding this comment

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

I really like this change.
We will see a huge uptick on code mappings for monorepos but I believe it will make a lot of things better.

Comment thread src/sentry/migrations/1058_change_code_mapping_unique_constraint.py
…ration

Use SeparateDatabaseAndState to add the new unique constraint
before removing the old one, avoiding a window without any
unique constraint on the table.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@romtsn romtsn merged commit 18cd9af into master Mar 31, 2026
107 of 108 checks passed
@romtsn romtsn deleted the rz/feat/code-mappings-unique-constraint branch March 31, 2026 12:16
jan-auer pushed a commit that referenced this pull request Apr 1, 2026
)

## Summary

- Change unique constraint on `RepositoryProjectPathConfig` from
`(project, stack_root)` to `(project, stack_root, source_root)`
- Update single-mapping serializer validation to include `source_root`
in duplicate check
- Update bulk endpoint upsert key to match on `(stack_root,
source_root)` instead of just `stack_root`

This allows the same stack trace root (e.g. `io/sentry/opentelemetry`)
to map to multiple source paths in the repo (e.g.
`sentry-opentelemetry-core/src/...` and
`sentry-opentelemetry-bootstrap/src/...`), which is needed for monorepos
with shared package prefixes across modules.

Stack trace resolution already handles this correctly — it iterates all
mappings sorted by specificity and uses the first match that resolves to
a real file.

## Test plan

- [x] All 63 existing tests pass (bulk + single endpoints)
- [x] Updated tests to reflect new constraint semantics
- [ ] Deploy migration and verify with real monorepo upload

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
dashed pushed a commit that referenced this pull request Apr 1, 2026
)

## Summary

- Change unique constraint on `RepositoryProjectPathConfig` from
`(project, stack_root)` to `(project, stack_root, source_root)`
- Update single-mapping serializer validation to include `source_root`
in duplicate check
- Update bulk endpoint upsert key to match on `(stack_root,
source_root)` instead of just `stack_root`

This allows the same stack trace root (e.g. `io/sentry/opentelemetry`)
to map to multiple source paths in the repo (e.g.
`sentry-opentelemetry-core/src/...` and
`sentry-opentelemetry-bootstrap/src/...`), which is needed for monorepos
with shared package prefixes across modules.

Stack trace resolution already handles this correctly — it iterates all
mappings sorted by specificity and uses the first match that resolves to
a real file.

## Test plan

- [x] All 63 existing tests pass (bulk + single endpoints)
- [x] Updated tests to reflect new constraint semantics
- [ ] Deploy migration and verify with real monorepo upload

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions github-actions bot locked and limited conversation to collaborators Apr 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants