fix(db): prevent user team change when tickets reference the user#283
fix(db): prevent user team change when tickets reference the user#283g-k-s-03 wants to merge 2 commits into
Conversation
|
Warning Review limit reached
More reviews will be available in 43 minutes and 45 seconds. Learn how PR review limits work. Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file). ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughA new Supabase migration adds a PL/pgSQL trigger function ChangesPrevent user team change when tickets exist
Estimated code review effort🎯 2 (Simple) | ⏱️ ~5 minutes Poem
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@supabase/migrations/20251021110000_prevent_user_team_change_trigger.sql`:
- Around line 6-11: The trigger prevents team changes by checking if a user is
referenced in tickets, but this check has a race condition where another
transaction can insert a ticket between the EXISTS check and the UPDATE commit.
To fix this, add a BEFORE INSERT or BEFORE UPDATE trigger on the tickets table
that validates the team consistency before allowing any ticket insertion or
update. Specifically, the new trigger should ensure that when created_by or
assigned_to are set on a ticket, the referenced user's team_id matches the
ticket's team_id. This enforcement at the source (tickets table) will prevent
concurrent inserts from bypassing the original team-change prevention logic by
making it impossible to create references to users in different teams in the
first place.
- Around line 2-3: The trigger function
prevent_user_team_change_if_tickets_exist() uses unqualified relation names and
function references that depend on search_path for resolution, which creates a
security and correctness risk. Qualify all table references (tickets and users
on lines 7, 18, and 20) with their schema prefix (e.g., public.tickets,
public.users) and ensure any function calls within the trigger are also
schema-qualified. This ensures the trigger operates on the intended tables
regardless of how search_path is configured.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 15c6fc40-c6c6-44e5-b38d-e9211ada689f
📒 Files selected for processing (1)
supabase/migrations/20251021110000_prevent_user_team_change_trigger.sql
… consistency check
|
@M4dhav, can you see this pr |
|
Hey @g-k-s-03 , It would be better for @dhruvi-16-me to take a look at it first. |
Fixes #237
Problem
No database-level enforcement existed to prevent a user's
team_idfrombeing changed while tickets still reference them as
created_byorassigned_to. This caused silent cross-team data integrity violations.Root Cause
Verified across all 10 migration files — no BEFORE UPDATE trigger on the
userstable and no constraint preventingteam_idchanges for referenced users.Fix
Added migration
20251021110000_prevent_user_team_change_trigger.sql:prevent_user_team_change_if_tickets_exist()trigger functionBEFORE UPDATE ON usersfor each rowOLD.team_id IS DISTINCT FROM NEW.team_idcreated_byorassigned_toTesting
created_byteam_idto team BSummary by CodeRabbit