-- ===================================================================
-- MARS-1005 INSTALL SCRIPT: OU_TOP Historical Data Export to ODS Bucket
-- ===================================================================
-- Purpose: One-time bulk export of 6 OU_TOP LEGACY tables to OCI DATA bucket
--          (ODS bucket area, CSV format)
--          Uses DATA_EXPORTER EXPORT_TABLE_DATA with pRegisterExport for file tracking
-- Author: Grzegorz Michalski
-- Date: 2026-03-06

-- 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_1005_' || 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-1005: OU_TOP Historical Data Export to ODS Bucket (One-Time Migration)
PROMPT =========================================================================
PROMPT 
PROMPT This script will export 6 OU_TOP LEGACY tables to OCI DATA bucket:
PROMPT 
PROMPT TARGET: DATA Bucket / ODS area (CSV format):
PROMPT   - OU_TOP.LEGACY_ALLOTMENT                     -> ODS/TOP/TOP_ALLOTMENT
PROMPT   - OU_TOP.LEGACY_ALLOTMENT_MODIFICATION_HEADER -> ODS/TOP/TOP_ALLOTMENT_MODIFICATION_HEADER
PROMPT   - OU_TOP.LEGACY_ALLOTMENT_MODIFICATION_ITEM   -> ODS/TOP/TOP_ALLOTMENT_MODIFICATION_ITEM
PROMPT   - OU_TOP.LEGACY_ANNOUNCEMENT                  -> ODS/TOP/TOP_ANNOUNCEMENT
PROMPT   - OU_TOP.LEGACY_FBL_ITEM                      -> ODS/TOP/TOP_FULLBIDLIST_ITEM
PROMPT   - OU_TOP.LEGACY_FULLBID_ARRAY_COMPILED        -> ODS/TOP/TOP_FULLBID_ARRAY_COMPILED
PROMPT 
PROMPT Key Features:
PROMPT   - Files registered in A_SOURCE_FILE_RECEIVED with PROCESS_NAME = 'MARS-1005'
PROMPT   - Template table column order matching (CT_ET_TEMPLATES.TOP_*)
PROMPT   - ODS/TOP bucket path structure
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 Step 1: Export OU_TOP Data to ODS Bucket
PROMPT =========================================================================
@@01_MARS_1005_export_top_data.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 2: Verify Exports (File Registration Check)
PROMPT =========================================================================
@@02_MARS_1005_verify_exports.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 3: Verify Data Integrity (Source vs Exported)
PROMPT =========================================================================
@@03_MARS_1005_verify_data_integrity.sql

PROMPT 
PROMPT =========================================================================
PROMPT MARS-1005 Installation - COMPLETED
PROMPT =========================================================================
PROMPT Check the log file for complete installation details.
PROMPT For rollback, use: rollback_mars1005.sql
PROMPT =========================================================================

spool off

quit;