You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(supabase): tolerate owner-mismatch on smtp_senders and whatsapp_sessions (#2838)
The QA DB has these tables pre-applied by a role that doesn't match
the migration runner, so CREATE INDEX and CREATE POLICY fail with
'must be owner of table' before IF NOT EXISTS can even run. Wrap
the owner-required DDL in DO blocks that catch insufficient_privilege
and skip silently.
Trade-off: when the runner isn't the owner, the index/policy may be
missing on QA after the migration. Acceptable because whoever
pre-applied the table should have created them too; on a fresh env
the migration applies correctly.
Verified locally against supabase_db_leadminer (PG 15.8): ran the
fixed migration against a pre-applied state, DO blocks apply cleanly
(duplicate_object) and would also catch insufficient_privilege on a
non-superuser runner (local postgres is superuser so we can't
perfectly simulate that, but the EXCEPTION condition matches the
error from the failed QA run).
Copy file name to clipboardExpand all lines: supabase/migrations/20260601000000_add_whatsapp_gateway.sql
+10-22Lines changed: 10 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -32,33 +32,21 @@ CREATE TABLE IF NOT EXISTS private.whatsapp_sessions (
32
32
);
33
33
34
34
-- Indexes
35
-
CREATEINDEXIF NOT EXISTS idx_whatsapp_sessions_user_id ONprivate.whatsapp_sessions(user_id);
36
-
CREATEINDEXIF NOT EXISTS idx_whatsapp_sessions_status ONprivate.whatsapp_sessions(status);
37
-
CREATEINDEXIF NOT EXISTS idx_whatsapp_sessions_active ONprivate.whatsapp_sessions(is_active);
35
+
-- CREATE INDEX / CREATE POLICY on an existing table require ownership.
36
+
-- On QA the table may have been pre-applied by a role that doesn't match
37
+
-- the migration runner, so these no-op cleanly when we lack privilege.
38
+
DO $$ BEGIN CREATE INDEX idx_whatsapp_sessions_user_id ONprivate.whatsapp_sessions(user_id); EXCEPTION WHEN insufficient_privilege OR duplicate_table THEN NULL; END $$;
39
+
DO $$ BEGIN CREATE INDEX idx_whatsapp_sessions_status ONprivate.whatsapp_sessions(status); EXCEPTION WHEN insufficient_privilege OR duplicate_table THEN NULL; END $$;
40
+
DO $$ BEGIN CREATE INDEX idx_whatsapp_sessions_active ONprivate.whatsapp_sessions(is_active); EXCEPTION WHEN insufficient_privilege OR duplicate_table THEN NULL; END $$;
DROP POLICY IF EXISTS "Users can view own whatsapp sessions"ONprivate.whatsapp_sessions;
44
-
CREATE POLICY "Users can view own whatsapp sessions"
45
-
ONprivate.whatsapp_sessions FOR SELECT
46
-
USING (auth.uid() = user_id);
47
-
48
-
DROP POLICY IF EXISTS "Users can insert own whatsapp sessions"ONprivate.whatsapp_sessions;
49
-
CREATE POLICY "Users can insert own whatsapp sessions"
50
-
ONprivate.whatsapp_sessions FOR INSERT
51
-
WITH CHECK (auth.uid() = user_id);
52
-
53
-
DROP POLICY IF EXISTS "Users can update own whatsapp sessions"ONprivate.whatsapp_sessions;
54
-
CREATE POLICY "Users can update own whatsapp sessions"
55
-
ONprivate.whatsapp_sessions FOR UPDATE
56
-
USING (auth.uid() = user_id);
57
-
58
-
DROP POLICY IF EXISTS "Users can delete own whatsapp sessions"ONprivate.whatsapp_sessions;
59
-
CREATE POLICY "Users can delete own whatsapp sessions"
60
-
ONprivate.whatsapp_sessions FOR DELETE
61
-
USING (auth.uid() = user_id);
46
+
DO $$ BEGIN EXECUTE 'DROP POLICY IF EXISTS "Users can view own whatsapp sessions" ON private.whatsapp_sessions'; EXECUTE 'CREATE POLICY "Users can view own whatsapp sessions" ON private.whatsapp_sessions FOR SELECT USING (auth.uid() = user_id)'; EXCEPTION WHEN insufficient_privilege OR duplicate_object THEN NULL; END $$;
47
+
DO $$ BEGIN EXECUTE 'DROP POLICY IF EXISTS "Users can insert own whatsapp sessions" ON private.whatsapp_sessions'; EXECUTE 'CREATE POLICY "Users can insert own whatsapp sessions" ON private.whatsapp_sessions FOR INSERT WITH CHECK (auth.uid() = user_id)'; EXCEPTION WHEN insufficient_privilege OR duplicate_object THEN NULL; END $$;
48
+
DO $$ BEGIN EXECUTE 'DROP POLICY IF EXISTS "Users can update own whatsapp sessions" ON private.whatsapp_sessions'; EXECUTE 'CREATE POLICY "Users can update own whatsapp sessions" ON private.whatsapp_sessions FOR UPDATE USING (auth.uid() = user_id)'; EXCEPTION WHEN insufficient_privilege OR duplicate_object THEN NULL; END $$;
49
+
DO $$ BEGIN EXECUTE 'DROP POLICY IF EXISTS "Users can delete own whatsapp sessions" ON private.whatsapp_sessions'; EXECUTE 'CREATE POLICY "Users can delete own whatsapp sessions" ON private.whatsapp_sessions FOR DELETE USING (auth.uid() = user_id)'; EXCEPTION WHEN insufficient_privilege OR duplicate_object THEN NULL; END $$;
62
50
63
51
-- Trigger to automatically update updated_at
64
52
CREATE OR REPLACEFUNCTIONprivate.update_whatsapp_session_updated_at()
0 commit comments