From 99d32cda6099d805e184c5e9a12bf31e4923dc2a Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Sun, 26 Apr 2026 08:52:31 +0000 Subject: [PATCH] Changes Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com> --- ...8_8b01c824-d7a9-43e8-aedb-3c0a89d3c06d.sql | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 supabase/migrations/20260426085228_8b01c824-d7a9-43e8-aedb-3c0a89d3c06d.sql diff --git a/supabase/migrations/20260426085228_8b01c824-d7a9-43e8-aedb-3c0a89d3c06d.sql b/supabase/migrations/20260426085228_8b01c824-d7a9-43e8-aedb-3c0a89d3c06d.sql new file mode 100644 index 0000000..3adcabf --- /dev/null +++ b/supabase/migrations/20260426085228_8b01c824-d7a9-43e8-aedb-3c0a89d3c06d.sql @@ -0,0 +1,29 @@ +-- Backfill case_id on payment_plans created via collections so they show up +-- on the case Payment Plans tab. +UPDATE public.payment_plans pp +SET case_id = c.case_id +FROM public.collections c +WHERE pp.collection_id = c.id + AND pp.case_id IS NULL + AND c.case_id IS NOT NULL; + +-- Trigger to auto-populate case_id on insert/update when collection_id is set +-- but case_id is missing. +CREATE OR REPLACE FUNCTION public.payment_plans_fill_case_id() +RETURNS TRIGGER +LANGUAGE plpgsql +SECURITY DEFINER +SET search_path = public +AS $$ +BEGIN + IF NEW.case_id IS NULL AND NEW.collection_id IS NOT NULL THEN + SELECT case_id INTO NEW.case_id FROM public.collections WHERE id = NEW.collection_id; + END IF; + RETURN NEW; +END; +$$; + +DROP TRIGGER IF EXISTS payment_plans_fill_case_id_trg ON public.payment_plans; +CREATE TRIGGER payment_plans_fill_case_id_trg +BEFORE INSERT OR UPDATE ON public.payment_plans +FOR EACH ROW EXECUTE FUNCTION public.payment_plans_fill_case_id(); \ No newline at end of file