Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

Fix N+1 query issue in releases field#3767

Open
HarshitVerma109 wants to merge 12 commits intoOWASP:mainfrom
HarshitVerma109:fix/releases-n-plus-1-query
Open

Fix N+1 query issue in releases field#3767
HarshitVerma109 wants to merge 12 commits intoOWASP:mainfrom
HarshitVerma109:fix/releases-n-plus-1-query

Conversation

@HarshitVerma109
Copy link
Contributor

Proposed change

Resolves #3662

Use Prefetch to fix N+1 query issue in releases field.

Checklist

  • Required: I followed the contributing workflow
  • Required: I verified that my code works as intended and resolves the issue as described
  • Required: I ran make check-test locally: all warnings addressed, tests passed
  • I used AI for code, documentation, tests, or communication related to this PR

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 4, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

Replace RepositoryNode's simple releases access with a Django Prefetch that prefilters releases (no drafts, no pre-releases, non-null published_at, ordered by published_at desc), stores results on prefetched_releases, and update the resolver and tests to use that prefetched attribute.

Changes

Cohort / File(s) Summary
Repository Node Implementation
backend/apps/github/api/internal/nodes/repository.py
Add Prefetch and Release imports; define a custom Prefetch for releases that filters is_draft=False, is_pre_release=False, published_at__isnull=False, orders by -published_at, and uses to_attr='prefetched_releases'; update resolver to return root.prefetched_releases.
Repository Node Tests
backend/tests/apps/github/api/internal/nodes/repository_test.py
Update test to set mock_repository.prefetched_releases = mock_releases and assert resolver returns that list; remove previous order_by expectation and usage of published_releases chain.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • arkid15r
  • kasya
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: fixing an N+1 query issue in the releases field using Prefetch, which is the primary objective of this PR.
Description check ✅ Passed The description accurately relates to the changeset by explaining the use of Django's Prefetch to fix the N+1 query issue in the releases field, matching the code changes in the PR.
Linked Issues check ✅ Passed The PR fully addresses issue #3662 by replacing optimization hints with Django's Prefetch object to eliminate N+1 queries when resolving the releases field, as required.
Out of Scope Changes check ✅ Passed All changes are within scope: modifications to the releases field implementation and its corresponding test align directly with the N+1 query optimization objective.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot]
coderabbitai bot previously approved these changes Feb 4, 2026
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 2 files

Confidence score: 3/5

  • There is a concrete behavior bug: the [:RECENT_RELEASES_LIMIT] slice in the Prefetch will cap releases across all repositories, so multi-repo queries can return too few releases per repo.
  • Given the severity (8/10) and high confidence, this poses a real user-facing regression risk if the endpoint is used with multiple repositories.
  • Pay close attention to backend/apps/github/api/internal/nodes/repository.py - the global slice in Prefetch limits releases across all repositories.
Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="backend/apps/github/api/internal/nodes/repository.py">

<violation number="1" location="backend/apps/github/api/internal/nodes/repository.py:89">
P1: The slice `[:RECENT_RELEASES_LIMIT]` inside `Prefetch` applies globally, not per repository. When querying multiple repositories, this will return at most 5 releases total across ALL repositories instead of 5 per repository.

Remove the slice from the `Prefetch` queryset and apply it in the resolver instead, similar to how `issues` and `recent_milestones` are implemented in this file.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

coderabbitai[bot]
coderabbitai bot previously approved these changes Feb 4, 2026
@HarshitVerma109
Copy link
Contributor Author

@cubic-dev-ai review this pull request

@cubic-dev-ai
Copy link
Contributor

cubic-dev-ai bot commented Feb 4, 2026

@cubic-dev-ai review this pull request

@HarshitVerma109 I have started the AI code review. It will take a few minutes to complete.

cubic-dev-ai[bot]
cubic-dev-ai bot previously approved these changes Feb 4, 2026
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

No issues found across 2 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

@HarshitVerma109
Copy link
Contributor Author

@arkid15r , please review it and let me know if any changes are required.

Copy link
Collaborator

@ahmedxgouda ahmedxgouda left a comment

Choose a reason for hiding this comment

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

Thank you for working on this. We are near.

@ahmedxgouda ahmedxgouda self-assigned this Feb 9, 2026
coderabbitai[bot]
coderabbitai bot previously approved these changes Feb 9, 2026
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@backend/apps/github/api/internal/nodes/repository.py`:
- Around line 81-96: The Prefetch for Release.objects.filter used in the
strawberry_django.field decorator (with to_attr="prefetched_releases") is
missing prefetching of repository__project_set, causing N+1 queries when
ReleaseNode.project_name is resolved; update the Prefetch queryset (the lambda
returning Prefetch) to include .prefetch_related("repository__project_set") so
that repository__project_set is loaded with the prefetched_releases and the
ReleaseNode.project_name resolver can read project data without extra queries.

coderabbitai[bot]
coderabbitai bot previously approved these changes Feb 9, 2026
Copy link
Collaborator

@ahmedxgouda ahmedxgouda left a comment

Choose a reason for hiding this comment

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

You optimized N+1 queries problem for the repositories query, but the repository query is failing now.
Image

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 2 files (changes from recent commits).

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="backend/apps/github/api/internal/nodes/repository.py">

<violation number="1" location="backend/apps/github/api/internal/nodes/repository.py:107">
P2: Fallback query path is missing `select_related`/`prefetch_related` that the `Prefetch` queryset applies. If this fallback is ever reached, it will trigger N+1 queries on `author__owasp_profile`, `repository__organization`, and `repository__project_set` — the same issue this PR is fixing. Consider applying the same optimizations to the fallback queryset.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

coderabbitai[bot]
coderabbitai bot previously approved these changes Feb 10, 2026
@HarshitVerma109
Copy link
Contributor Author

@ahmedxgouda, I think the problem is completely solved now.

@ahmedxgouda
Copy link
Collaborator

@HarshitVerma109
This is spamming. I will take a look into it soon.
image

@HarshitVerma109
Copy link
Contributor Author

Sorry for the pings! I didn't intend to spam; I was just worried the message might have been missed. I’ll be more patient in the future and wait for your feedback. Thanks for your time!

@ahmedxgouda
Copy link
Collaborator

@HarshitVerma109 It is not about the pings, it is about deleting and posting the same comment. Any way, sorry for the delays.

@sonarqubecloud
Copy link

@codecov
Copy link

codecov bot commented Feb 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.34%. Comparing base (62fbb50) to head (eb9a7b2).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #3767   +/-   ##
=======================================
  Coverage   96.34%   96.34%           
=======================================
  Files         512      512           
  Lines       15815    15823    +8     
  Branches     2174     2176    +2     
=======================================
+ Hits        15237    15245    +8     
  Misses        431      431           
  Partials      147      147           
Flag Coverage Δ
backend 95.99% <100.00%> (+<0.01%) ⬆️
frontend 97.42% <ø> (ø)

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

Files with missing lines Coverage Δ
backend/apps/github/api/internal/nodes/release.py 100.00% <100.00%> (ø)
...ckend/apps/github/api/internal/nodes/repository.py 100.00% <100.00%> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 62fbb50...eb9a7b2. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Collaborator

@ahmedxgouda ahmedxgouda left a comment

Choose a reason for hiding this comment

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

LGTM

@HarshitVerma109
Copy link
Contributor Author

@arkid15r , please review it and let me know if any changes are required.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize releases field in RepositoryNode

2 participants