-- ===================================================================
-- MARS-1057 ROLLBACK SCRIPT: Batch External Table Creation
-- ===================================================================
-- Purpose: Rollback FILE_MANAGER package to v3.3.0 (remove batch external table procedures)
-- Author: Grzegorz Michalski
-- Date: 2025-11-27

-- Dynamic spool file generation (using SYS_CONTEXT - no DBA privileges required)
-- Log files are automatically created in log/ subdirectory
-- IMPORTANT: Ensure log/ directory exists before SPOOL (use host mkdir)
host mkdir log 2>nul

var filename VARCHAR2(100)
BEGIN
    :filename := 'log/ROLLBACK_MARS_1057_' || SYS_CONTEXT('USERENV', 'CON_NAME') || '_' || TO_CHAR(SYSDATE,'YYYYMMDD_HH24MISS') || '.log';
END;
/
column filename new_value _filename
select :filename filename from dual;
spool &_filename

SET ECHO OFF
SET TIMING ON
SET SERVEROUTPUT ON SIZE UNLIMITED
SET PAUSE OFF

PROMPT =========================================================================
PROMPT MARS-1057: Rollback Batch External Table Creation
PROMPT =========================================================================
PROMPT WARNING: This will reverse all changes from MARS-1057 installation!
PROMPT 
PROMPT Changes to be rolled back:
PROMPT - FILE_MANAGER package specification (3.4.0 -> 3.3.0)
PROMPT - FILE_MANAGER package body (3.4.0 -> 3.3.0)
PROMPT - Remove CREATE_EXTERNAL_TABLES_SET procedure
PROMPT - Remove CREATE_EXTERNAL_TABLES_BATCH procedure
PROMPT =========================================================================

-- Confirm rollback with user
ACCEPT continue CHAR PROMPT 'Type YES to continue with rollback, or Ctrl+C to abort: '
WHENEVER SQLERROR EXIT SQL.SQLCODE
BEGIN
    IF '&continue' IS NULL OR TRIM('&continue') IS NULL OR UPPER(TRIM('&continue')) != 'YES' THEN
        RAISE_APPLICATION_ERROR(-20001, 'Rollback aborted by user');
    END IF;
END;
/
WHENEVER SQLERROR CONTINUE

-- Execute rollback scripts in REVERSE order (92, 91, not 91, 92)
PROMPT 
PROMPT =========================================================================
PROMPT Step 1: Rollback FILE_MANAGER Package Body to v3.3.0
PROMPT =========================================================================
@@91_MARS_1057_rollback_CT_MRDS_FILE_MANAGER_BODY.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 2: Rollback FILE_MANAGER Package Specification to v3.3.0
PROMPT =========================================================================
@@92_MARS_1057_rollback_CT_MRDS_FILE_MANAGER_SPEC.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 3: Track Rollback Version
PROMPT =========================================================================
@@track_package_versions.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 4: Verify All Tracked Packages
PROMPT =========================================================================
@@verify_packages_version.sql

PROMPT 
PROMPT =========================================================================
PROMPT MARS-1057 Rollback - COMPLETED
PROMPT =========================================================================
PROMPT Check the log file for complete rollback details.
PROMPT Log file: log/ROLLBACK_MARS_1057_<PDB>_<timestamp>.log
PROMPT =========================================================================

spool off

quit;
