-- Manual rollback for migration 013.
-- Refuses to discard user-facing filename provenance once new uploads exist.

BEGIN;

DO $$
BEGIN
    IF current_database() <> 'booking_test' THEN
        RAISE EXCEPTION
            'ARR daily upload filename rollback is allowed only in booking_test';
    END IF;
    IF NOT EXISTS (
        SELECT 1
        FROM information_schema.columns
        WHERE table_schema = 'ingestion'
          AND table_name = 'processing_runs'
          AND column_name = 'uploaded_filename'
    ) THEN
        RAISE EXCEPTION
            'ARR daily upload filename migration is not applied';
    END IF;
    IF EXISTS (
        SELECT 1
        FROM ingestion.processing_runs
        WHERE uploaded_filename IS NOT NULL
    ) THEN
        RAISE EXCEPTION
            'refusing rollback: uploaded filename provenance exists';
    END IF;
END;
$$;

ALTER TABLE ingestion.processing_runs
    DROP CONSTRAINT processing_runs_uploaded_filename_check,
    DROP COLUMN uploaded_filename;

COMMIT;
