-- ===================================================================
-- MARS-956 ROLLBACK SCRIPT
-- ===================================================================
-- Purpose: Rollback/cleanup for MARS-956 C2D MPEC historical data export
-- Author: Grzegorz Michalski
-- Date: 2026-02-11
--
-- NOTE: This is primarily for cleanup of log entries and tracking data.
--       The exported CSV files would need to be manually removed from 
--       the DATA bucket if rollback is required.
-- ===================================================================

-- Start logging
spool rollback_mars956.log

PROMPT =========================================================================
PROMPT MARS-956 ROLLBACK - Cleanup Historical C2D MPEC Export
PROMPT =========================================================================
PROMPT Rollback Start: 
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS ROLLBACK_START FROM DUAL;

SET SERVEROUTPUT ON SIZE 1000000

PROMPT
PROMPT Step 1: Review Export Activity
PROMPT =========================================================================

-- Show what was exported
PROMPT Recent MARS-956 export activity:
SELECT TO_CHAR(EVENT_TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS') AS EXPORT_TIME,
       PROCEDURE_NAME,
       EVENT_TYPE,
       EVENT_MESSAGE
FROM CT_MRDS.A_PROCESS_LOG
WHERE PACKAGE_NAME = 'MARS-956'
   OR PROCEDURE_NAME LIKE '%MARS_956%'
ORDER BY EVENT_TIMESTAMP DESC;

PROMPT
PROMPT Step 2: Cleanup Log Entries (Optional)
PROMPT =========================================================================

-- Optionally remove MARS-956 log entries (uncomment if needed)
/*
DELETE FROM CT_MRDS.A_PROCESS_LOG 
WHERE PACKAGE_NAME = 'MARS-956'
   OR PROCEDURE_NAME LIKE '%MARS_956%';

PROMPT Deleted log entries: 
SELECT SQL%ROWCOUNT AS DELETED_ROWS FROM DUAL;

COMMIT;
*/

PROMPT Log cleanup skipped (uncomment DELETE statement if cleanup needed)

PROMPT
PROMPT Step 3: Manual Steps Required
PROMPT =========================================================================

PROMPT ⚠️  MANUAL CLEANUP REQUIRED:
PROMPT 
PROMPT If complete rollback is needed, manually remove CSV files from DATA bucket:
PROMPT   - mrds_data_dev/ODS/C2D/C2D_MPEC_ADMIN/*.csv
PROMPT   - mrds_data_dev/ODS/C2D/C2D_MPEC_CONTENT/*.csv
PROMPT   - mrds_data_dev/ODS/C2D/C2D_MPEC_CONTENT_CRITERION/*.csv
PROMPT
PROMPT Use OCI CLI or console to remove files:
PROMPT   oci os object list --bucket-name mrds_data_dev --prefix "ODS/C2D/C2D_MPEC"
PROMPT   oci os object delete --bucket-name mrds_data_dev --name "path/to/file.csv"

PROMPT
PROMPT =========================================================================
PROMPT MARS-956 ROLLBACK SUMMARY
PROMPT =========================================================================

PROMPT Rollback Completed:
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS ROLLBACK_END FROM DUAL;

PROMPT
PROMPT Note: This rollback script primarily cleans up log entries.
PROMPT      Exported CSV files require manual removal from DATA bucket.

spool off
quit;