-- ===================================================================
-- MARS-1443-PREHOOK ROLLBACK SCRIPT
-- ===================================================================
-- Purpose: Rollback pre-hook changes for MARS-1443
-- Author: Grzegorz Michalski
-- Date: 2026-03-26

-- 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_1443_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-1443-PREHOOK: Rollback
PROMPT =========================================================================
PROMPT WARNING: This will reverse all changes from MARS-1443-PREHOOK installation!
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_1443_PREHOOK_rollback_CT_MRDS_FILE_MANAGER_BODY.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 2: Rollback FILE_MANAGER Package Specification to v3.3.0
PROMPT =========================================================================
@@91_MARS_1443_PREHOOK_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_1443_PREHOOK_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_1443_PREHOOK_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-1443-PREHOOK Rollback - COMPLETED
PROMPT =========================================================================
PROMPT Check the log file for complete rollback details.
PROMPT Log file: log/ROLLBACK_MARS_1443_PREHOOK_<PDB>_<timestamp>.log
PROMPT =========================================================================

spool off

quit;
