-- Never roll back after PM exclusion facts have been 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_pm_rows <> 0) OR EXISTS (SELECT 1 FROM finance.daily_records WHERE outcome = 'excluded_pm') THEN RAISE EXCEPTION 'refusing rollback: PM-exclusion facts exist'; END IF; END $$; ALTER TABLE finance.daily_records DROP CONSTRAINT daily_records_pm_exclusion_shape; 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', 'excluded_cancelled' )); 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 + excluded_cancelled_rows ); ALTER TABLE finance.daily_versions DROP COLUMN excluded_pm_rows; COMMIT;