fix(actions): use upsert/deleteMany in toggleStarMarked to prevent constraint errors - #520
Conversation
…nstraint errors db.starMark.create throws a P2002 unique constraint violation when the user clicks star twice rapidly, because the first click already inserted the row. db.starMark.delete throws P2025 when the record does not exist (unstar when already unstarred). Replace create with upsert (idempotent on repeated star clicks) and delete with deleteMany (no-op when no matching row exists). Applied to both the playground and dashboard action modules. Closes piyushdotcomm#518
|
Warning Review limit reached
Next review available in: 16 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
👋 Thanks for opening a PR, @Xenon010101!Your PR has entered the 🚦 PR Review Pipeline.
What happens next
A pipeline status comment will appear below and update automatically as your PR progresses. While you wait
This comment is posted only once. |
PR Summary by QodoFix star toggle race: use upsert/deleteMany to avoid Prisma constraint errors
AI Description
Diagram
High-Level Assessment
Files changed (2)
|
What changed
Replaced
db.starMark.create/deletewithdb.starMark.upsert/deleteManyintoggleStarMarkedacross both the playground and dashboard action modules.Closes #518
Why
db.starMark.createthrows aP2002unique constraint violation when a user clicks the star button twice in rapid succession, because the first click already inserted the row with the same@@unique([userId, playgroundId])key. The error is caught and returned as{ success: false }, but the user gets no meaningful feedback — the toggle just silently fails.The inverse path has the same problem:
db.starMark.deletethrowsP2025(record not found) when unstarring a playground that was already unstarred.Fix
create→upsertwithupdate: { isMarked: true }— second click is a no-op, no constraint error.delete→deleteMany— no-op when no matching row exists, no P2025 error.Applied to both
modules/playground/actions/index.tsandmodules/dashboard/actions/index.tswhich had identical copies of the bug.Verification
npm run lint— 0 errorsnpm test— 112/112 tests passnpm run build— builds successfully