Merge pull request #277 from ArnasDon/fix/017-rls-rewrite-idempotent

fix(migrations): make 017 RLS rewrite re-runnable
This commit is contained in:
Arnas Donauskas 2026-06-22 09:52:21 +03:00 committed by GitHub
commit 9d13a1d308
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -352,6 +352,35 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_one_active_run_per_contact
-- assignment + audit) but is no longer consulted for isolation.
-- ============================================================
-- Make the RLS rewrite re-runnable. CREATE POLICY has no IF NOT EXISTS
-- form, and the DROP statements below only name the *legacy* policies —
-- the new ones (contacts_select, …) would error with 42710 "policy
-- already exists" on a second run. 017 owns every policy on these tables
-- (no later migration adds others), so drop them all first, then the
-- CREATEs below re-establish the full set.
DO $$
DECLARE
pol RECORD;
BEGIN
FOR pol IN
SELECT policyname, tablename
FROM pg_policies
WHERE schemaname = 'public'
AND tablename = ANY (ARRAY[
'contacts', 'tags', 'custom_fields', 'contact_notes',
'conversations', 'whatsapp_config', 'message_templates',
'pipelines', 'deals', 'broadcasts', 'automations',
'automation_logs', 'flows', 'flow_runs', 'contact_tags',
'contact_custom_values', 'messages', 'pipeline_stages',
'broadcast_recipients', 'automation_steps', 'flow_nodes',
'flow_run_events', 'message_reactions', 'profiles',
'accounts', 'account_invitations'
])
LOOP
EXECUTE format('DROP POLICY IF EXISTS %I ON public.%I', pol.policyname, pol.tablename);
END LOOP;
END $$;
-- ---- contacts ---------------------------------------------------
DROP POLICY IF EXISTS "Users can manage own contacts" ON contacts;
CREATE POLICY contacts_select ON contacts FOR SELECT USING (is_account_member(account_id));