-- ============================================================================
-- MARS-1409 Master Rollback Script
-- ============================================================================
-- Author: Grzegorz Michalski
-- Purpose: Rollback A_WORKFLOW_HISTORY_KEY column changes from A_SOURCE_FILE_RECEIVED
-- Target Schema: CT_MRDS
-- Estimated Time: 1-2 minutes
-- Prerequisites: Backup of current FILE_MANAGER package, ADMIN privileges
-- ============================================================================

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_1409_' || 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-1409 Rollback Starting
PROMPT ============================================================================
PROMPT Package: CT_MRDS.FILE_MANAGER
PROMPT Change: Remove A_WORKFLOW_HISTORY_KEY column and restore previous version
PROMPT Steps: 10 (Restore FILE_ARCHIVER, Restore FILE_MANAGER, Restore ENV_MANAGER, Restore trigger, Clear data, Drop column, Verify)
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(-20000, 'Rollback aborted by user');
    END IF;
END;
/

PROMPT
PROMPT ============================================================================
PROMPT STEP 1: Restore FILE_ARCHIVER package specification (previous version)
PROMPT ============================================================================
@@91_MARS_1409_rollback_CT_MRDS_FILE_ARCHIVER_SPEC.sql

PROMPT
PROMPT ============================================================================
PROMPT STEP 2: Restore FILE_ARCHIVER package body (previous version)
PROMPT ============================================================================
@@92_MARS_1409_rollback_CT_MRDS_FILE_ARCHIVER_BODY.sql

PROMPT
PROMPT ============================================================================
PROMPT STEP 3: Restore FILE_MANAGER package specification (previous version)
PROMPT ============================================================================
@@93_MARS_1409_rollback_CT_MRDS_FILE_MANAGER_SPEC.sql

PROMPT
PROMPT ============================================================================
PROMPT STEP 4: Restore FILE_MANAGER package body (previous version)
PROMPT ============================================================================
@@94_MARS_1409_rollback_CT_MRDS_FILE_MANAGER_BODY.sql

PROMPT
PROMPT ============================================================================
PROMPT STEP 5: Restore ENV_MANAGER package specification (previous version)
PROMPT ============================================================================
@@95_MARS_1409_rollback_CT_MRDS_ENV_MANAGER_SPEC.sql

PROMPT
PROMPT ============================================================================
PROMPT STEP 6: Restore ENV_MANAGER package body (previous version)
PROMPT ============================================================================
@@96_MARS_1409_rollback_CT_MRDS_ENV_MANAGER_BODY.sql

PROMPT
PROMPT ============================================================================
PROMPT STEP 7: Restore A_WORKFLOW_HISTORY trigger (previous version)
PROMPT ============================================================================
@@97_MARS_1409_rollback_CT_MRDS_A_WORKFLOW_HISTORY.sql

PROMPT
PROMPT ============================================================================
PROMPT STEP 8: Drop A_WORKFLOW_HISTORY_KEY column
PROMPT ============================================================================
@@99_MARS_1409_rollback_workflow_history_key_column.sql

PROMPT
PROMPT ============================================================================
PROMPT STEP 9: Verify rollback
PROMPT ============================================================================
@@90_MARS_1409_verify_rollback.sql

PROMPT
PROMPT ============================================================================
PROMPT STEP 10: Verify package versions
PROMPT ============================================================================
@@verify_packages_version.sql

PROMPT
PROMPT ============================================================================
PROMPT MARS-1409 Rollback Complete
PROMPT ============================================================================
PROMPT Final Status:
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS rollback_end FROM DUAL;
PROMPT
PROMPT Review the log file for detailed results: &_filename
PROMPT ============================================================================

spool off
