-- Roll back durable Booking Excel extraction drafts.

BEGIN;

DO $$
BEGIN
    IF current_database() <> 'booking_test' THEN
        RAISE EXCEPTION
            'ARR Booking review rollback is allowed only in booking_test';
    END IF;
    IF to_regclass('booking.extraction_drafts') IS NULL
       OR to_regclass('booking.extraction_draft_items') IS NULL THEN
        RAISE EXCEPTION
            'ARR Booking review migration is not applied';
    END IF;
    IF EXISTS (
        SELECT 1
        FROM booking.extraction_drafts
        WHERE draft_status = 'reviewing'
    ) THEN
        RAISE EXCEPTION
            'ARR Booking review rollback refused while a review draft is open';
    END IF;
END;
$$;

DROP TABLE booking.extraction_draft_items;
DROP TABLE booking.extraction_drafts;

COMMIT;
