-- ===================================================================
-- MARS-826 INSTALL SCRIPT: CSDB Historical Data Export to HIST Bucket
-- ===================================================================
-- Purpose: One-time export of 19 CSDB tables from OU_LM/OU_MRR to HIST bucket
-- Author: Grzegorz Michalski
-- Date: 2025-12-02
-- Version: 1.0.0
--
-- Description:
--   Exports historical data from operational CSDB tables to ARCHIVE/LM/ 
--   in HIST bucket with Parquet format and Hive-style partitioning.
--   Column mapping: A_ETL_LOAD_SET_KEY[_FK] -> A_WORKFLOW_HISTORY_KEY
--
-- Execution: ADMIN user required for cross-schema operations
-- Timing: Weekend bulk execution recommended (non-time-critical)
-- Duration: Estimated 2-4 hours for ~31M total records
-- ===================================================================

-- Create log directory if it doesn't exist
host mkdir log 2>nul

-- Dynamic spool file generation (using SYS_CONTEXT - no DBA privileges required)
-- Log files are automatically created in log/ subdirectory
var filename VARCHAR2(100)
BEGIN
    :filename := 'log/INSTALL_MARS_826_' || 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-826: CSDB Historical Data Export to HIST Bucket
PROMPT =========================================================================
PROMPT 
PROMPT This script will export 19 CSDB tables from OU_LM/OU_MRR schemas
PROMPT to HIST bucket in Parquet format.
PROMPT 
PROMPT Export Groups:
PROMPT   1. ADHOC_ADJ tables (3 tables, ~209 records)
PROMPT   2. BALANCESHEET tables (2 tables, ~7.6M records) - LARGE
PROMPT   3. CSM_ADJ tables (3 tables, ~7,756 records)
PROMPT   4. STANDING_FACILITY tables (2 tables, ~1.2M records) - LARGE
PROMPT   5. MRR_IND_CURR_ACC tables (2 tables)
PROMPT   6. FORECAST tables (2 tables, ~21.6M records) - VERY LARGE
PROMPT   7. QR_ADJ tables (3 tables, ~62,573 records)
PROMPT   8. TTS tables (2 tables, ~1,120 records)
PROMPT 
PROMPT Total: ~31 million records across 19 tables
PROMPT 
PROMPT Column Mapping: A_ETL_LOAD_SET_KEY[_FK] -> A_WORKFLOW_HISTORY_KEY
PROMPT 

PROMPT =========================================================================

-- Confirm installation with user
ACCEPT continue CHAR PROMPT 'Type YES to continue with data export, 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, 'Installation aborted by user');
    END IF;
END;
/
WHENEVER SQLERROR CONTINUE

PROMPT 
PROMPT =========================================================================
PROMPT Pre-Check: Existing Archive Files in HIST Bucket
PROMPT =========================================================================
@@00_MARS_826_pre_check_existing_files.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 1: Export ADHOC_ADJ Tables (3 tables)
PROMPT =========================================================================
@@01_MARS_826_export_ADHOC_ADJ_tables.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 2: Export BALANCESHEET Tables (2 tables, ~7.6M records)
PROMPT =========================================================================
@@02_MARS_826_export_BALANCESHEET_tables.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 3: Export CSM_ADJ Tables (3 tables)
PROMPT =========================================================================
@@03_MARS_826_export_CSM_ADJ_tables.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 4: Export STANDING_FACILITY Tables (2 tables, ~1.2M records)
PROMPT =========================================================================
@@04_MARS_826_export_STANDING_FACILITY_tables.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 5: Export MRR_IND_CURRENT_ACCOUNT Tables (2 tables)
PROMPT =========================================================================
@@05_MARS_826_export_MRR_IND_CURRENT_ACCOUNT_tables.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 6: Export FORECAST Tables (2 tables, ~21.6M records)
PROMPT =========================================================================
@@06_MARS_826_export_FORECAST_tables.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 7: Export QR_ADJ Tables (3 tables)
PROMPT =========================================================================
@@07_MARS_826_export_QR_ADJ_tables.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 8: Export TTS Tables (2 tables)
PROMPT =========================================================================
@@08_MARS_826_export_TTS_tables.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 9: Verify Exports
PROMPT =========================================================================
@@09_MARS_826_verify_exports.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 10: Verify Record Counts
PROMPT =========================================================================
@@10_MARS_826_verify_record_counts.sql

PROMPT 
PROMPT =========================================================================
PROMPT MARS-826 Installation - COMPLETED
PROMPT =========================================================================
PROMPT All 19 CSDB tables have been exported to HIST bucket.
PROMPT Check the log file for complete export details.
PROMPT 
PROMPT Next Steps:
PROMPT   1. Review log file for any errors or warnings
PROMPT   2. Verify record counts match source tables
PROMPT   3. Check HIST bucket for Parquet files with Hive partitioning
PROMPT =========================================================================

spool off

quit;
