[Backend][fix] - FK constraint on docker image starts#1093
Conversation
… image would not see this FK issue.
Older cmux preview screenshots (latest comment is below)Preview ScreenshotsOpen Workspace (1 hr expiry) · Open Dev Browser (1 hr expiry) · Open Diff Heatmap Screenshot capture was skipped.
Generated by cmux preview system |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
📝 WalkthroughWalkthroughAdds an idempotent TeamMember upsert and moves the permission grant into the same Changes
Sequence Diagram(s)(omitted) Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
📜 Recent review detailsConfiguration used: defaults Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🧰 Additional context used📓 Path-based instructions (4)**/*.{tsx,ts,jsx,js}📄 CodeRabbit inference engine (AGENTS.md)
Files:
**/*.{tsx,ts}📄 CodeRabbit inference engine (AGENTS.md)
Files:
**/*.{ts,tsx,js,jsx}📄 CodeRabbit inference engine (AGENTS.md)
Files:
**/*.{ts,tsx}📄 CodeRabbit inference engine (AGENTS.md)
Files:
🧬 Code graph analysis (1)apps/backend/prisma/seed.ts (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (13)
🔇 Additional comments (2)
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 |
There was a problem hiding this comment.
Greptile Overview
Greptile Summary
This PR adds a TeamMember upsert before granting team permissions to fix a foreign key constraint error that occurs when the STACK_SEED_INTERNAL_PROJECT_USER_INTERNAL_ACCESS setting changes from false to true between container restarts.
Key changes:
- Adds
TeamMemberupsert (lines 387-403) whenadminInternalAccess=trueto ensure the record exists before callinggrantTeamPermission - Handles idempotency by using upsert with empty update clause
Critical issue found:
- The
grantTeamPermissioncall (lines 405-410) is not wrapped in anif (adminInternalAccess)check, which means it will still fail with a FK constraint error whenadminInternalAccess=false(since noTeamMemberrecord exists in that case)
Confidence Score: 3/5
- This PR partially fixes the issue but introduces incomplete logic that can still fail
- The PR correctly identifies and addresses the FK constraint issue when internal access changes from false to true, but the fix is incomplete. The grantTeamPermission call should be wrapped in an adminInternalAccess check to prevent FK errors when adminInternalAccess=false
- apps/backend/prisma/seed.ts needs the grantTeamPermission call wrapped in adminInternalAccess check
Important Files Changed
File Analysis
| Filename | Score | Overview |
|---|---|---|
| apps/backend/prisma/seed.ts | 3/5 | Adds TeamMember upsert before granting permissions, fixing FK constraint when internal access changes from false to true. However, grantTeamPermission should be wrapped in adminInternalAccess check. |
Sequence Diagram
sequenceDiagram
participant Seed as Seed Script
participant DB as Database
participant Check as Admin Config
Seed->>Check: Check if admin credentials provided
alt Admin credentials exist
Seed->>DB: Query for existing admin user
alt User already exists
Note over Seed,DB: User exists from previous run
Seed->>Check: Check adminInternalAccess=true
alt adminInternalAccess is true
Seed->>DB: Upsert TeamMember
Note over DB: Ensures TeamMember exists<br/>before granting permissions
DB-->>Seed: TeamMember exists
end
Seed->>DB: grantTeamPermission (TeamMemberDirectPermission)
Note over Seed,DB: ⚠️ ISSUE: Not wrapped in<br/>adminInternalAccess check
DB-->>Seed: Permission granted or FK error
else User does not exist
Seed->>DB: Create ProjectUser
DB-->>Seed: User created
Seed->>Check: Check adminInternalAccess=true
alt adminInternalAccess is true
Seed->>DB: Create TeamMember
DB-->>Seed: TeamMember created
end
Seed->>Check: Check adminInternalAccess=true (NEW)
alt adminInternalAccess is true
Seed->>DB: Upsert TeamMember
DB-->>Seed: TeamMember ensured
end
Seed->>DB: grantTeamPermission
Note over Seed,DB: ⚠️ ISSUE: If adminInternalAccess=false,<br/>no TeamMember exists → FK error
DB-->>Seed: Permission granted or FK error
end
end
Additional Comments (1)
When Prompt To Fix With AIThis is a comment left during a code review.
Path: apps/backend/prisma/seed.ts
Line: 405:410
Comment:
Wrap this in `if (adminInternalAccess)` block to prevent FK constraint errors.
When `adminInternalAccess=false`, no `TeamMember` record exists, so `grantTeamPermission` will fail with a foreign key constraint error (since `TeamMemberDirectPermission` has an FK to `TeamMember`).
```suggestion
if (adminInternalAccess) {
await grantTeamPermission(internalPrisma, {
tenancy: internalTenancy,
teamId: internalTeamId,
userId: defaultUserId,
permissionId: "team_admin",
});
}
```
How can I resolve this? If you propose a fix, please make it concise. |
N2D4
left a comment
There was a problem hiding this comment.
isn't this duplicated logic now?
N2D4
left a comment
There was a problem hiding this comment.
ff to merge after fixing the duplicated logic
Older cmux preview screenshots (latest comment is below)Preview Screenshots⏳ Preview screenshots are being captured... Workspace and dev browser links will appear here once the preview environment is ready. Generated by cmux preview system |
Preview ScreenshotsOpen Workspace (1 hr expiry) · Open Dev Browser (1 hr expiry) · Open Diff Heatmap Screenshot capture was skipped.
Generated by cmux preview system |
Foreign Key Constraint
When deploying Stack Auth with Docker and changing
STACK_SEED_INTERNAL_PROJECT_USER_INTERNAL_ACCESSbetween container restarts, the seed script fails with:This is a bug in the seed script's idempotency logic. The issue occurs in
apps/backend/prisma/seed.ts(lines 296–388):grantTeamPermission()call at line 382 is outside the if/else block and always runsSTACK_SEED_INTERNAL_PROJECT_USER_INTERNAL_ACCESS=falsepreviously, or the TeamMember was never created), the foreign key constraint fails.How could this happen?
INTERNAL_ACCESSsetting: First run withSTACK_SEED_INTERNAL_PROJECT_USER_INTERNAL_ACCESS=false(user created, no TeamMember), then restarted with=trueThe most likely scenario would be 1 here:
Scenario 1:
STACK_SEED_INTERNAL_PROJECT_USER_INTERNAL_ACCESS=falseadminInternalAccess=false)STACK_SEED_INTERNAL_PROJECT_USER_INTERNAL_ACCESS=truegrantTeamPermission()called → tries to create TeamMemberDirectPermissionSolution
Add a
TeamMemberupsert before granting permissions whenadminInternalAccessis true:This ensures the
TeamMemberrecord exists before `grantTeamPermission() is called, regardless of whether the user was just created or already existed.Impact
TeamMemberalready exists, the upsert does nothing.Testing
Tested by building a local Docker image and running the reproduction script that:
INTERNAL_ACCESS=falseINTERNAL_ACCESS=trueSummary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.