--=============================================================================================================================
-- MARS-835 ROLLBACK: Delete Group 2 Exported Files (4 HIST-only tables)
--=============================================================================================================================
-- Purpose: Delete exported Parquet files from HIST bucket
-- WARNING: This will permanently delete exported data files!
-- Author: Grzegorz Michalski
-- Date: 2025-12-17
-- Related: MARS-835 - CSDB Data Export Rollback
--=============================================================================================================================

SET SERVEROUTPUT ON SIZE UNLIMITED

PROMPT ========================================================================
PROMPT ROLLBACK: Deleting INSTR_RAT_FULL exported files
PROMPT ========================================================================

DECLARE
    vBucketUri VARCHAR2(500);
    vCredentialName VARCHAR2(100);
BEGIN
    -- Get bucket URI and credential
    vBucketUri := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('ARCHIVE');
    vCredentialName := CT_MRDS.ENV_MANAGER.gvCredentialName;

    DBMS_OUTPUT.PUT_LINE('Deleting INSTR_RAT_FULL files from HIST bucket...');
    
    FOR rec IN (
        SELECT object_name 
        FROM TABLE(DBMS_CLOUD.LIST_OBJECTS(
            credential_name => vCredentialName,
            location_uri => vBucketUri || 'ARCHIVE/CSDB/CSDB_INSTR_RAT_FULL/'
        ))
        WHERE object_name LIKE '%PARTITION_YEAR=%'  -- Hive-style partitioning folders
          AND object_name LIKE '%.parquet'
          AND REGEXP_LIKE(object_name, '[0-9]{6}(_[0-9]+_[0-9]{8}T[0-9]{6,}Z)?\.parquet$')  -- YYYYMM or YYYYMM_1_timestamp
    ) LOOP
        BEGIN
            DBMS_CLOUD.DELETE_OBJECT(
                credential_name => vCredentialName,
                object_uri => vBucketUri || 'ARCHIVE/CSDB/CSDB_INSTR_RAT_FULL/' || rec.object_name
            );
            DBMS_OUTPUT.PUT_LINE('  Deleted: ' || rec.object_name);
        EXCEPTION
            WHEN OTHERS THEN
                IF SQLCODE = -20404 THEN
                    DBMS_OUTPUT.PUT_LINE('  Skipped (not found): ' || rec.object_name);
                ELSE
                    RAISE;
                END IF;
        END;
    END LOOP;
    
    DBMS_OUTPUT.PUT_LINE('SUCCESS: INSTR_RAT_FULL files deleted');
END;
/

PROMPT ========================================================================
PROMPT ROLLBACK: Deleting INSTR_DESC_FULL exported files
PROMPT ========================================================================

DECLARE
    vBucketUri VARCHAR2(500);
    vCredentialName VARCHAR2(100);
BEGIN
    -- Get bucket URI and credential
    vBucketUri := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('ARCHIVE');
    vCredentialName := CT_MRDS.ENV_MANAGER.gvCredentialName;

    DBMS_OUTPUT.PUT_LINE('Deleting INSTR_DESC_FULL files from HIST bucket...');
    
    FOR rec IN (
        SELECT object_name 
        FROM TABLE(DBMS_CLOUD.LIST_OBJECTS(
            credential_name => vCredentialName,
            location_uri => vBucketUri || 'ARCHIVE/CSDB/CSDB_INSTR_DESC_FULL/'
        ))
        WHERE object_name LIKE '%PARTITION_YEAR=%'  -- Hive-style partitioning folders
          AND object_name LIKE '%.parquet'
          AND REGEXP_LIKE(object_name, '[0-9]{6}(_[0-9]+_[0-9]{8}T[0-9]{6,}Z)?\.parquet$')  -- YYYYMM or YYYYMM_1_timestamp
    ) LOOP
        BEGIN
            DBMS_CLOUD.DELETE_OBJECT(
                credential_name => vCredentialName,
                object_uri => vBucketUri || 'ARCHIVE/CSDB/CSDB_INSTR_DESC_FULL/' || rec.object_name
            );
            DBMS_OUTPUT.PUT_LINE('  Deleted: ' || rec.object_name);
        EXCEPTION
            WHEN OTHERS THEN
                IF SQLCODE = -20404 THEN
                    DBMS_OUTPUT.PUT_LINE('  Skipped (not found): ' || rec.object_name);
                ELSE
                    RAISE;
                END IF;
        END;
    END LOOP;
    
    DBMS_OUTPUT.PUT_LINE('SUCCESS: INSTR_DESC_FULL files deleted');
END;
/

PROMPT ========================================================================
PROMPT ROLLBACK: Deleting ISSUER_RAT_FULL exported files
PROMPT ========================================================================

DECLARE
    vBucketUri VARCHAR2(500);
    vCredentialName VARCHAR2(100);
BEGIN
    -- Get bucket URI and credential
    vBucketUri := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('ARCHIVE');
    vCredentialName := CT_MRDS.ENV_MANAGER.gvCredentialName;

    DBMS_OUTPUT.PUT_LINE('Deleting ISSUER_RAT_FULL files from HIST bucket...');
    
    FOR rec IN (
        SELECT object_name 
        FROM TABLE(DBMS_CLOUD.LIST_OBJECTS(
            credential_name => vCredentialName,
            location_uri => vBucketUri || 'ARCHIVE/CSDB/CSDB_ISSUER_RAT_FULL/'
        ))
        WHERE object_name LIKE '%PARTITION_YEAR=%'  -- Hive-style partitioning folders
          AND object_name LIKE '%.parquet'
          AND REGEXP_LIKE(object_name, '[0-9]{6}(_[0-9]+_[0-9]{8}T[0-9]{6,}Z)?\.parquet$')  -- YYYYMM or YYYYMM_1_timestamp
    ) LOOP
        BEGIN
            DBMS_CLOUD.DELETE_OBJECT(
                credential_name => vCredentialName,
                object_uri => vBucketUri || 'ARCHIVE/CSDB/CSDB_ISSUER_RAT_FULL/' || rec.object_name
            );
            DBMS_OUTPUT.PUT_LINE('  Deleted: ' || rec.object_name);
        EXCEPTION
            WHEN OTHERS THEN
                IF SQLCODE = -20404 THEN
                    DBMS_OUTPUT.PUT_LINE('  Skipped (not found): ' || rec.object_name);
                ELSE
                    RAISE;
                END IF;
        END;
    END LOOP;
    
    DBMS_OUTPUT.PUT_LINE('SUCCESS: ISSUER_RAT_FULL files deleted');
END;
/

PROMPT ========================================================================
PROMPT ROLLBACK: Deleting ISSUER_DESC_FULL exported files
PROMPT ========================================================================

DECLARE
    vBucketUri VARCHAR2(500);
    vCredentialName VARCHAR2(100);
BEGIN
    -- Get bucket URI and credential
    vBucketUri := CT_MRDS.FILE_MANAGER.GET_BUCKET_URI('ARCHIVE');
    vCredentialName := CT_MRDS.ENV_MANAGER.gvCredentialName;

    DBMS_OUTPUT.PUT_LINE('Deleting ISSUER_DESC_FULL files from HIST bucket...');
    
    FOR rec IN (
        SELECT object_name 
        FROM TABLE(DBMS_CLOUD.LIST_OBJECTS(
            credential_name => vCredentialName,
            location_uri => vBucketUri || 'ARCHIVE/CSDB/CSDB_ISSUER_DESC_FULL/'
        ))
        WHERE object_name LIKE '%PARTITION_YEAR=%'  -- Hive-style partitioning folders
          AND object_name LIKE '%.parquet'
          AND REGEXP_LIKE(object_name, '[0-9]{6}(_[0-9]+_[0-9]{8}T[0-9]{6,}Z)?\.parquet$')  -- YYYYMM or YYYYMM_1_timestamp
    ) LOOP
        BEGIN
            DBMS_CLOUD.DELETE_OBJECT(
                credential_name => vCredentialName,
                object_uri => vBucketUri || 'ARCHIVE/CSDB/CSDB_ISSUER_DESC_FULL/' || rec.object_name
            );
            DBMS_OUTPUT.PUT_LINE('  Deleted: ' || rec.object_name);
        EXCEPTION
            WHEN OTHERS THEN
                IF SQLCODE = -20404 THEN
                    DBMS_OUTPUT.PUT_LINE('  Skipped (not found): ' || rec.object_name);
                ELSE
                    RAISE;
                END IF;
        END;
    END LOOP;
    
    DBMS_OUTPUT.PUT_LINE('SUCCESS: ISSUER_DESC_FULL files deleted');
END;
/

PROMPT SUCCESS: Group 2 rollback completed

--=============================================================================================================================
-- End of Script
--=============================================================================================================================
