Skip to content

Commit 7f4104b

Browse files
committed
fix(platform-links): prevent duplicate displayOrder via unique constraint 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 #485
1 parent fe45ee6 commit 7f4104b

5 files changed

Lines changed: 536 additions & 24 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Add unique constraint on (user_id, display_order) to prevent duplicate display orders per user
2+
CREATE UNIQUE INDEX "platform_links_user_id_display_order_key" ON "platform_links"("user_id", "display_order");

apps/backend/prisma/schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ model PlatformLink {
5151
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
5252
cardLinks CardLink[]
5353
54+
@@unique([userId, displayOrder])
5455
@@map("platform_links")
5556
}
5657

0 commit comments

Comments
 (0)