Skip to content

perf: bound unbounded findMany queries (#1206) - #1214

Merged
LucasSantana-Dev merged 2 commits into
mainfrom
perf/bound-unbounded-queries
Jun 4, 2026
Merged

LucasSantana-Dev merged 2 commits into
mainfrom
perf/bound-unbounded-queries

Conversation

@LucasSantana-Dev

@LucasSantana-Dev LucasSantana-Dev commented Jun 4, 2026 •

Copy link
Copy Markdown
Owner

Closes part of #1206 by bounding two critical unbounded Prisma queries in hot paths.

Summary

  • Birthday scheduler (packages/bot/src/utils/general/birthdayScheduler.ts):
    Paginate guildSettings.findMany() in role reconciliation with take:500
    cursor-based pagination. Prevents full-table scans on large deployments when
    reconciling guilds without birthday role matches.

  • Feedback service (packages/bot/src/services/musicRecommendation/feedbackService.ts):
    Cap userArtistPreference.findMany() with take:5000 to protect against
    power-user preference sprawl in the autoplay scoring path.

Implementation details

  • Birthday scheduler pagination uses Prisma cursor + take + skip pattern with
    guildId ordering to safely traverse large result sets in 500-guild chunks.
  • Feedback service bounds respect typical user behavior (<100 prefs) while protecting
    against edge cases.
  • Behavior is preserved: birthday scheduler still reconciles all guilds over time
    (pagination is transparent); feedback scoring includes all user prefs up to 5000.

Tests added

  • Birthday scheduler: pagination with multiple pages and early-stop scenarios
  • Feedback service: bounds enforcement for preferred and blocked artist queries
  • All existing tests still pass

Follow-ups

  • Database indexes on (birthdayRoleId) and (discordUserId, guildId, preference)
    recommended for further optimization (out of scope for this PR).

Summary by cubic

Bound two unbounded Prisma queries in hot paths to prevent full-table scans and reduce latency on large deployments. Addresses part of Linear #1206.

  • Refactors
    • Birthday scheduler: paginate guildSettings.findMany with cursor-based 500-row pages ordered by guildId to reconcile roles for guilds without matches without scanning the table.
    • Feedback service: cap userArtistPreference.findMany with take: 5000 for preferred/blocked artist lookups in autoplay scoring.

Written for commit 3f33b26. Summary will update on new commits.

Review in cubic

Summary by CodeRabbit

  • Bug Fixes

    • Improved stability of music recommendation service by implementing query limits
    • Enhanced birthday scheduler efficiency with optimized batch processing
  • Tests

    • Added test coverage for query optimization improvements

@vercel

vercel Bot commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lucky Ready Ready Preview, Comment Jun 4, 2026 9:29pm

Request Review

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.

@coderabbitai

coderabbitai Bot commented Jun 4, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds result limits to two database query paths in the bot services to prevent unbounded record retrieval. The music recommendation service caps artist preference lookups at 5000 records per query, and the birthday scheduler replaces a single unbounded guild settings query with paginated 500-record batches.

Changes

Database Query Result Bounding

Layer / File(s) Summary
Artist preference query bounding
packages/bot/src/services/musicRecommendation/feedbackService.ts, packages/bot/src/services/musicRecommendation/feedbackService.spec.ts
getArtistKeysFromDb adds a take: 5000 limit to Prisma findMany queries for preferred and blocked artist preferences. Tests mock large result sets and verify the take option is applied correctly.
Guild settings pagination
packages/bot/src/utils/general/birthdayScheduler.ts, packages/bot/src/utils/general/birthdayScheduler.spec.ts
Imports are refactored to multiline form. reconcileGuildsWithoutMatches replaces unbounded guildSettings.findMany with cursor-based pagination using PAGE_SIZE and take: 500. Test helpers are reformatted and two new test cases verify pagination behavior across multiple batches and early termination when a page is smaller than the batch limit.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • LucasSantana-Dev/Lucky#1012: Refactors the same artist key getters (getPreferredArtistKeys/getBlockedArtistKeys) that are now bounded with take: 5000.
  • LucasSantana-Dev/Lucky#572: Modifies the artist preference handling path where this PR adds the take: 5000 cap to the underlying database queries.

Suggested labels

bot, enhancement, size/m

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'perf: bound unbounded findMany queries' accurately and concisely describes the main objective of the PR—adding bounds to unbounded Prisma queries to improve performance.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf/bound-unbounded-queries

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.

@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown

Failed to generate code suggestions for PR

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No issues found across 4 files

Auto-approved: This PR adds safe query limits (pagination and take) to two unbounded Prisma queries in non-critical performance paths, preserving existing behavior and reducing risk of full-table scans without altering business logic.

Re-trigger cubic

@LucasSantana-Dev
LucasSantana-Dev enabled auto-merge (squash) June 4, 2026 21:27

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.

@sonarqubecloud

sonarqubecloud Bot commented Jun 4, 2026

Copy link
Copy Markdown

@LucasSantana-Dev
LucasSantana-Dev merged commit cdc0082 into main Jun 4, 2026
38 checks passed
@LucasSantana-Dev
LucasSantana-Dev deleted the perf/bound-unbounded-queries branch June 4, 2026 21:33

This branch was successfully deployed

1 active deployment
Preview — 3f33b266 Deployed Jun 4, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot enhancement New feature or request size/m

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant