Files
wyndham-ARR/database/014_booking_current_source_batch.down.sql
2026-07-31 15:11:42 +08:00

73 lines
2.1 KiB
PL/PgSQL

-- Manual rollback for migration 014.
-- Refuses once real Booking Excel data exists because removing the pointer would aggregate full batches together.
BEGIN;
DO $$
BEGIN
IF current_database() <> 'booking_test' THEN
RAISE EXCEPTION
'ARR Booking current-source rollback is allowed only in booking_test';
END IF;
IF to_regclass('booking.current_source_batch') IS NULL THEN
RAISE EXCEPTION
'ARR Booking current-source migration is not applied';
END IF;
IF EXISTS (
SELECT 1
FROM booking.source_batches
WHERE source_kind = 'booking_excel'
) THEN
RAISE EXCEPTION
'refusing rollback: Booking Excel source history exists';
END IF;
IF (
SELECT count(*)
FROM booking.source_batches
WHERE batch_status = 'accepted'
) > 1 THEN
RAISE EXCEPTION
'refusing rollback: multiple accepted Booking source batches exist';
END IF;
END;
$$;
CREATE OR REPLACE VIEW booking.v_current_room_items AS
SELECT
source.id AS source_row_id,
source.source_batch_id,
source.source_worksheet,
source.source_row_no,
source.group_code_raw,
source.group_code_key,
source.type_of_room_raw,
source.no_of_rooms AS source_no_of_rooms,
parsed.id AS parse_version_id,
parsed.version_no AS parse_version_no,
item.item_no,
item.room_type_raw,
item.room_type_code,
item.quantity,
item.unit_price,
item.currency_code,
item.price_token_raw,
item.source_fragment
FROM booking.current_row_parses AS current_parse
JOIN booking.parse_versions AS parsed
ON parsed.id = current_parse.parse_version_id
JOIN booking.source_rows AS source
ON source.id = current_parse.source_row_id
JOIN booking.source_batches AS batch
ON batch.id = source.source_batch_id
JOIN booking.room_items AS item
ON item.parse_version_id = parsed.id
WHERE batch.batch_status = 'accepted'
AND parsed.parse_status = 'accepted';
DROP TRIGGER current_source_batch_acceptance_guard
ON booking.current_source_batch;
DROP FUNCTION booking.validate_current_source_batch();
DROP TABLE booking.current_source_batch;
COMMIT;