-- ===================================================================
-- MARS-1046 ROLLBACK SCRIPT: ISO 8601 Date Format Fix
-- ===================================================================
-- Purpose: Rollback FILE_MANAGER to version 3.3.0 (MARS-1056)
-- Author: Grzegorz Michalski
-- Date: 2025-11-27
--
-- WARNING: This will reverse all changes from MARS-1046 installation!
--
-- Rollback User: ADMIN (required for schema operations)
-- ===================================================================

-- Dynamic spool file generation (using SYS_CONTEXT - no DBA privileges required)
var filename VARCHAR2(100)
BEGIN
    :filename := 'ROLLBACK_MARS_1046_' || 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-1046: Rollback Package
PROMPT =========================================================================
PROMPT WARNING: This will reverse all changes from MARS-1046 installation!
PROMPT 
PROMPT This will:
PROMPT - Restore FILE_MANAGER package to version 3.3.0 (MARS-1056)
PROMPT - Remove NORMALIZE_DATE_FORMAT function
PROMPT - Restore original GENERATE_EXTERNAL_TABLE_PARAMS behavior
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

PROMPT 
PROMPT =========================================================================
PROMPT Step 1: Restore FILE_MANAGER Package Body (v3.3.0)
PROMPT =========================================================================
@@91_MARS_1046_rollback_CT_MRDS_FILE_MANAGER_BODY.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 2: Restore FILE_MANAGER Package Specification (v3.3.0)
PROMPT =========================================================================
@@92_MARS_1046_rollback_CT_MRDS_FILE_MANAGER_SPEC.sql

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

PROMPT 
PROMPT =========================================================================
PROMPT Step 4: Verify Package Status
PROMPT =========================================================================
@@test/verify_packages_version.sql

PROMPT 
PROMPT =========================================================================
PROMPT MARS-1046 Rollback - COMPLETED
PROMPT =========================================================================
PROMPT 
PROMPT FILE_MANAGER restored to version 3.3.0
PROMPT 
PROMPT Verification:
PROMPT   SELECT FILE_MANAGER.GET_VERSION() FROM DUAL;
PROMPT 
PROMPT Check the log file for complete rollback details:
PROMPT   &_filename
PROMPT =========================================================================

spool off
quit;
