-- Rollback is safe only before any cancellation-exclusion facts are committed. BEGIN; DO $$ BEGIN IF current_database() <> 'booking_test' THEN RAISE EXCEPTION 'ARR migration is allowed only in booking_test'; END IF; IF EXISTS (SELECT 1 FROM finance.daily_versions WHERE excluded_cancelled_rows <> 0) OR EXISTS (SELECT 1 FROM finance.daily_records WHERE outcome = 'excluded_cancelled') THEN RAISE EXCEPTION 'refusing rollback: cancellation-exclusion facts exist'; END IF; END $$; ALTER TABLE finance.daily_records DROP CONSTRAINT daily_records_outcome_check; ALTER TABLE finance.daily_records ADD CONSTRAINT daily_records_outcome_check CHECK (outcome IN ( 'retained', 'excluded_rate_code', 'duplicate', 'validation_failed', 'price_unmatched' )); ALTER TABLE finance.daily_versions DROP CONSTRAINT daily_versions_counts_reconcile; ALTER TABLE finance.daily_versions ADD CONSTRAINT daily_versions_counts_reconcile CHECK ( source_rows = retained_rows + excluded_rate_code_rows + duplicate_rows + validation_failed_rows + price_unmatched_rows ); ALTER TABLE finance.daily_versions DROP COLUMN excluded_cancelled_rows; COMMIT;