-- ===================================================================
-- MARS-835 ROLLBACK SCRIPT: CSDB Data Export Rollback
-- ===================================================================
-- Purpose: Rollback MARS-835 - Delete exported CSV/Parquet files from OCI buckets
-- WARNING: This will DELETE all exported data files!
-- Author: Grzegorz Michalski
-- Date: 2025-12-17

-- Dynamic spool file generation (using SYS_CONTEXT - no DBA privileges required)
-- IMPORTANT: Ensure log/ directory exists before SPOOL (use host mkdir)
host mkdir log 2>nul

var filename VARCHAR2(100)
BEGIN
    :filename := 'log/ROLLBACK_MARS_835_' || 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 PAUSE OFF

PROMPT =========================================================================
PROMPT MARS-835: Rollback CSDB Data Export
PROMPT =========================================================================
PROMPT WARNING: This will DELETE exported CSV and Parquet files from OCI buckets!
PROMPT          - DATA bucket: mrds_data_dev/ODS/CSDB/
PROMPT          - HIST bucket: mrds_hist_dev/ARCHIVE/CSDB/
PROMPT 
PROMPT Only proceed if export failed and needs to be restarted!
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

PROMPT 
PROMPT =========================================================================
PROMPT Step 1: Delete Group 2 Exported Files (HIST only - 4 tables)
PROMPT =========================================================================
@@92_MARS_835_rollback_step2.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 2: Delete Group 1 Exported Files (DATA + HIST - 2 tables)
PROMPT =========================================================================
@@91_MARS_835_rollback_step1.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 3: Delete File Registration Records from A_SOURCE_FILE_RECEIVED
PROMPT =========================================================================
@@90_MARS_835_rollback_file_registrations.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 4: Verify Rollback Completed
PROMPT =========================================================================
@@99_MARS_835_verify_rollback.sql

PROMPT 
PROMPT =========================================================================
PROMPT MARS-835 Rollback - COMPLETED
PROMPT =========================================================================

spool off

quit;
