-
Notifications
You must be signed in to change notification settings - Fork 3
Recipient query and model #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
50f1bc5
Add schedule sentinel date constant and migration for optimized cron …
cursoragent 8604ae3
Optimize cron query with raw SQL and sentinel date pattern
cursoragent 904480c
Use sentinel date instead of NULL for invalid schedules
cursoragent ab48656
Update scripts to use sentinel date for invalid schedules
cursoragent a5105ee
Update tests to compute schedule fields for recipients
cursoragent b20519c
Use Prisma TypedSQL and separate sentinel dates for prev/next schedule
cursoragent c40d410
Fix sentinel updates and cron test
cursoragent eca49fc
Add documentation explaining denormalized fields and sentinel dates
cursoragent d5a246c
Fix overdue cron test coverage
cursoragent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /** | ||
| * SCHEDULE SENTINEL DATES | ||
| * | ||
| * These sentinel values replace NULL for schedule fields, enabling efficient SQLite index usage. | ||
| * | ||
| * WHY NOT NULL? | ||
| * The original cron query used `OR nextScheduledAt IS NULL` which defeats SQLite index usage, | ||
| * causing full table scans (~259ms). By using sentinel dates instead of NULL: | ||
| * - The query becomes a simple range check: `nextScheduledAt <= $cutoff` | ||
| * - SQLite can use the composite index efficiently (single-digit ms) | ||
| * | ||
| * See: prisma/sql/getrecipientsforcron.sql for full performance documentation. | ||
| */ | ||
|
|
||
| /** | ||
| * Sentinel date used when nextScheduledAt cannot be computed (invalid cron, etc.) | ||
| * Using a far-future date (9999-12-31) means these recipients are naturally filtered out | ||
| * by the `nextScheduledAt <= reminderCutoff` condition - no special handling needed. | ||
| */ | ||
| export const NEXT_SCHEDULE_SENTINEL_DATE = new Date('9999-12-31T23:59:59.999Z') | ||
|
|
||
| /** | ||
| * Sentinel date used when prevScheduledAt cannot be computed (invalid cron, etc.) | ||
| * Using a far-past date (1970-01-01) indicates there was never a valid previous schedule. | ||
| */ | ||
| export const PREV_SCHEDULE_SENTINEL_DATE = new Date('1970-01-01T00:00:00.000Z') | ||
|
|
||
| /** | ||
| * @deprecated Use NEXT_SCHEDULE_SENTINEL_DATE instead | ||
| */ | ||
| export const SCHEDULE_SENTINEL_DATE = NEXT_SCHEDULE_SENTINEL_DATE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
prisma/migrations/20260202140000_optimize_cron_query/migration.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| -- Optimize the cron query by adding a composite index that matches the query pattern | ||
| -- This index covers: verified, disabled, nextScheduledAt (for range queries), and userId (for the join) | ||
|
|
||
| -- Drop existing less optimal indexes that will be superseded | ||
| DROP INDEX IF EXISTS "Recipient_verified_disabled_idx"; | ||
| DROP INDEX IF EXISTS "Recipient_nextScheduledAt_idx"; | ||
| DROP INDEX IF EXISTS "Recipient_userId_nextScheduledAt_idx"; | ||
|
|
||
| -- Update NULL schedule values to sentinel dates | ||
| -- nextScheduledAt uses far-future date (9999-12-31) - will be filtered out by the query | ||
| -- prevScheduledAt uses far-past date (1970-01-01) - indicates no previous schedule | ||
| -- This eliminates the OR ... IS NULL pattern which defeats index usage in SQLite | ||
| UPDATE "Recipient" SET "nextScheduledAt" = '9999-12-31T23:59:59.999Z' WHERE "nextScheduledAt" IS NULL; | ||
| UPDATE "Recipient" SET "prevScheduledAt" = '1970-01-01T00:00:00.000Z' WHERE "prevScheduledAt" IS NULL; | ||
|
|
||
| -- Create optimized composite index for the cron query | ||
| -- Order: equality columns first (verified, disabled), then range column (nextScheduledAt), then join column (userId) | ||
| CREATE INDEX "Recipient_cron_query_idx" ON "Recipient"("verified", "disabled", "nextScheduledAt", "userId"); | ||
|
|
||
| -- Create a partial index on User for stripeId IS NOT NULL - this helps the EXISTS subquery | ||
| CREATE INDEX "User_stripe_active_idx" ON "User"("id") WHERE "stripeId" IS NOT NULL; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.