-- ===================================================================
-- MARS-835 INSTALL SCRIPT: CSDB Data Export to External Tables
-- ===================================================================
-- Purpose: One-time bulk export of 6 CSDB tables from OU_CSDB schema
--          to OCI buckets (DATA/CSV and HIST/Parquet formats)
--          Uses DATA_EXPORTER v2.4.0 with per-column date format handling for data migration
-- Author: Grzegorz Michalski
-- Date: 2025-12-17
-- Version: 1.0.0

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

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

-- Set current schema context (optional - use when modifying packages in specific schema)
-- ALTER SESSION SET CURRENT_SCHEMA = CT_MRDS;

PROMPT =========================================================================
PROMPT MARS-835: CSDB Data Export to External Tables (One-Time Migration)
PROMPT =========================================================================
PROMPT 
PROMPT This script will export 6 CSDB tables to OCI buckets:
PROMPT 
PROMPT GROUP 1 - Split DATA + HIST (Time Critical):
PROMPT   - DEBT: Last 6 months to DATA, older to HIST
PROMPT   - DEBT_DAILY: Last 6 months to DATA, older to HIST
PROMPT 
PROMPT GROUP 2 - HIST Only (Weekend Bulk):
PROMPT   - INSTR_RAT_FULL, INSTR_DESC_FULL
PROMPT   - ISSUER_RAT_FULL, ISSUER_DESC_FULL
PROMPT 
PROMPT Column transformations:
PROMPT   - A_ETL_LOAD_SET_FK renamed to A_WORKFLOW_HISTORY_KEY (all tables)
PROMPT   - Legacy columns removed from DEBT and DEBT_DAILY
PROMPT 
PROMPT Expected Duration: 30-60 minutes (depends on data volume)
PROMPT =========================================================================

-- Confirm installation with user
ACCEPT continue CHAR PROMPT 'Type YES to continue with installation, 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: Verify existing files in DATA and HIST buckets
PROMPT =========================================================================
@@00_MARS_835_pre_check_existing_files.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 1: Export Group 1 - Split DATA + HIST (DEBT, DEBT_DAILY)
PROMPT =========================================================================
@@01_MARS_835_install_step1.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 2: Export Group 2 - HIST Only (4 tables)
PROMPT =========================================================================
@@02_MARS_835_install_step2.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 3: Verify Exports (File Presence Check)
PROMPT =========================================================================
@@03_MARS_835_verify_exports.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 4: Verify Record Counts (Source vs Archive)
PROMPT =========================================================================
@@04_MARS_835_verify_record_counts.sql

PROMPT 
PROMPT =========================================================================
PROMPT MARS-835 Installation - COMPLETED
PROMPT =========================================================================
PROMPT Check the log file for complete installation details.
PROMPT =========================================================================

spool off

quit;
