--=============================================================================================================================
-- MARS-1049-ADHOC: Master Rollback Script
--=============================================================================================================================
-- Purpose: Rollback CSDB external tables encoding changes from WE8MSWIN1252 to UTF8
-- Author: Grzegorz Michalski
-- Date: 2025-11-26
-- Related: MARS-1049 (CSV Encoding Support), MARS-821 (Original CSDB Setup)
--
-- Prerequisites:
--   - FILE_MANAGER v3.2.1+ with pEncoding parameter support
--   - FILE_MANAGER_ODS v2.1.0+ with pEncoding parameter support
--   - Template tables exist in CT_ET_TEMPLATES schema
--   - Proper bucket URIs configured in ENV_MANAGER
--   - Execute as ODS user or with sufficient privileges
--
-- Rollback Sequence:
--   Step 1: Rollback CSDB configurations to UTF8 encoding
--   Step 2: Drop CSDB external tables with WE8MSWIN1252 encoding
--   Step 3: Recreate CSDB external tables with UTF8 encoding (using MARS-821 patterns)
--   Step 4: Verify all tables created successfully
--
-- Post-Rollback:
--   - CSDB configurations restored to UTF8
--   - External tables recreated with UTF8 encoding
--   - System restored to pre-MARS-1049-ADHOC state
--=============================================================================================================================

SET SERVEROUTPUT ON
SET ECHO ON
SET VERIFY OFF

-- ===================================================================
-- MARS-1049-ADHOC ROLLBACK SCRIPT: Restore UTF8 Encoding for CSDB
-- ===================================================================
-- Purpose: Rollback CSDB external tables to UTF8 encoding
-- Author: Grzegorz Michalski
-- Date: 2025-11-26
-- Version: 1.0.0

-- Dynamic spool file generation (using SYS_CONTEXT - no DBA privileges required)
var filename VARCHAR2(100)
BEGIN
    :filename := 'ROLLBACK_MARS_1049_ADHOC_' || SYS_CONTEXT('USERENV', 'CON_NAME') || '_' || TO_CHAR(SYSDATE,'YYYYMMDD_HH24MISS') || '.log';
END;
/
column filename new_value _filename
select :filename filename from dual;
spool &_filename

PROMPT =========================================================================
PROMPT MARS-1049-ADHOC: Rollback to UTF8 Encoding
PROMPT =========================================================================
PROMPT 
PROMPT This script will:
PROMPT - Rollback CSDB configurations to UTF8 encoding
PROMPT - Drop existing CSDB external tables (WE8MSWIN1252)
PROMPT - Recreate 18 CSDB external tables with UTF8 encoding
PROMPT - Verify successful rollback
PROMPT 
PROMPT Expected Duration: 2-3 minutes
PROMPT =========================================================================

PROMPT 
PROMPT =========================================================================
PROMPT Step 1: Rollback CSDB Encoding Configuration to UTF8
PROMPT =========================================================================
@@91_MARS_1049_ADHOC_rollback_encoding.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 2: Drop Existing CSDB External Tables
PROMPT =========================================================================
@@01_MARS_1049_ADHOC_drop_external_tables.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 3: Recreate CSDB External Tables with UTF8 Encoding
PROMPT =========================================================================
@@92_MARS_1049_ADHOC_recreate_tables_utf8.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 4: Verify External Tables Creation
PROMPT =========================================================================
@@03_MARS_1049_ADHOC_verify_external_tables.sql

PROMPT 
PROMPT =========================================================================
PROMPT MARS-1049-ADHOC Rollback - COMPLETED
PROMPT =========================================================================
PROMPT Check the log file for complete rollback details.
PROMPT =========================================================================

spool off

PROMPT 
PROMPT =========================================================================
PROMPT MARS-1049-ADHOC Rollback Complete
PROMPT =========================================================================
PROMPT 
PROMPT All CSDB external tables have been restored to UTF8 encoding.
PROMPT System returned to pre-MARS-1049-ADHOC state.
PROMPT 
PROMPT =========================================================================

quit;
