-- ============================================================================
-- MARS-826-PREHOOK Master Installation Script
-- ============================================================================
-- Purpose: Deploy updated DATA_EXPORTER package with A_ETL_LOAD_SET_KEY support
-- Target Schema: CT_MRDS
-- Estimated Time: 1-2 minutes
-- Prerequisites: Database connection to CT_MRDS schema
-- ============================================================================

SET SERVEROUTPUT ON SIZE UNLIMITED
SET VERIFY OFF
SET FEEDBACK ON
SET ECHO OFF

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

-- Generate dynamic SPOOL filename with timestamp
var filename VARCHAR2(100)
BEGIN
    :filename := 'log/INSTALL_MARS_826_PREHOOK_' || SYS_CONTEXT('USERENV', 'CON_NAME') || '_' || TO_CHAR(SYSDATE,'YYYYMMDD_HH24MISS') || '.log';
END;
/
column filename new_value _filename
select :filename filename from dual;
spool &_filename

PROMPT
PROMPT ============================================================================
PROMPT MARS-826-PREHOOK Installation Starting
PROMPT ============================================================================
PROMPT Package: CT_MRDS.DATA_EXPORTER
PROMPT Change: Column reference A_WORKFLOW_HISTORY_KEY -> A_ETL_LOAD_SET_KEY
PROMPT Purpose: Support for renamed column in CT_ODS.A_LOAD_HISTORY
PROMPT Timestamp: 
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS install_start FROM DUAL;
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

-- Installation steps
PROMPT
PROMPT Step 1/5: Deploying DATA_EXPORTER Package Specification
PROMPT ========================================================
@@00_MARS_826_PREHOOK_install_DATA_EXPORTER_SPEC.sql

PROMPT
PROMPT Step 2/4: Deploying DATA_EXPORTER Package Body
PROMPT ===============================================
@@01_MARS_826_PREHOOK_install_DATA_EXPORTER_BODY.sql

PROMPT
PROMPT Step 3/4: Tracking Package Version
PROMPT =====================================
@@track_package_versions.sql

PROMPT
PROMPT Step 4/4: Verifying Package Status
PROMPT ======================================
@@verify_packages_version.sql

PROMPT
PROMPT ============================================================================
PROMPT MARS-826-PREHOOK Installation Completed
PROMPT ============================================================================
PROMPT Completion Time:
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS install_end FROM DUAL;
PROMPT
PROMPT Installation Summary:
PROMPT - Package: CT_MRDS.DATA_EXPORTER
PROMPT - Version: 2.1.0 -> 2.1.1 (PATCH)
PROMPT - Change: A_WORKFLOW_HISTORY_KEY -> A_ETL_LOAD_SET_KEY
PROMPT
PROMPT
PROMPT Log file: &_filename
PROMPT ============================================================================

spool off

quit;
