Skip to content

Night Shift: add compound indexes for song queries#21

Open
EtanHey wants to merge 1 commit intomasterfrom
nightshift/2026-02-25-3798
Open

Night Shift: add compound indexes for song queries#21
EtanHey wants to merge 1 commit intomasterfrom
nightshift/2026-02-25-3798

Conversation

@EtanHey
Copy link
Owner

@EtanHey EtanHey commented Feb 25, 2026

User description

Automated improvement by Golems Night Shift.

add compound indexes for song queries


PR Type

Enhancement


Description

  • Add compound indexes for user+song queries in database schema

  • Update 6 query methods to use new indexes instead of filtering

  • Eliminate table scans on hot path operations like checkbox toggles

  • Minor formatting and code style improvements


Diagram Walkthrough

flowchart LR
  A["userSongProgress & lineProgress tables"] -->|"Add compound indexes"| B["by_user_song, by_user_song_line"]
  B -->|"Replace filter operations"| C["6 query methods"]
  C -->|"Eliminate table scans"| D["Improved performance"]
Loading

File Walkthrough

Relevant files
Configuration changes
schema.ts
Add compound indexes to schema tables                                       

convex/schema.ts

  • Add by_user_song compound index to userSongProgress table
  • Add by_user_song and by_user_song_line compound indexes to
    lineProgress table
  • Indexes combine userId and songId (and lineNumber for lineProgress) to
    optimize queries
+5/-2     
Enhancement
songProgress.ts
Replace filter operations with compound indexes                   

convex/songProgress.ts

  • Update getByUserSong to use by_user_song index instead of filtering
  • Update recordLineCompletion to use by_user_song index for lookups
  • Update recordLinesCompletion to use by_user_song index for existing
    record queries
  • Update toggleLineLearned to use by_user_song_line index for
    lineProgress queries
  • Update getLineProgressByUserSong to use by_user_song index instead of
    filtering
  • Minor formatting improvements for code readability
+46/-38 

Summary by CodeRabbit

  • Performance Improvements
    • Optimized database queries for user song progress and line completion tracking to enhance data retrieval efficiency and improve overall application responsiveness.

The songProgress queries used .withIndex("by_user").filter(songId)
which scans all of a user's records to find one song. Added compound
indexes (by_user_song, by_user_song_line) to userSongProgress and
lineProgress tables, and updated all 6 affected queries to use them.
This eliminates table scans on the hot path (checkbox toggles, page loads).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@vercel
Copy link

vercel bot commented Feb 25, 2026

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

Project Deployment Actions Updated (UTC)
songscript Building Building Preview, Comment Feb 25, 2026 2:08am

@coderabbitai
Copy link

coderabbitai bot commented Feb 25, 2026

📝 Walkthrough

Walkthrough

The changes introduce new composite database indices to the schema (by_user_song and by_user_song_line) and update corresponding query and mutation operations to leverage these indices for improved query efficiency. The public API signatures remain unchanged.

Changes

Cohort / File(s) Summary
Schema Indices
convex/schema.ts
Added three composite indices: by_user_song on userSongProgress table and by_user_song, by_user_song_line on lineProgress table to enable more efficient filtered queries.
Query and Mutation Updates
convex/songProgress.ts
Updated seven data retrieval and modification operations to use new composite indices instead of the previous by_user index, adjusting query predicates to chain userId and songId. Affected functions: getByUser, getByUserSong, getLineProgressByUserSong, recordLineCompletion, recordLinesCompletion, toggleLineLearned, getRecentForContinue, and getWithSongDetails. Progress calculations and filtering logic remain unchanged.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

Indices composite, shining so bright,
By user and song, aligned just right,
Queries now leap with database grace,
Performance gains at a breakneck pace,
The bunny approves! 🐰⚡

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding compound indexes for song queries, which is the primary focus of both schema.ts and songProgress.ts modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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 (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch nightshift/2026-02-25-3798

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.

@qodo-code-review
Copy link

PR Compliance Guide 🔍

Below is a summary of compliance checks for this PR:

Security Compliance
🟢
No security concerns identified No security vulnerabilities detected by AI analysis. Human verification advised for critical code.
Ticket Compliance
🎫 No ticket provided
  • Create ticket/issue
Codebase Duplication Compliance
Codebase context is not defined

Follow the guide to enable codebase context checks.

Custom Compliance
🟢
Generic: Comprehensive Audit Trails

Objective: To create a detailed and reliable record of critical system actions for security analysis
and compliance.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Meaningful Naming and Self-Documenting Code

Objective: Ensure all identifiers clearly express their purpose and intent, making code
self-documenting

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Robust Error Handling and Edge Case Management

Objective: Ensure comprehensive error handling that provides meaningful context and graceful
degradation

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Error Handling

Objective: To prevent the leakage of sensitive system information through error messages while
providing sufficient detail for internal debugging.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Logging Practices

Objective: To ensure logs are useful for debugging and auditing without exposing sensitive
information like PII, PHI, or cardholder data.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Security-First Input Validation and Data Handling

Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent
vulnerabilities

Status:
Optional index keys: The new compound indexes by_user_song and by_user_song_line index the optional field
userId, which may be unsupported or may create unexpected query behavior when userId is
undefined and should be confirmed against Convex indexing constraints.

Referred Code
  .index("by_visitor_song", ["visitorId", "songId"])
  .index("by_user_song", ["userId", "songId"]),

// Track which lines users have explicitly marked as "learned"
// Separate from linesCompleted which tracks practice - this is for mastery
lineProgress: defineTable({
  visitorId: v.string(), // localStorage-generated ID if no auth
  userId: v.optional(v.string()), // Better Auth user ID
  songId: v.id("songs"),
  lineNumber: v.number(),
  learned: v.boolean(), // Whether this line is marked as learned
})
  .index("by_visitor", ["visitorId"])
  .index("by_user", ["userId"])
  .index("by_visitor_song", ["visitorId", "songId"])
  .index("by_visitor_song_line", ["visitorId", "songId", "lineNumber"])
  .index("by_user_song", ["userId", "songId"])
  .index("by_user_song_line", ["userId", "songId", "lineNumber"]),

Learn more about managing compliance generic rules or creating your own custom rules

Compliance status legend 🟢 - Fully Compliant
🟡 - Partial Compliant
🔴 - Not Compliant
⚪ - Requires Further Human Verification
🏷️ - Compliance label

@qodo-code-review
Copy link

PR Code Suggestions ✨

No code suggestions found for the PR.

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.

1 participant