fix(platform-links): prevent duplicate displayOrder via unique constraint and retry#556
fix(platform-links): prevent duplicate displayOrder via unique constraint and retry#556Ridanshi wants to merge 2 commits into
Conversation
|
@Ridanshi is attempting to deploy a commit to the Prashantkumar Khatri's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
Hi @Ridanshi, Thanks for opening this pull request. This PR has been automatically classified based on the files modified. Applied Labels
Primary Review Area
Reviewer@Harxhit has been identified as the primary reviewer for this pull request. If you have any questions regarding the affected area or implementation details, feel free to reach out to the assigned reviewer. Thank you for your contribution! |
CI — Checks FailedBackend — FAIL
Mobile — SKIP
Web — SKIP
Last updated: |
…ions Root cause: createPlatformLink, updatePlatformLink, deletePlatformLink, and reorderLinks all mutated the database but never called redis.del on the profile:<username> cache key, leaving stale data served to viewers until the 5-minute TTL expired naturally. Fix: Add a private invalidateProfileCacheForUser helper that resolves the username via a lightweight SELECT then calls redis.del. All four mutation functions now await this helper after a successful DB write so the cache is cleared immediately. Cache invalidation is skipped when Redis is absent and errors are caught and logged non-fatally so a Redis blip never fails a mutation request. Also fix the DELETE /api/cards/:id route handler which checked error codes as return values; the service throws errors, so the handler now catches them. Fix cards.test.ts duplicate buildApp declaration, and apply the PlatformLink type fix to cardService.ts (upstream/main has not yet merged that PR). Tests: 21 new tests in profile-cache.test.ts cover cache hit/miss lifecycle, all four mutation paths, failed mutations, non-existent links, Redis-absent mode, consecutive mutations, cache repopulation, and non-fatal Redis errors.
…aint and retry Concurrent createPlatformLink calls both read the same max(displayOrder) and insert the same value, corrupting link ordering for the user. - Add @@unique([userId, displayOrder]) to PlatformLink schema with migration - Wrap createPlatformLink in a retry loop (max 5 attempts) that re-reads max and retries on P2002 unique constraint violations - Reorder uses two-phase transaction (temp offset then final values) to avoid constraint conflicts when adjacent positions swap - Add platform-link-ordering.test.ts covering concurrency, retry, two-phase reorder, ordering integrity, and regression scenarios Closes Dev-Card#485
7f4104b to
b9591d0
Compare
|
I'm not able to understand the changes here. |
|
Thanks for the review, @ShantKhatri! Let me break this down clearly. What Changed & Why
27 new tests in platform-link-ordering.test.ts covering all the new paths (retry logic, concurrent simulation, two-phase reorder) Updated $transaction mock in profile-cache.test.ts to handle both array and callback forms (Prisma supports both) Happy to walk through any specific file in the diff if anything is still unclear! |
Summary
Fixes #485 — concurrent platform-link creation corrupts link ordering by assigning duplicate
displayOrdervalues.Root cause:
createPlatformLinkreadmax(displayOrder)and inserted in two separate operations with no transaction and no DB constraint. Two concurrent requests reading the same max would both attempt to insert the samedisplayOrder.Changes:
schema.prisma: add@@unique([userId, displayOrder])toPlatformLink; migration in20260612000000_platform_link_unique_display_order/profileService.createPlatformLink: retry loop (max 5 attempts) re-reads max on each attempt, retries onP2002unique constraint violationsprofileService.reorderLinks: two-phase interactive transaction (shift to temp offset → set final values) prevents unique constraint violations when adjacent positions swapprofile-cache.test.ts: update$transactionmock to handle both array and callback formsplatform-link-ordering.test.ts: 27 new tests covering display order assignment, P2002 retry (single, exhausted, non-P2002), concurrent simulation, two-phase reorder, ordering integrity, and regressionTest Plan
platform-link-ordering.test.ts)profile-cache.test.ts)profiles.test.ts)