-- ===================================================================
-- MARS-835-PREHOOK ROLLBACK SCRIPT: DRY Refactoring for DATA_EXPORTER (Rollback)
-- ===================================================================
-- Purpose: Pre-hook rollback for MARS-835 - Rollback DATA_EXPORTER to v2.1.1 (restore pre-refactoring code)
-- Author: Grzegorz Michalski
-- Date: 2025-12-19
-- Version: 2.1.1 (rollback)

-- 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 Package
PROMPT =========================================================================
PROMPT WARNING: This will reverse all changes from MARS-835-PREHOOK installation!
PROMPT WARNING: DRY refactoring will be reverted to v2.1.1!
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 (body first, then spec)
PROMPT 
PROMPT =========================================================================
PROMPT Step 1: Rollback DATA_EXPORTER Package Body to v2.1.1
PROMPT =========================================================================
@@91_MARS_835_rollback_DATA_EXPORTER_BODY.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 2: Rollback DATA_EXPORTER Package Specification to v2.1.1
PROMPT =========================================================================
@@92_MARS_835_rollback_DATA_EXPORTER_SPEC.sql

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

PROMPT 
PROMPT =========================================================================
PROMPT Step 4: Verify Package Versions
PROMPT =========================================================================
@@verify_packages_version.sql

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

spool off

quit;
