Files
accounted/supabase/migrations/20260714121345_prevent_credit_note_paid_status.sql
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

29 lines
953 B
SQL

-- Credit notes are adjustments, not customer receivables. They must not enter
-- the ordinary customer-payment lifecycle.
--
-- NOT VALID avoids scanning or rewriting existing rows during deployment. The
-- constraint still applies to every new row and every future update, so all
-- API, RPC, extension, and legacy write paths share the same invariant.
DO $migration$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_constraint
WHERE conname = 'invoices_credit_note_not_paid'
AND conrelid = 'public.invoices'::regclass
) THEN
ALTER TABLE public.invoices
ADD CONSTRAINT invoices_credit_note_not_paid
CHECK (
credited_invoice_id IS NULL
OR status NOT IN ('paid', 'partially_paid')
) NOT VALID;
END IF;
END
$migration$;
COMMENT ON CONSTRAINT invoices_credit_note_not_paid ON public.invoices IS
'Credit notes cannot use the ordinary customer-invoice payment states.';
NOTIFY pgrst, 'reload schema';