-- =====================================================================
-- Script: 96_MARS_828_rollback_release01_configuration.sql
-- MARS Issue: MARS-828
-- Purpose: ROLLBACK archival configuration for Release 01 tables
-- Author: Grzegorz Michalski
-- Date: 2026-02-05
-- 
-- Description:
--   Rolls back archival configuration for 25 tables by setting all
--   archival parameters back to NULL (unconfigured state):
--   - ARCHIVAL_STRATEGY
--   - MINIMUM_AGE_MONTHS
--   - ARCHIVE_THRESHOLD_FILES_COUNT
--   - ARCHIVE_THRESHOLD_ROWS_COUNT
--   - ARCHIVE_THRESHOLD_BYTES_SUM
--   - HOURS_TO_EXPIRE_STATISTICS
--
--   This script reverts changes made by:
--   - 06_MARS_828_configure_release01_tables.sql
--
-- Dependencies:
--   - A_SOURCE_FILE_CONFIG table with archival columns
-- =====================================================================

PROMPT =====================================================================
PROMPT MARS-828: ROLLBACK - Remove Archival Configuration
PROMPT =====================================================================
PROMPT
PROMPT This script will REMOVE archival configuration from 25 tables
PROMPT
PROMPT WARNING: This will reset ALL archival parameters to NULL
PROMPT
PROMPT Tables affected:
PROMPT   - 19 LM tables
PROMPT   - 2 CSDB DEBT tables
PROMPT   - 4 CSDB rating/description tables
PROMPT
PROMPT Current timestamp: 
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS CURRENT_TIME FROM DUAL;
PROMPT
PROMPT =====================================================================
PROMPT SECTION 1: Rollback LM Tables Configuration
PROMPT =====================================================================

-- Rollback all 19 LM tables in a single statement
UPDATE CT_MRDS.A_SOURCE_FILE_CONFIG
SET ARCHIVAL_STRATEGY = NULL,
    MINIMUM_AGE_MONTHS = NULL,
    ODS_SCHEMA_NAME = NULL,
    ARCHIVE_THRESHOLD_FILES_COUNT = NULL,
    ARCHIVE_THRESHOLD_ROWS_COUNT = NULL,
    ARCHIVE_THRESHOLD_BYTES_SUM = NULL,
    HOURS_TO_EXPIRE_STATISTICS = NULL
WHERE SOURCE_FILE_TYPE = 'INPUT'
  AND A_SOURCE_KEY = 'LM'
  AND TABLE_ID IN (
    'LM_STANDING_FACILITIES',
    'LM_STANDING_FACILITIES_HEADER',
    'LM_TTS_HEADER',
    'LM_TTS_ITEM',
    'LM_ADHOC_ADJUSTMENTS_HEADER',
    'LM_ADHOC_ADJUSTMENTS_ITEM',
    'LM_ADHOC_ADJUSTMENTS_ITEM_HEADER',
    'LM_BALANCESHEET_HEADER',
    'LM_BALANCESHEET_ITEM',
    'LM_CSM_ADJUSTMENTS_HEADER',
    'LM_CSM_ADJUSTMENTS_ITEM',
    'LM_CSM_ADJUSTMENTS_ITEM_HEADER',
    'LM_CURRENT_ACCOUNTS_HEADER',
    'LM_CURRENT_ACCOUNTS_ITEM',
    'LM_FORECAST_HEADER',
    'LM_FORECAST_ITEM',
    'LM_QRE_ADJUSTMENTS_HEADER',
    'LM_QRE_ADJUSTMENTS_ITEM',
    'LM_QRE_ADJUSTMENTS_ITEM_HEADER'
  );

PROMPT
PROMPT LM tables rollback completed
PROMPT

PROMPT =====================================================================
PROMPT SECTION 2: Rollback CSDB DEBT Tables Configuration
PROMPT =====================================================================

-- Rollback CSDB DEBT tables
UPDATE CT_MRDS.A_SOURCE_FILE_CONFIG
SET ARCHIVAL_STRATEGY = NULL,
    MINIMUM_AGE_MONTHS = NULL,
    ODS_SCHEMA_NAME = NULL,
    ARCHIVE_THRESHOLD_FILES_COUNT = NULL,
    ARCHIVE_THRESHOLD_ROWS_COUNT = NULL,
    ARCHIVE_THRESHOLD_BYTES_SUM = NULL,
    HOURS_TO_EXPIRE_STATISTICS = NULL
WHERE SOURCE_FILE_TYPE = 'INPUT'
  AND A_SOURCE_KEY = 'CSDB'
  AND TABLE_ID IN ('CSDB_DEBT', 'CSDB_DEBT_DAILY');

PROMPT
PROMPT CSDB DEBT tables rollback completed
PROMPT

PROMPT =====================================================================
PROMPT SECTION 3: Rollback CSDB Rating/Description Tables Configuration
PROMPT =====================================================================

-- Rollback CSDB rating/description tables
UPDATE CT_MRDS.A_SOURCE_FILE_CONFIG
SET ARCHIVAL_STRATEGY = NULL,
    MINIMUM_AGE_MONTHS = NULL,
    ODS_SCHEMA_NAME = NULL,
    ARCHIVE_THRESHOLD_FILES_COUNT = NULL,
    ARCHIVE_THRESHOLD_ROWS_COUNT = NULL,
    ARCHIVE_THRESHOLD_BYTES_SUM = NULL,
    HOURS_TO_EXPIRE_STATISTICS = NULL
WHERE SOURCE_FILE_TYPE = 'INPUT'
  AND A_SOURCE_KEY = 'CSDB'
  AND TABLE_ID IN (
    'CSDB_INSTR_RAT_FULL',
    'CSDB_INSTR_DESC_FULL',
    'CSDB_ISSUER_RAT_FULL',
    'CSDB_ISSUER_DESC_FULL'
  );

PROMPT
PROMPT CSDB rating/description tables rollback completed
PROMPT

PROMPT =====================================================================
PROMPT Committing rollback changes...
PROMPT =====================================================================

COMMIT;

PROMPT
PROMPT Rollback committed successfully.
PROMPT

PROMPT =====================================================================
PROMPT VERIFICATION: Archival Configuration Removed
PROMPT =====================================================================

PROMPT
PROMPT LM Tables (should all be NULL):
PROMPT

SELECT 
    TABLE_ID,
    ARCHIVAL_STRATEGY,
    MINIMUM_AGE_MONTHS,
    ARCHIVE_THRESHOLD_FILES_COUNT AS FILE_THR,
    ARCHIVE_THRESHOLD_ROWS_COUNT AS ROW_THR,
    ARCHIVE_THRESHOLD_BYTES_SUM AS BYTE_THR,
    HOURS_TO_EXPIRE_STATISTICS AS STATS_HRS,
    CASE 
        WHEN ARCHIVAL_STRATEGY IS NULL 
         AND MINIMUM_AGE_MONTHS IS NULL
         AND ARCHIVE_THRESHOLD_FILES_COUNT IS NULL
         AND ARCHIVE_THRESHOLD_ROWS_COUNT IS NULL
         AND ARCHIVE_THRESHOLD_BYTES_SUM IS NULL
         AND HOURS_TO_EXPIRE_STATISTICS IS NULL
        THEN 'OK'
        ELSE 'ERROR - Still configured'
    END AS STATUS
FROM CT_MRDS.A_SOURCE_FILE_CONFIG
WHERE A_SOURCE_KEY = 'LM'
  AND SOURCE_FILE_TYPE = 'INPUT'
  AND TABLE_ID LIKE 'LM_%'
ORDER BY TABLE_ID;

PROMPT
PROMPT CSDB DEBT Tables (should all be NULL):
PROMPT

SELECT 
    TABLE_ID,
    ARCHIVAL_STRATEGY,
    MINIMUM_AGE_MONTHS,
    ARCHIVE_THRESHOLD_FILES_COUNT AS FILE_THR,
    ARCHIVE_THRESHOLD_ROWS_COUNT AS ROW_THR,
    ARCHIVE_THRESHOLD_BYTES_SUM AS BYTE_THR,
    HOURS_TO_EXPIRE_STATISTICS AS STATS_HRS,
    CASE 
        WHEN ARCHIVAL_STRATEGY IS NULL 
         AND MINIMUM_AGE_MONTHS IS NULL
         AND ARCHIVE_THRESHOLD_FILES_COUNT IS NULL
         AND ARCHIVE_THRESHOLD_ROWS_COUNT IS NULL
         AND ARCHIVE_THRESHOLD_BYTES_SUM IS NULL
         AND HOURS_TO_EXPIRE_STATISTICS IS NULL
        THEN 'OK'
        ELSE 'ERROR - Still configured'
    END AS STATUS
FROM CT_MRDS.A_SOURCE_FILE_CONFIG
WHERE A_SOURCE_KEY = 'CSDB'
  AND SOURCE_FILE_TYPE = 'INPUT'
  AND TABLE_ID IN ('CSDB_DEBT', 'CSDB_DEBT_DAILY')
ORDER BY TABLE_ID;

PROMPT
PROMPT CSDB Rating/Description Tables (should all be NULL):
PROMPT

SELECT 
    TABLE_ID,
    ARCHIVAL_STRATEGY,
    MINIMUM_AGE_MONTHS,
    ARCHIVE_THRESHOLD_FILES_COUNT AS FILE_THR,
    ARCHIVE_THRESHOLD_ROWS_COUNT AS ROW_THR,
    ARCHIVE_THRESHOLD_BYTES_SUM AS BYTE_THR,
    HOURS_TO_EXPIRE_STATISTICS AS STATS_HRS,
    CASE 
        WHEN ARCHIVAL_STRATEGY IS NULL 
         AND MINIMUM_AGE_MONTHS IS NULL
         AND ARCHIVE_THRESHOLD_FILES_COUNT IS NULL
         AND ARCHIVE_THRESHOLD_ROWS_COUNT IS NULL
         AND ARCHIVE_THRESHOLD_BYTES_SUM IS NULL
         AND HOURS_TO_EXPIRE_STATISTICS IS NULL
        THEN 'OK'
        ELSE 'ERROR - Still configured'
    END AS STATUS
FROM CT_MRDS.A_SOURCE_FILE_CONFIG
WHERE A_SOURCE_KEY = 'CSDB'
  AND SOURCE_FILE_TYPE = 'INPUT'
  AND TABLE_ID IN ('CSDB_INSTR_RAT_FULL', 'CSDB_INSTR_DESC_FULL', 'CSDB_ISSUER_RAT_FULL', 'CSDB_ISSUER_DESC_FULL')
ORDER BY TABLE_ID;

PROMPT
PROMPT =====================================================================
PROMPT Summary: Total Tables with Rollback Applied
PROMPT =====================================================================

SELECT 
    COUNT(*) AS TOTAL_TABLES,
    SUM(CASE WHEN ARCHIVAL_STRATEGY IS NULL THEN 1 ELSE 0 END) AS STRATEGY_NULL,
    SUM(CASE WHEN MINIMUM_AGE_MONTHS IS NULL THEN 1 ELSE 0 END) AS MIN_AGE_NULL,
    SUM(CASE WHEN FILES_COUNT_OVER_ARCHIVE_THRESHOLD IS NULL THEN 1 ELSE 0 END) AS FILE_THR_NULL,
    SUM(CASE WHEN ROWS_COUNT_OVER_ARCHIVE_THRESHOLD IS NULL THEN 1 ELSE 0 END) AS ROWS_THR_NULL,
    SUM(CASE WHEN BYTES_SUM_OVER_ARCHIVE_THRESHOLD IS NULL THEN 1 ELSE 0 END) AS BYTES_THR_NULL,
    SUM(CASE WHEN HOURS_TO_EXPIRE_STATISTICS IS NULL THEN 1 ELSE 0 END) AS STATS_NULL
FROM CT_MRDS.A_SOURCE_FILE_CONFIG
WHERE SOURCE_FILE_TYPE = 'INPUT'
  AND ((A_SOURCE_KEY = 'LM' AND TABLE_ID LIKE 'LM_%')
    OR (A_SOURCE_KEY = 'CSDB' AND TABLE_ID IN (
        'CSDB_DEBT', 'CSDB_DEBT_DAILY', 
        'CSDB_INSTR_RAT_FULL', 'CSDB_INSTR_DESC_FULL', 
        'CSDB_ISSUER_RAT_FULL', 'CSDB_ISSUER_DESC_FULL'
      )));

PROMPT
PROMPT Expected: All counts should be 25 (all parameters NULL for all tables)
PROMPT   - TOTAL_TABLES = 25
PROMPT   - STRATEGY_NULL = 25
PROMPT   - MIN_AGE_NULL = 25
PROMPT   - FILE_THR_NULL = 25
PROMPT   - ROWS_THR_NULL = 25
PROMPT   - BYTES_THR_NULL = 25
PROMPT   - STATS_NULL = 25
PROMPT

PROMPT =====================================================================
PROMPT Rollback Complete
PROMPT =====================================================================
PROMPT
PROMPT Next Steps:
PROMPT 1. Review verification results above
PROMPT 2. All 25 tables should show STATUS = 'OK'
PROMPT 3. All archival parameters should be NULL
PROMPT 4. Tables are now in unconfigured state (no archival strategy)
PROMPT
PROMPT To restore configuration, re-run:
PROMPT   @06_MARS_828_configure_release01_tables.sql
PROMPT
PROMPT Log file: 96_MARS_828_rollback_release01_configuration.log
PROMPT =====================================================================
