/*
================================================================================
MARS-1056: Master Rollback Script for VARCHAR2 Definition Fixes
Author: Grzegorz Michalski
Date: November 25, 2025
Purpose: Main rollback script for VARCHAR2 fixes in case of issues

USAGE:
sql 'CT_MRDS/Cloudpass#34@ggmichalski_high' '@rollback_mars1056.sql'
================================================================================
*/

-- Dynamic spool file generation (using SYS_CONTEXT - no DBA privileges required)
var filename VARCHAR2(100)
BEGIN
    :filename := 'ROLLBACK_MARS_1056_' || 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 ========================================================================
PROMPT   MARS-1056: VARCHAR2 Definition Fixes ROLLBACK
PROMPT ========================================================================
PROMPT
PROMPT WARNING: This will rollback all MARS-1056 changes!
PROMPT
PROMPT After rollback:
PROMPT - FILE_MANAGER will use original VARCHAR2 definition logic
PROMPT - External tables will again have data_length based definitions
PROMPT - CHAR/BYTE semantics will NOT be preserved from templates
PROMPT
PROMPT Continue only if you are experiencing issues with MARS-1056 changes
PROMPT
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

-- Set current schema context  
ALTER SESSION SET CURRENT_SCHEMA = CT_MRDS;

PROMPT
PROMPT === Step 1: Rollback packages to pre-MARS-1056 versions ===
@@91_MARS_1056_rollback_packages.sql

PROMPT
PROMPT === Step 2: Track rollback version ===
@@track_package_versions.sql

PROMPT
PROMPT === Step 3: Verify all tracked packages ===
@@verify_packages_version.sql

PROMPT
PROMPT ========================================================================
PROMPT   MARS-1056: Rollback completed successfully
PROMPT ========================================================================
PROMPT
PROMPT All packages have been restored to pre-MARS-1056 state:
PROMPT - FILE_MANAGER: Original VARCHAR2 definition logic restored (v3.2.1)
PROMPT - FILE_MANAGER_ODS: Unchanged (v2.1.0 - wrapper only)
PROMPT - External tables will use data_length based definitions again
PROMPT
PROMPT Impact:
PROMPT - VARCHAR2 columns in external tables will NOT match template definitions
PROMPT - CHAR semantics will be lost (converted to BYTE with larger sizes)
PROMPT - This is the original problematic behavior that MARS-1056 was meant to fix
PROMPT
PROMPT Rollback tracking has been logged for audit purposes.
PROMPT
PROMPT ========================================================================
PROMPT Rollback log saved to: &_filename
PROMPT ========================================================================

spool off

quit;
