-- ===================================================================
-- MARS-835-PREHOOK ROLLBACK SCRIPT: Parallel Processing + Smart Column Mapping
-- ===================================================================
-- Purpose: Rollback for MARS-835-PREHOOK - Restore previous package versions
-- Author: Grzegorz Michalski
-- Date: 2026-01-09
-- Version: ENV_MANAGER 3.1.0, DATA_EXPORTER 2.1.0 (rollback targets)

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

var filename VARCHAR2(100)
BEGIN
    :filename := 'log/ROLLBACK_MARS_835_PREHOOK_' || 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-835-PREHOOK: Rollback to Previous Versions
PROMPT =========================================================================
PROMPT WARNING: This will reverse all changes from MARS-835-PREHOOK installation!
PROMPT - Removes A_PARALLEL_EXPORT_CHUNKS table
PROMPT - Reverts A_SOURCE_FILE_RECEIVED table (rename ARCH_PATH to ARCH_FILE_NAME, drop PROCESS_NAME column)
PROMPT - Restores ENV_MANAGER v3.1.0 (removes parallel error codes)
PROMPT - Restores DATA_EXPORTER v2.1.0 (removes parallel + Smart Column Mapping)
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(-20999, 'Rollback aborted by user.');
    END IF;
END;
/
WHENEVER SQLERROR CONTINUE

-- Execute rollback scripts in reverse order of installation
PROMPT 
PROMPT =========================================================================
PROMPT Step 0: Drop A_PARALLEL_EXPORT_CHUNKS Table
PROMPT =========================================================================
@@90_MARS_835_PREHOOK_rollback_PARALLEL_CHUNKS_TABLE.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 1: Rollback ENV_MANAGER Package
PROMPT =========================================================================
@@91_MARS_835_PREHOOK_rollback_ENV_MANAGER.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 2: Rollback DATA_EXPORTER Package
PROMPT =========================================================================
@@92_MARS_835_PREHOOK_rollback_DATA_EXPORTER.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 3: Rollback A_SOURCE_FILE_RECEIVED Table Structure
PROMPT =========================================================================
@@93_MARS_835_PREHOOK_rollback_SOURCE_FILE_RECEIVED_table.sql

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

PROMPT 
PROMPT =========================================================================
PROMPT Step 5: Verify Package Versions After Rollback
PROMPT =========================================================================
@@verify_packages_version.sql

PROMPT 
PROMPT =========================================================================
PROMPT MARS-835-PREHOOK Rollback - COMPLETED
PROMPT =========================================================================

spool off

quit;
