-- ===================================================================
-- MARS-835-PREHOOK INSTALL SCRIPT: Parallel Processing + Smart Column Mapping
-- ===================================================================
-- Purpose: Pre-hook for MARS-835 - Implement DBMS_PARALLEL_EXECUTE + Smart Column Mapping
-- Author: Grzegorz Michalski
-- Date: 2026-01-09
-- Version: ENV_MANAGER 3.2.0, DATA_EXPORTER 2.4.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_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

SET ECHO OFF
SET TIMING ON
SET SERVEROUTPUT ON SIZE UNLIMITED
SET PAUSE OFF

PROMPT =========================================================================
PROMPT MARS-835-PREHOOK: Parallel Processing + Smart Column Mapping
PROMPT =========================================================================
PROMPT 
PROMPT This script will:
PROMPT - Create A_PARALLEL_EXPORT_CHUNKS table with unique timestamp task names
PROMPT - Update A_SOURCE_FILE_RECEIVED table (rename ARCH_FILE_NAME to ARCH_PATH, add PROCESS_NAME column)
PROMPT - Update ENV_MANAGER to v3.2.0 (add parallel execution error codes)
PROMPT - Update DATA_EXPORTER to v2.4.0 (DBMS_PARALLEL_EXECUTE + Smart Column Mapping)
PROMPT - Add pParallelDegree parameter (1-16 threads) to EXPORT_*_BY_DATE procedures
PROMPT - Add pTargetTableOwner/pTargetTableName for CSV column order mapping
PROMPT - Fix unique constraint violations with auto-cleanup and timestamp task names
PROMPT 
PROMPT Expected Duration: 2-3 minutes
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(-20999, 'Installation aborted by user.');
    END IF;
END;
/
WHENEVER SQLERROR CONTINUE

PROMPT 
PROMPT =========================================================================
PROMPT Step 1: Create Parallel Export Chunks Table
PROMPT =========================================================================
@@00_MARS_835_PREHOOK_CREATE_PARALLEL_CHUNKS_TABLE.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 2: Update A_SOURCE_FILE_RECEIVED Table Structure
PROMPT =========================================================================
@@03_MARS_835_PREHOOK_update_SOURCE_FILE_RECEIVED_table.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 3: Deploy ENV_MANAGER Package
PROMPT =========================================================================
@@01_MARS_835_PREHOOK_install_ENV_MANAGER.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 4: Deploy DATA_EXPORTER Package
PROMPT =========================================================================
@@02_MARS_835_PREHOOK_install_DATA_EXPORTER.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 5: Track Package Versions
PROMPT =========================================================================
@@track_package_versions.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 6: Verify Package Versions
PROMPT =========================================================================
@@verify_packages_version.sql

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

spool off

quit;
