Exclude PM room types from shared daily report processing

This commit is contained in:
Wyndham ARR committed 2026-10-08 22:30:04 +08:00
1 parent d3902dd857
commit f46efd6d9a
32 files changed
+968 -69

No files matched your search

+28
View File
@@ -0,0 +1,28 @@
-- 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;
+52
View File
@@ -0,0 +1,52 @@
-- Add PM exclusion auditing without rewriting any released daily facts.
-- PostgreSQL 15+. Migrations 008 through 020 remain immutable.
BEGIN;
DO $$
BEGIN
IF current_database() <> 'booking_test' THEN
RAISE EXCEPTION 'ARR migration is allowed only in booking_test';
END IF;
IF NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_schema = 'finance' AND table_name = 'daily_versions'
AND column_name = 'excluded_cancelled_rows'
AND data_type = 'integer' AND is_nullable = 'NO'
) OR NOT EXISTS (
SELECT 1 FROM pg_constraint
WHERE conrelid = 'finance.daily_records'::regclass
AND conname = 'daily_records_outcome_check'
AND pg_get_constraintdef(oid) LIKE '%excluded_cancelled%'
) THEN
RAISE EXCEPTION 'ARR migration 020 must be applied first';
END IF;
END $$;
ALTER TABLE finance.daily_versions
ADD COLUMN excluded_pm_rows integer NOT NULL DEFAULT 0 CHECK (excluded_pm_rows >= 0);
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 + excluded_pm_rows
);
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', 'excluded_pm'
));
ALTER TABLE finance.daily_records
ADD CONSTRAINT daily_records_pm_exclusion_shape CHECK (
outcome <> 'excluded_pm' OR (
room_category_label IS NOT NULL AND upper(btrim(room_category_label)) = 'PM'
AND decision_codes = ARRAY['ROOM_TYPE_PM_EXCLUDED']::text[]
AND real_price IS NULL AND total_price IS NULL AND kb_amount IS NULL
AND channel_key IS NULL AND pricing_method IS NULL
)
);
COMMENT ON COLUMN finance.daily_versions.excluded_pm_rows IS
'Source rows explicitly excluded for PM room type under the pinned processor rules; older versions remain zero.';
COMMIT;