forked from Dev-Card/DevCard
-
Notifications
You must be signed in to change notification settings - Fork 0
fix(platform-links): prevent duplicate displayOrder via unique constraint and retry #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Ridanshi
wants to merge
3
commits into
Harxhit:main
from
Ridanshi:fix/platform-link-display-order-race
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
82a256c
feat: implement GitHub platform auto-discovery from GitHub API (#506)
udaycodespace 6c6a2aa
fix(cache): invalidate public profile cache after platform link mutat…
Ridanshi b9591d0
fix(platform-links): prevent duplicate displayOrder via unique constr…
Ridanshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
...backend/prisma/migrations/20260612000000_platform_link_unique_display_order/migration.sql
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| -- Add unique constraint on (user_id, display_order) to prevent duplicate display orders per user | ||
| CREATE UNIQUE INDEX "platform_links_user_id_display_order_key" ON "platform_links"("user_id", "display_order"); |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import cookie from '@fastify/cookie'; | ||
| import jwt from '@fastify/jwt'; | ||
| import Fastify from 'fastify'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { authRoutes } from '../routes/auth.js'; | ||
|
|
||
| import type { PrismaClient } from '@prisma/client'; | ||
|
|
||
| const mockUser = { | ||
| id: 'user-123', | ||
| username: 'devcard-demo', | ||
| }; | ||
|
|
||
| const prismaMock = { | ||
| user: { | ||
| findUnique: vi.fn(), | ||
| }, | ||
| }; | ||
|
|
||
| async function buildApp(nodeEnv: string) { | ||
| vi.stubEnv('NODE_ENV', nodeEnv); | ||
|
|
||
| const app = Fastify(); | ||
| await app.register(jwt, { secret: 'test-secret' }); | ||
| await app.register(cookie); | ||
| app.decorate('prisma', prismaMock as unknown as PrismaClient); | ||
| app.decorate('authenticate', async () => {}); | ||
| await app.register(authRoutes, { prefix: '/auth' }); | ||
| await app.ready(); | ||
| return app; | ||
| } | ||
|
|
||
| describe('auth dev-login route registration', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllEnvs(); | ||
| }); | ||
|
|
||
| it('registers /auth/dev-login outside production', async () => { | ||
| prismaMock.user.findUnique.mockResolvedValue(mockUser); | ||
| const app = await buildApp('development'); | ||
|
|
||
| const res = await app.inject({ | ||
| method: 'POST', | ||
| url: '/auth/dev-login', | ||
| }); | ||
|
|
||
| expect(res.statusCode).toBe(200); | ||
| expect(res.json()).toHaveProperty('token'); | ||
| expect(prismaMock.user.findUnique).toHaveBeenCalledWith({ | ||
| where: { username: 'devcard-demo' }, | ||
| }); | ||
|
|
||
| await app.close(); | ||
| }); | ||
|
|
||
| it('does not register /auth/dev-login in production', async () => { | ||
| const app = await buildApp('production'); | ||
|
|
||
| const res = await app.inject({ | ||
| method: 'POST', | ||
| url: '/auth/dev-login', | ||
| }); | ||
|
|
||
| expect(res.statusCode).toBe(404); | ||
| expect(prismaMock.user.findUnique).not.toHaveBeenCalled(); | ||
|
|
||
| await app.close(); | ||
| }); | ||
|
|
||
| it('keeps other auth routes registered in production', async () => { | ||
| const app = await buildApp('production'); | ||
|
|
||
| const res = await app.inject({ | ||
| method: 'POST', | ||
| url: '/auth/logout', | ||
| }); | ||
|
|
||
| expect(res.statusCode).toBe(200); | ||
| expect(res.json()).toMatchObject({ message: 'Logged out' }); | ||
|
|
||
| await app.close(); | ||
| }); | ||
| }); | ||
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: Adding
@@unique([userId, displayOrder])without first deduplicating existing duplicate rows in the migration will cause PostgreSQL to rejectCREATE UNIQUE INDEXif duplicates exist. The migration must include a pre-step to remediate historical duplicates (e.g., assign sequential display orders per user) before creating the unique constraint.Prompt for AI agents