-- ============================================================================
-- MARS-826-PREHOOK Master Rollback Script
-- ============================================================================
-- Purpose: Rollback DATA_EXPORTER package to previous version
-- Target Schema: CT_MRDS
-- WARNING: Only execute if you need to revert to A_WORKFLOW_HISTORY_KEY
-- ============================================================================

SET SERVEROUTPUT ON SIZE UNLIMITED
SET VERIFY OFF
SET FEEDBACK ON
SET ECHO OFF

-- Create log directory if it doesn't exist
host mkdir log 2>nul

-- Generate dynamic SPOOL filename with timestamp
var filename VARCHAR2(100)
BEGIN
    :filename := 'log/ROLLBACK_MARS_826_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

PROMPT
PROMPT ============================================================================
PROMPT MARS-826-PREHOOK Rollback Starting
PROMPT ============================================================================
PROMPT WARNING: This will restore DATA_EXPORTER to use A_WORKFLOW_HISTORY_KEY
PROMPT
PROMPT CRITICAL PREREQUISITES:
PROMPT 1. CT_ODS.A_LOAD_HISTORY must have column A_WORKFLOW_HISTORY_KEY
PROMPT    (if column is A_ETL_LOAD_SET_KEY, rollback will fail)
PROMPT 2. No active MARS-826 exports should be running
PROMPT
PROMPT Timestamp:
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS rollback_start FROM DUAL;
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

-- Rollback steps (SPEC first, then BODY - standard Oracle deployment order)
PROMPT
PROMPT Step 1/2: Restoring Previous DATA_EXPORTER Package Specification
PROMPT =================================================================
@@91_MARS_826_PREHOOK_rollback_DATA_EXPORTER_SPEC.sql

PROMPT
PROMPT Step 2/2: Restoring Previous DATA_EXPORTER Package Body
PROMPT ========================================================
@@92_MARS_826_PREHOOK_rollback_DATA_EXPORTER_BODY.sql

-- Verify rollback
PROMPT
PROMPT Verification: Package Compilation Status
PROMPT =========================================
SELECT object_name, object_type, status, last_ddl_time
FROM all_objects 
WHERE owner = 'CT_MRDS'
  AND object_name = 'DATA_EXPORTER' 
  AND object_type IN ('PACKAGE', 'PACKAGE BODY')
ORDER BY object_type;

PROMPT
PROMPT ============================================================================
PROMPT MARS-826-PREHOOK Rollback Completed
PROMPT ============================================================================
PROMPT Completion Time:
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS rollback_end FROM DUAL;
PROMPT
PROMPT Rollback Summary:
PROMPT - Package Body: CT_MRDS.DATA_EXPORTER
PROMPT - Restored Version: Previous (with A_WORKFLOW_HISTORY_KEY references)
PROMPT
PROMPT Log file: &_filename
PROMPT ============================================================================

spool off

quit;
