Files
accounted/supabase/migrations/20260715181301_reconcile_credit_note_paid_status.sql
Mattsson aa5edd3aa7 fix(database): unblock credit note constraint validation (#1024)
* docs: record legacy credit note migration repair

* fix(database): assert repaired credit note state

---------

Co-authored-by: Jakob Wennberg <jakob.wennberg@gmail.com>
2026-07-16 16:04:06 +02:00

25 lines
846 B
SQL

-- The linked production database contained one legacy credit note in the
-- ordinary payment lifecycle. It was reconciled before this migration by
-- linking its existing balanced reversal voucher and normalizing the invoice
-- metadata. Keep an explicit forward migration that verifies the repaired
-- invariant before the remaining migration chain proceeds.
-- pg-test: covered-by lib/invoices/__tests__/credit-note-not-payable.pg.test.ts
DO $reconcile$
BEGIN
IF EXISTS (
SELECT 1
FROM public.invoices
WHERE credited_invoice_id IS NOT NULL
AND status IN ('paid', 'partially_paid')
) THEN
RAISE EXCEPTION
'Legacy credit notes still use ordinary customer-payment states';
END IF;
END
$reconcile$;
ALTER TABLE public.invoices
VALIDATE CONSTRAINT invoices_credit_note_not_paid;
NOTIFY pgrst, 'reload schema';