Fix submissions count token join#54
Merged
Merged
Conversation
Contributor
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
pinchbench-api | 23cbcbc | Commit Preview URL Branch Preview URL |
Jun 08 2026, 02:20 PM |
Contributor
There was a problem hiding this comment.
Pull request overview
Optimizes the hot /api/submissions pagination COUNT(*) query by avoiding an unnecessary tokens join when the request is not filtering for verified=true, reducing D1 read work while preserving response shape for the data query.
Changes:
- Make the
/api/submissionsCOUNT query conditionally jointokensonly whenverified=truerequirest.claimed_at. - Add regression tests covering COUNT SQL shape (join/no-join) and parity vs the legacy joined-count behavior.
- Add CI coverage via a minimal GitHub Actions workflow and synchronize
package-lock.jsonto include the existingvitestdev dependency.
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/routes/submissions.ts |
Skips tokens join in the COUNT query unless verified=true requires t.claimed_at. |
src/routes/submissions.test.ts |
Adds fixture-backed tests asserting COUNT query join behavior and result parity/regressions. |
package-lock.json |
Updates lockfile to align with vitest in package.json for clean npm ci installs. |
.github/workflows/test.yml |
Adds CI workflow to run npm ci and npm test on PRs and main pushes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
The public
/api/submissionspaginated list runs a data query plus a paginationCOUNT(*)query. The COUNT path reused the data query'stokensjoin even when the request was not filtering byverified=true, adding unnecessary D1 read work on a hot read-heavy endpoint.The observed production pattern was heavily read-biased: 912 visitors produced 76K requests, 77K API calls, 299K D1 queries, and 548M rows read, with only 104 rows written.
Root Cause
src/routes/submissions.tsalways joinedtokensin the/api/submissionsCOUNT query:tokensto projectclaimed.tokenswhen applyingAND t.claimed_at IS NOT NULLforverified=true.verified=falselist requests, the join was redundant becausesubmissions.token_idis non-null and backed by the token FK.Solution
JOIN tokens t ON s.token_id = t.idonly whenverified=true.claimedprojection stay identical.package-lock.jsonwith the existingvitestdev dependency so clean CI installs can run tests.npm ciandnpm testfor PRs.Test Coverage
Added
src/routes/submissions.test.tswith a production-equivalent D1 fixture covering:/api/submissionsresponse shape and total count against the previous joined-count behavior.tokenswhent.claimed_atis required.verified=false, official-only, and model/provider filtered unverified COUNT variants do not reintroduce a token join.Validated locally:
npm ci && npm testnpm testgit diff --checkMetrics Impact
Expected impact is scoped to
/api/submissionspagination COUNT queries:tokensjoin from every default or otherwise unverified/api/submissionsCOUNT request./api/submissionstraffic that is notverified=true.Risks & Mitigations
submissions.token_idrows exist.token_id TEXT NOT NULLwithFOREIGN KEY (token_id) REFERENCES tokens(id), and submission creation validates tokens before insert.verified=true.