-- ===================================================================
-- MARS-1057 ROLLBACK SCRIPT: Batch External Table Creation
-- ===================================================================
-- Purpose: Rollback FILE_MANAGER package to v3.3.0 (remove batch external table procedures and area filter)
-- Author: Grzegorz Michalski
-- Date: 2026-02-18

-- 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 + Area Filter
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.5.0 -> 3.3.0)
PROMPT - FILE_MANAGER package body (3.5.0 -> 3.3.0)
PROMPT - FILE_MANAGER_ODS package specification (2.4.0 -> 2.1.0)
PROMPT - FILE_MANAGER_ODS package body (2.4.0 -> 2.1.0)
PROMPT - Remove CREATE_EXTERNAL_TABLES_SET procedures
PROMPT - Remove CREATE_EXTERNAL_TABLES_BATCH procedures
PROMPT - Remove pArea parameter (selective table creation)
PROMPT - Remove pRestoreGrants parameter (grant preservation)
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 (94, 93, 92, 91)
PROMPT 
PROMPT =========================================================================
PROMPT Step 1: Rollback FILE_MANAGER Package Body to v3.3.0
PROMPT =========================================================================
@@92_MARS_1057_rollback_CT_MRDS_FILE_MANAGER_BODY.sql

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

PROMPT 
PROMPT =========================================================================
PROMPT Step 3: Rollback FILE_MANAGER_ODS Package Body to v2.1.0
PROMPT =========================================================================
@@93_MARS_1057_rollback_ODS_FILE_MANAGER_ODS_BODY.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 4: Rollback FILE_MANAGER_ODS Package Specification to v2.1.0
PROMPT =========================================================================
@@94_MARS_1057_rollback_ODS_FILE_MANAGER_ODS_SPEC.sql

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

PROMPT 
PROMPT =========================================================================
PROMPT Step 6: 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;
