-- ===================================================================
-- MARS-828: Auto Rollback (bypasses confirmation)
-- ===================================================================
-- Purpose: Rollback MARS-828 without user prompts
-- Author: Grzegorz Michalski
-- Date: 2026-01-29
-- ===================================================================

-- Dynamic spool file generation
host mkdir ..\log 2>nul

var filename VARCHAR2(100)
BEGIN
    :filename := '../log/ROLLBACK_MARS_828_AUTO_' || 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 LINESIZE 200
SET PAGESIZE 1000
SET PAUSE OFF

PROMPT
PROMPT ============================================================================
PROMPT MARS-828 Rollback Starting (AUTO MODE - No Confirmation)
PROMPT ============================================================================
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS rollback_start FROM DUAL;
PROMPT ============================================================================

-- Rollback steps (in reverse order)
PROMPT
PROMPT Step 1/6: Restoring FILE_ARCHIVER Package Specification v2.0.0
PROMPT ===============================================================
@@../91_MARS_828_rollback_FILE_ARCHIVER_SPEC.sql

PROMPT
PROMPT Step 2/6: Restoring FILE_ARCHIVER Package Body v2.0.0
PROMPT ======================================================
@@../92_MARS_828_rollback_FILE_ARCHIVER_BODY.sql

PROMPT
PROMPT Step 3/6: Dropping validation trigger
PROMPT ======================================
@@../93_MARS_828_rollback_trigger.sql

PROMPT
PROMPT Step 4/6: Dropping archival strategy columns
PROMPT =============================================
@@../94_MARS_828_rollback_columns.sql

PROMPT
PROMPT Step 5/6: Tracking rollback version
PROMPT ====================================
@@../track_package_versions.sql

PROMPT
PROMPT Step 6/6: Verifying tracked packages
PROMPT =====================================
@@../verify_packages_version.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 = 'FILE_ARCHIVER' 
  AND object_type IN ('PACKAGE', 'PACKAGE BODY')
ORDER BY object_type;

PROMPT
PROMPT ============================================================================
PROMPT MARS-828 Rollback Completed
PROMPT ============================================================================
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS rollback_end FROM DUAL;
PROMPT
PROMPT Log file: &_filename
PROMPT ============================================================================

spool off
quit;
