diff --git a/apps/codecov-api/core/admin.py b/apps/codecov-api/core/admin.py index 415cf4d61..2affa52fd 100644 --- a/apps/codecov-api/core/admin.py +++ b/apps/codecov-api/core/admin.py @@ -135,7 +135,11 @@ class Meta: class RepositoryAdmin(AdminMixin, admin.ModelAdmin): inlines = [RepositoryTokenInline] list_display = ("name", "service_id", "author") - search_fields = ("author__username__exact",) + search_fields = ( + "name", + "author__username__exact", + "service_id__exact", + ) show_full_result_count = False autocomplete_fields = ("bot",) form = RepositoryAdminForm @@ -172,21 +176,24 @@ def get_search_results( search_term: str, ) -> tuple[QuerySet[Repository], bool]: """ - Search for repositories by name or repoid. + Search for repositories by name, service_id, author username, or repoid. https://docs.djangoproject.com/en/5.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_search_results """ - # Default search is by author username (defined in `search_fields`) + # Default search is by name, author username, and service_id (defined in `search_fields`) queryset, may_have_duplicates = super().get_search_results( request, queryset, search_term, ) + # 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") return queryset, may_have_duplicates def has_add_permission(self, _, obj=None): diff --git a/libs/shared/shared/django_apps/core/migrations/0077_repository_repos_name_trgm_idx.py b/libs/shared/shared/django_apps/core/migrations/0077_repository_repos_name_trgm_idx.py new file mode 100644 index 000000000..d9bae1884 --- /dev/null +++ b/libs/shared/shared/django_apps/core/migrations/0077_repository_repos_name_trgm_idx.py @@ -0,0 +1,34 @@ +# Generated by Django 4.2.25 on 2025-12-04 01:26 + +import django.contrib.postgres.indexes +import django.db.models.functions.text +from django.db import migrations + +from shared.django_apps.migration_utils import RiskyAddIndex + + +class Migration(migrations.Migration): + """ + BEGIN; + -- + -- Create index repos_name_trgm_idx on OpClass(Upper(F(name)), name=gin_trgm_ops) on model repository + -- + CREATE INDEX "repos_name_trgm_idx" ON "repos" USING gin ((UPPER("name")) gin_trgm_ops); + COMMIT; + """ + + dependencies = [ + ("core", "0076_increment_version"), + ] + + operations = [ + RiskyAddIndex( + model_name="repository", + index=django.contrib.postgres.indexes.GinIndex( + django.contrib.postgres.indexes.OpClass( + django.db.models.functions.text.Upper("name"), name="gin_trgm_ops" + ), + name="repos_name_trgm_idx", + ), + ), + ] diff --git a/libs/shared/shared/django_apps/core/models.py b/libs/shared/shared/django_apps/core/models.py index ad6cb4b8a..bd7a88569 100644 --- a/libs/shared/shared/django_apps/core/models.py +++ b/libs/shared/shared/django_apps/core/models.py @@ -153,6 +153,9 @@ class Meta: fields=["service_id", "author"], name="repos_service_id_author", ), + GinIndex( + OpClass(Upper("name"), name="gin_trgm_ops"), name="repos_name_trgm_idx" + ), ] constraints = [ models.UniqueConstraint(fields=["author", "name"], name="repos_slug"),