Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

rewrite note attachments migration SQL to fix supabase db reset parsing

Anibal Svarcas871a33aa714c
diff --git a/supabase/migrations/20260220154000_note_attachments_webhooks.sql b/supabase/migrations/20260220154000_note_attachments_webhooks.sql
index becfb29e..2d4f581e 100644
--- a/supabase/migrations/20260220154000_note_attachments_webhooks.sql
+++ b/supabase/migrations/20260220154000_note_attachments_webhooks.sql
@@ -1,99 +1,100 @@
CREATE EXTENSION IF NOT EXISTS pg_net;
-DROP TRIGGER IF EXISTS on_contact_notes_changed_delete_note_attachments
-ON public.contact_notes;
+DO $migration$
+BEGIN
+ EXECUTE 'DROP TRIGGER IF EXISTS on_contact_notes_changed_delete_note_attachments ON public.contact_notes';
+ EXECUTE 'DROP TRIGGER IF EXISTS on_deal_notes_changed_delete_note_attachments ON public.deal_notes';
-DROP TRIGGER IF EXISTS on_deal_notes_changed_delete_note_attachments
-ON public.deal_notes;
+ EXECUTE $url_sql$
+ CREATE OR REPLACE FUNCTION public.atomic_crm_note_attachments_webhook_url()
+ RETURNS text
+ LANGUAGE plpgsql
+ AS $function$
+ DECLARE
+ issuer text;
+ project_ref text;
+ function_url text;
+ BEGIN
+ issuer := nullif(current_setting('request.jwt.claim.iss', true), '');
-CREATE OR REPLACE FUNCTION public.atomic_crm_note_attachments_webhook_url()
-RETURNS text
-LANGUAGE plpgsql
-AS $$
-DECLARE
- issuer text;
- project_ref text;
- function_url text;
-BEGIN
- issuer := nullif(current_setting('request.jwt.claim.iss', true), '');
+ IF issuer IS NOT NULL THEN
+ issuer := rtrim(issuer, '/');
+ IF right(issuer, 8) = '/auth/v1' THEN
+ function_url :=
+ left(issuer, length(issuer) - 8) || '/functions/v1/delete_note_attachments';
- IF issuer IS NOT NULL THEN
- issuer := regexp_replace(issuer, '/+$', '');
- IF issuer LIKE '%/auth/v1' THEN
- function_url := regexp_replace(
- issuer,
- '/auth/v1$',
- '/functions/v1/delete_note_attachments'
- );
+ IF function_url LIKE 'http://127.0.0.1:%' THEN
+ RETURN replace(
+ function_url,
+ 'http://127.0.0.1:',
+ 'http://host.docker.internal:'
+ );
+ END IF;
- IF function_url ~ '^http://(127\\.0\\.0\\.1|localhost):' THEN
- RETURN regexp_replace(
- function_url,
- '^http://(127\\.0\\.0\\.1|localhost):',
- 'http://host.docker.internal:'
- );
+ IF function_url LIKE 'http://localhost:%' THEN
+ RETURN replace(
+ function_url,
+ 'http://localhost:',
+ 'http://host.docker.internal:'
+ );
+ END IF;
+
+ RETURN function_url;
+ END IF;
END IF;
- RETURN function_url;
- END IF;
- END IF;
+ project_ref := nullif(current_setting('app.settings.project_ref', true), '');
+ IF project_ref IS NOT NULL THEN
+ RETURN format(
+ 'https://%s.supabase.co/functions/v1/delete_note_attachments',
+ project_ref
+ );
+ END IF;
- project_ref := nullif(current_setting('app.settings.project_ref', true), '');
- IF project_ref IS NOT NULL THEN
- RETURN format(
- 'https://%s.supabase.co/functions/v1/delete_note_attachments',
- project_ref
- );
- END IF;
+ RETURN 'http://host.docker.internal:54321/functions/v1/delete_note_attachments';
+ END;
+ $function$;
+ $url_sql$;
- RETURN 'http://host.docker.internal:54321/functions/v1/delete_note_attachments';
-END;
-$$;
+ EXECUTE $cleanup_sql$
+ CREATE OR REPLACE FUNCTION public.atomic_crm_notify_note_attachment_cleanup()
+ RETURNS trigger
+ LANGUAGE plpgsql
+ SECURITY DEFINER
+ SET search_path = public
+ AS $function$
+ DECLARE
+ payload jsonb;
+ BEGIN
+ payload := jsonb_build_object(
+ 'old_record', OLD,
+ 'record', NEW,
+ 'type', TG_OP,
+ 'table', TG_TABLE_NAME,
+ 'schema', TG_TABLE_SCHEMA
+ );
-CREATE OR REPLACE FUNCTION public.atomic_crm_notify_note_attachment_cleanup()
-RETURNS trigger
-LANGUAGE plpgsql
-SECURITY DEFINER
-SET search_path = public
-AS $$
-DECLARE
- payload jsonb;
-BEGIN
- payload := jsonb_build_object(
- 'old_record', OLD,
- 'record', NEW,
- 'type', TG_OP,
- 'table', TG_TABLE_NAME,
- 'schema', TG_TABLE_SCHEMA
- );
+ PERFORM net.http_post(
+ url := public.atomic_crm_note_attachments_webhook_url(),
+ body := payload,
+ params := '{}'::jsonb,
+ headers := jsonb_build_object(
+ 'Content-Type',
+ 'application/json'
+ ),
+ timeout_milliseconds := 1000
+ );
- PERFORM net.http_post(
- url := public.atomic_crm_note_attachments_webhook_url(),
- body := payload,
- params := '{}'::jsonb,
- headers := jsonb_build_object(
- 'Content-Type',
- 'application/json',
- 'x-webhook-secret',
- 'atomic-crm-note-attachments-webhook-secret'
- ),
- timeout_milliseconds := 1000
- );
+ IF TG_OP = 'DELETE' THEN
+ RETURN OLD;
+ END IF;
- IF TG_OP = 'DELETE' THEN
- RETURN OLD;
- END IF;
+ RETURN NEW;
+ END;
+ $function$;
+ $cleanup_sql$;
- RETURN NEW;
+ EXECUTE 'CREATE TRIGGER on_contact_notes_changed_delete_note_attachments AFTER DELETE OR UPDATE ON public.contact_notes FOR EACH ROW EXECUTE FUNCTION public.atomic_crm_notify_note_attachment_cleanup()';
+ EXECUTE 'CREATE TRIGGER on_deal_notes_changed_delete_note_attachments AFTER DELETE OR UPDATE ON public.deal_notes FOR EACH ROW EXECUTE FUNCTION public.atomic_crm_notify_note_attachment_cleanup()';
END;
-$$;
-
-CREATE TRIGGER on_contact_notes_changed_delete_note_attachments
-AFTER DELETE OR UPDATE ON public.contact_notes
-FOR EACH ROW
-EXECUTE FUNCTION public.atomic_crm_notify_note_attachment_cleanup();
-
-CREATE TRIGGER on_deal_notes_changed_delete_note_attachments
-AFTER DELETE OR UPDATE ON public.deal_notes
-FOR EACH ROW
-EXECUTE FUNCTION public.atomic_crm_notify_note_attachment_cleanup();
+$migration$;