/*
================================================================================
MARS-1056: Master Installation Script for VARCHAR2 Definition Fixes
Author: Grzegorz Michalski
Date: November 25, 2025
Purpose: Main installation script for VARCHAR2 definition fixes in external tables

PROBLEM: 
FILE_MANAGER.GENERATE_EXTERNAL_TABLE_PARAMS uses data_length instead of char_length
for VARCHAR2 columns, causing incorrect definitions in external tables.

SOLUTION:
Modify column definition generation logic to preserve CHAR/BYTE semantics
from template tables.

USAGE:
sql 'CT_MRDS/Cloudpass#34@ggmichalski_high' '@install_mars1056.sql'
================================================================================
*/

-- Dynamic spool file generation (using SYS_CONTEXT - no DBA privileges required)
var filename VARCHAR2(100)
BEGIN
    :filename := 'INSTALL_MARS_1056_' || 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 ========================================================================
PROMPT   MARS-1056: VARCHAR2 Definition Fixes Installation
PROMPT ========================================================================
PROMPT
PROMPT Problem: FILE_MANAGER creates external tables with incorrect VARCHAR2 definitions
PROMPT Solution: Preserve CHAR/BYTE semantics from template tables
PROMPT
PROMPT Installation will:
PROMPT 1. Install updated FILE_MANAGER with VARCHAR2 fixes (v3.3.0)
PROMPT 2. Track package version
PROMPT 3. Verify all tracked packages
PROMPT
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

-- Set current schema context
ALTER SESSION SET CURRENT_SCHEMA = CT_MRDS;

PROMPT
PROMPT === Step 1: Install updated FILE_MANAGER package ===
@@01_MARS_1056_update_FILE_MANAGER_package.sql

PROMPT
PROMPT === Step 2: Track package version ===
@@track_package_versions.sql

PROMPT
PROMPT === Step 3: Verify all tracked packages ===
@@verify_packages_version.sql

PROMPT
PROMPT ========================================================================
PROMPT   MARS-1056: Installation completed successfully
PROMPT ========================================================================
PROMPT
PROMPT VARCHAR2 definition fixes have been implemented:
PROMPT - GENERATE_EXTERNAL_TABLE_PARAMS now preserves CHAR/BYTE semantics
PROMPT - External tables will match template table definitions
PROMPT - Test results shown above should indicate SUCCESS
PROMPT
PROMPT Next steps:
PROMPT 1. Verify test results above show all SUCCESS indicators
PROMPT 2. Test external table creation with various templates
PROMPT 3. Monitor for any issues with existing processes
PROMPT 4. If problems occur, use rollback_mars1056.sql
PROMPT
PROMPT ========================================================================
PROMPT Installation log saved to: &_filename
PROMPT ========================================================================

spool off

quit;
