-- ===================================================================
-- MARS-1049 ROLLBACK SCRIPT: CSV Encoding Support
-- ===================================================================
-- Purpose: Complete rollback of CSV encoding support changes
-- Author: Grzegorz Michalski
-- Date: 2025-11-25
-- Version: 1.5.0

-- Dynamic spool file generation
var filename VARCHAR2(100)
BEGIN
    SELECT 'ROLLBACK_MARS_1049_' || SUBSTR(PDB_NAME, (INSTR(PDB_NAME,'_',1)+1), (LENGTH(PDB_NAME)-INSTR(PDB_NAME,'_',1))) ||  '_' ||TO_CHAR(SYSDATE,'YYYYMMDD_HH24MISS')||'.log' INTO :filename from DBA_PDBS;
END;
/
column filename new_value _filename
select :filename filename from dual;
spool &_filename
-- 
-- CURRENT IMPLEMENTATION STATUS:
-- - CT_MRDS.FILE_MANAGER: Version 3.2.1 with pEncoding parameter (deployed)
-- - ODS.FILE_MANAGER_ODS: Version 2.1.0 with pEncoding wrapper (deployed)
-- - A_SOURCE_FILE_CONFIG: ENCODING VARCHAR2(50) column (deployed)
-- 
-- ROLLBACK STRATEGY:
-- - Uses current_version/ folder for pre-MARS-1049 package versions
-- - Uses new_version/ folder for MARS-1049 enhanced versions (for reference)
-- 
-- ROLLBACK ORDER:
-- 1. Restore ODS.FILE_MANAGER_ODS package from current_version/ (version 2.0.0)
-- 2. Restore CT_MRDS.FILE_MANAGER package from current_version/ (version 3.2.0)  
-- 3. Remove ENCODING column from A_SOURCE_FILE_CONFIG
-- 4. Track package versions
-- 5. Verify all tracked packages
-- 6. Revoke SELECT ANY DICTIONARY from CT_MRDS
-- ===================================================================

PROMPT ===============================================
PROMPT MARS-1049: CSV Encoding Support - Full Rollback
PROMPT ===============================================

-- Set error handling
WHENEVER SQLERROR CONTINUE
WHENEVER OSERROR CONTINUE

PROMPT
PROMPT Warning: This will remove the ENCODING column and revert all package changes.
PROMPT Make sure you have backed up your data if needed.
PROMPT

-- Step 1: Restore ODS.FILE_MANAGER_ODS package to version 2.0.0
PROMPT Step 1: Restoring ODS.FILE_MANAGER_ODS package to version 2.0.0...
PROMPT ===============================================

-- Restore ODS.FILE_MANAGER_ODS specification from current_version (before MARS-1049)
@@current_version/FILE_MANAGER_ODS.pkg

-- Restore ODS.FILE_MANAGER_ODS body from current_version (before MARS-1049)  
@@current_version/FILE_MANAGER_ODS.pkb

PROMPT ODS.FILE_MANAGER_ODS package restored to version 2.0.0

-- Step 2: Restore CT_MRDS.FILE_MANAGER package to version 3.2.0
PROMPT Step 2: Restoring CT_MRDS.FILE_MANAGER package to version 3.2.0...
PROMPT ===============================================

-- Restore CT_MRDS.FILE_MANAGER specification from current_version (before MARS-1049)
@@current_version/FILE_MANAGER.pkg

-- Restore CT_MRDS.FILE_MANAGER body from current_version (before MARS-1049)
@@current_version/FILE_MANAGER.pkb

PROMPT CT_MRDS.FILE_MANAGER package restored to version 3.2.0

-- Step 3: Remove ENCODING column from A_SOURCE_FILE_CONFIG
PROMPT Step 3: Remove ENCODING column from A_SOURCE_FILE_CONFIG...
PROMPT ===============================================

@@91_MARS_1049_rollback_DROP_ENCODING_COLUMN.sql

-- Step 4: Track rollback version
PROMPT Step 4: Tracking package versions...
PROMPT ===============================================

@@track_package_versions.sql

PROMPT
PROMPT Step 5: Verifying all tracked packages...
PROMPT ===============================================
@@verify_packages_version.sql

PROMPT
PROMPT Step 6: Revoking cross-schema hash tracking permissions...
PROMPT ===============================================
@@90_MARS_1049_rollback_revoke_permissions.sql

PROMPT ===============================================
PROMPT MARS-1049: Rollback Complete!
PROMPT ===============================================
PROMPT
PROMPT Summary of Rollback Changes:
PROMPT - A_SOURCE_FILE_CONFIG: Removed ENCODING VARCHAR2(50) column
PROMPT - CT_MRDS.FILE_MANAGER: Restored to version 3.2.0 (before MARS-1049)
PROMPT - ODS.FILE_MANAGER_ODS: Restored to version 2.0.0 (before MARS-1049)
PROMPT - SELECT ANY DICTIONARY: Revoked from CT_MRDS (cross-schema tracking disabled)
PROMPT
PROMPT All MARS-1049 changes have been successfully rolled back.
PROMPT System is now in pre-MARS-1049 state.
PROMPT

spool off

exit