Skip to content

fix : use upsert with onConflict to handle race condition in streak freeze#832

Open
tmdeveloper007 wants to merge 2 commits into
Priyanshu-byte-coder:mainfrom
tmdeveloper007:#808
Open

fix : use upsert with onConflict to handle race condition in streak freeze#832
tmdeveloper007 wants to merge 2 commits into
Priyanshu-byte-coder:mainfrom
tmdeveloper007:#808

Conversation

@tmdeveloper007
Copy link
Copy Markdown
Contributor

fix : use upsert with onConflict to handle race condition in streak freeze

@vercel
Copy link
Copy Markdown

vercel Bot commented May 23, 2026

@TESTPERSONAL is attempting to deploy a commit to the PRIYANSHU DOSHI's projects Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added gssoc26 GSSoC 2026 contribution type:bug GSSoC type bonus: bug fix type:testing GSSoC type bonus: tests (+10 pts) labels May 23, 2026
@github-actions
Copy link
Copy Markdown

GSSoC Label Checklist 🏷️

@Priyanshu-byte-coder — please apply the appropriate labels before merging:

Difficulty (pick one):

  • level:beginner — 20 pts
  • level:intermediate — 35 pts
  • level:advanced — 55 pts
  • level:critical — 80 pts

Quality (optional):

  • quality:clean — ×1.2 multiplier
  • quality:exceptional — ×1.5 multiplier

Validation (required to score):

  • gssoc:approved — counts for points
  • gssoc:invalid / gssoc:spam / gssoc:ai-slop — does not score

Type labels (type:*) are auto-detected from files and title. Review and adjust if needed.
Points formula: (difficulty × quality_multiplier) + type_bonus

Copy link
Copy Markdown
Owner

@Priyanshu-byte-coder Priyanshu-byte-coder left a comment

Choose a reason for hiding this comment

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

The upsert correctly eliminates the race condition, but removes the 409 response that clients relied on to detect 'already have an unused freeze'. Now both new and duplicate freezes return 200 — callers can no longer distinguish them. Add a flag to the response (e.g. { ...record, already_existed: boolean }) or restore the 409 for the duplicate case.

@Priyanshu-byte-coder
Copy link
Copy Markdown
Owner

The upsert approach is correct for handling race conditions, but removing the 409 response removes user feedback.

Issue: The previous code returned a helpful 409 Conflict with message "You already have an unused streak freeze." when a duplicate was attempted. After your change, both a race-condition duplicate and a genuine DB error both return 500 Failed to apply freeze.

Please restore the 409 path. With upsert, you can check if the returned row's created_at matches the original insert (or simply keep a flag), but the simplest fix is: if upsert succeeds and returns data, treat it as success — but if no error occurs and data is returned, check whether it was an insert or an existing row by comparing with today's date against any existing freeze for the user. Alternatively, just restore the 409 for upsert no-ops:

// After upsert succeeds, check if freeze already existed
if (!error && freeze) {
  // upsert always succeeds — but if freeze_date already existed, it's a no-op update
  // to distinguish: query streak_freezes for this user before upsert, or use insert + catch 23505
}

The simplest approach is to keep insert and return 409 on error code 23505 (which your original code did correctly). The race condition upsert is fine, but users should still see the "already frozen" message.

@Priyanshu-byte-coder Priyanshu-byte-coder added gssoc:approved GSSoC: PR approved for scoring level:intermediate GSSoC: Intermediate difficulty (35 pts) labels May 23, 2026
@vercel
Copy link
Copy Markdown

vercel Bot commented May 23, 2026

Deployment failed with the following error:

The provided GitHub repository does not contain the requested branch or commit reference. Please ensure the repository is not empty.

@tmdeveloper007
Copy link
Copy Markdown
Contributor Author

This pull request is fully up-to-date with the latest upstream merges, all review items are addressed, local tests are passing cleanly, and it is fully ready to be merged! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc:approved GSSoC: PR approved for scoring gssoc26 GSSoC 2026 contribution level:intermediate GSSoC: Intermediate difficulty (35 pts) type:bug GSSoC type bonus: bug fix type:testing GSSoC type bonus: tests (+10 pts)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants