Commit 2ccfa2f
authored
fix(supabase): resolve delete-user FK violation + remove duplicate migration (#2841)
* fix(supabase): add ON DELETE CASCADE on smtp_senders FK and update delete_user_data RPC
The delete-user edge function failed with FK violation on
smtp_senders_mining_source_id_fkey because:
- smtp_senders.mining_source_id FK had no ON DELETE CASCADE
- private.delete_user_data RPC did not delete from smtp_senders
before mining_sources
This migration (1) adds ON DELETE CASCADE to the FK and
(2) updates the RPC body to explicitly clean up all user-owned
tables added since 2025-01-16, making the RPC self-documenting
and resilient to future FK additions.
* fix(supabase): correct broken DELETEs in delete_user_data RPC
The migration added in decc735 had 4 bugs in the SMS cleanup block
that would break the RPC at execution time:
1. sms_campaign_unsubscribes used non-existent recipient_id column.
The table only has user_id, phone, and campaign_id. Replaced with
a direct WHERE user_id = owner_id filter.
2. sms_campaign_link_clicks USING-joined private.sms_campaign_recipients
on r.user_id, but sms_campaign_recipients has no user_id column.
Re-routed the join through private.sms_campaigns (which has user_id
and CASCADEs down to recipients/link_clicks).
3. sms_campaign_recipient_gateways had the same broken r.user_id
join. Re-routed through sms_campaigns the same way.
4. DELETE FROM sms_campaign_recipients WHERE user_id = owner_id would
trigger plpgsql parameter substitution (no such column exists),
producing WHERE $1 = owner_id (always true) and silently deleting
every row in the table across all users. Re-routed through
sms_campaigns.
All re-routed deletes are technically redundant (sms_campaigns has
ON DELETE CASCADE down the chain), but kept explicit to match the
original RPC's defensive style. sms_campaign_unsubscribes is the
only SMS table whose campaign_id FK is ON DELETE SET NULL, so it
genuinely needs the explicit user_id delete.
* fix(supabase): alias tables in delete_user_data to avoid plpgsql param shadowing
The unqualified 'user_id' references in the function body would
resolve to the function parameter (not the column) in plpgsql
since plpgsql.variable_conflict defaults to 'error' and the
search order is variables > columns. This would have caused
the DELETE to compare the parameter to itself (always true or
column-not-found), deleting rows belonging to other users.
Add explicit table aliases to all new DELETEs, matching the
pattern already used in the original RPC body for messages,
tags, mining_sources, and engagement.
* docs(supabase): note CASCADE side effect for manual mining-source deletion
* fix(supabase): remove duplicate sms_campaigns migration
Two migrations with identical content (587 bytes each) caused
npx supabase db reset to fail. The 20260311000001 version runs
before private.sms_campaigns is created (in 20260317000000),
producing:
ERROR: relation "private.sms_campaigns" does not exist
The 20260328100000 version runs at the correct position. Keep
the 20260328100000 version (md5 matches).1 parent b06b4ea commit 2ccfa2f
2 files changed
Lines changed: 75 additions & 16 deletions
Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 75 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
0 commit comments