diff --git a/veza-backend-api/migrations/983_seller_transfers_reversal_pending_not_null.sql b/veza-backend-api/migrations/983_seller_transfers_reversal_pending_not_null.sql index dc3bf215a..608e06a6a 100644 --- a/veza-backend-api/migrations/983_seller_transfers_reversal_pending_not_null.sql +++ b/veza-backend-api/migrations/983_seller_transfers_reversal_pending_not_null.sql @@ -21,10 +21,22 @@ -- legitimately NULL in the other statuses (pending, completed, -- failed without pending retry, reversed, permanently_failed). -ALTER TABLE seller_transfers - ADD CONSTRAINT chk_reversal_pending_has_next_retry_at - CHECK (status <> 'reversal_pending' OR next_retry_at IS NOT NULL) - NOT VALID; +-- ADD CONSTRAINT is not natively IF NOT EXISTS in Postgres — the +-- DO block catches `duplicate_object` so re-running the migration +-- (test runs, manual psql apply, re-deploys) is a no-op. Same shape +-- the runner expects from other idempotent migrations in this +-- directory. +DO $$ +BEGIN + ALTER TABLE seller_transfers + ADD CONSTRAINT chk_reversal_pending_has_next_retry_at + CHECK (status <> 'reversal_pending' OR next_retry_at IS NOT NULL) + NOT VALID; +EXCEPTION + WHEN duplicate_object THEN + -- constraint already present (prior manual apply or prior run) + NULL; +END $$; -- NOT VALID creates the constraint without scanning existing rows. -- Pre-v1.0.7.2 rows are grandfathered — item A + day 2 of B don't