Skip to content

Conversation

@calvin-codecov
Copy link
Contributor

@calvin-codecov calvin-codecov commented Dec 2, 2025

Closes https://linear.app/getsentry/issue/CCMRG-1906/allow-django-admin-repository-table-to-be-searchable-across-more


Note

Expands Django admin repository search (name, service_id, numeric repoid) and adds a GIN trigram index on UPPER(name) for faster lookups.

  • Admin (Django):
    • Extend RepositoryAdmin.search_fields to include name and service_id__exact.
    • Enhance get_search_results to match numeric repoid and select_related("author") to avoid N+1.
  • Database/Models:
    • Add GinIndex(OpClass(Upper("name"), name="gin_trgm_ops"), name="repos_name_trgm_idx") to Repository.Meta.indexes and new migration core/0077_repository_repos_name_trgm_idx.py.

Written by Cursor Bugbot for commit 4db8f39. This will update automatically on new commits. Configure here.

@linear
Copy link

linear bot commented Dec 2, 2025

@sentry
Copy link

sentry bot commented Dec 2, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.87%. Comparing base (a2f8b4d) to head (4db8f39).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #591   +/-   ##
=======================================
  Coverage   93.87%   93.87%           
=======================================
  Files        1284     1285    +1     
  Lines       46667    46675    +8     
  Branches     1522     1522           
=======================================
+ Hits        43809    43817    +8     
  Misses       2548     2548           
  Partials      310      310           
Flag Coverage Δ
apiunit 96.57% <100.00%> (+<0.01%) ⬆️
sharedintegration 38.77% <0.00%> (-0.02%) ⬇️
sharedunit 88.77% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@codecov-eu
Copy link

codecov-eu bot commented Dec 2, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@codecov-notifications
Copy link

codecov-notifications bot commented Dec 2, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@calvin-codecov calvin-codecov marked this pull request as ready for review December 3, 2025 20:48
# Also search by repoid if the search term is numeric
try:
search_term_as_int = int(search_term)
except ValueError:

This comment was marked as outdated.

Copy link
Contributor

@thomasrockhu-codecov thomasrockhu-codecov left a comment

Choose a reason for hiding this comment

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

Should double-check that those fields are indexed or it's going to be slow as hell

Is service_id not just github, gitlab, etc?

@calvin-codecov calvin-codecov force-pushed the cy/repo_admin_search_fields branch from 2350489 to ac9554d Compare December 4, 2025 01:34
@calvin-codecov calvin-codecov requested a review from a team as a code owner December 4, 2025 01:34
Comment on lines +188 to +196
# Also search by repoid if the search term is numeric
try:
search_term_as_int = int(search_term)
except ValueError:
pass
else:
queryset |= self.model.objects.filter(repoid=search_term_as_int)
# avoid N+1 queries for foreign key author
queryset = queryset.select_related("author")
Copy link

Choose a reason for hiding this comment

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

Bug: Applying select_related("author") after union() for a queryset added without prior optimization causes N+1 queries for the author relationship.
Severity: CRITICAL | Confidence: High

🔍 Detailed Analysis

The code applies select_related("author") after performing a union() operation on two querysets. One of the querysets, self.model.objects.filter(repoid=search_term_as_int), is added without select_related(). This violates Django best practices, which require select_related() to be applied before union(). This will cause N+1 queries when accessing the author relationship for repositories matched by repoid in the Django admin, leading to significant performance degradation.

💡 Suggested Fix

Apply select_related("author") to the new queryset self.model.objects.filter(repoid=search_term_as_int) before the union() operation. The final select_related call can remain or be removed.

🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: apps/codecov-api/core/admin.py#L188-L196

Potential issue: The code applies `select_related("author")` after performing a
`union()` operation on two querysets. One of the querysets,
`self.model.objects.filter(repoid=search_term_as_int)`, is added without
`select_related()`. This violates Django best practices, which require
`select_related()` to be applied before `union()`. This will cause N+1 queries when
accessing the `author` relationship for repositories matched by `repoid` in the Django
admin, leading to significant performance degradation.

Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID: 5359759

@codspeed-hq
Copy link

codspeed-hq bot commented Dec 4, 2025

CodSpeed Performance Report

Merging #591 will not alter performance

Comparing cy/repo_admin_search_fields (4db8f39) with main (7383d2c)1

Summary

✅ 9 untouched

Footnotes

  1. No successful run was found on main (a2f8b4d) during the generation of this report, so 7383d2c was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants