-- ===================================================================
-- MARS-1046 INSTALL SCRIPT: ISO 8601 Date Format Fix
-- ===================================================================
-- Purpose: Fix parsing of ISO 8601 datetime formats with milliseconds and timezone
--          Example: 2012-03-02T14:16:23.798+01:00
-- Author: Grzegorz Michalski
-- Date: 2025-11-27
-- Version: 1.0.0
--
-- Changes:
--   - FILE_MANAGER: 3.3.0 → 3.3.1 (PATCH - Bug fix)
--   - Added NORMALIZE_DATE_FORMAT private function
--   - Modified GENERATE_EXTERNAL_TABLE_PARAMS to normalize date formats
--
-- Prerequisites:
--   - Oracle Database 23ai
--   - ENV_MANAGER v3.1.0+
--   - FILE_MANAGER v3.3.0
--
-- Installation User: ADMIN (required for schema operations)
-- ===================================================================

-- Dynamic spool file generation (using SYS_CONTEXT - no DBA privileges required)
var filename VARCHAR2(100)
BEGIN
    :filename := 'INSTALL_MARS_1046_' || 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-1046: ISO 8601 Date Format Fix for FILE_MANAGER
PROMPT =========================================================================
PROMPT 
PROMPT This script will:
PROMPT - Update FILE_MANAGER package specification (3.3.0 → 3.3.1)
PROMPT - Update FILE_MANAGER package body with NORMALIZE_DATE_FORMAT function
PROMPT - Fix parsing of ISO 8601 formats: YYYY-MM-DDTHH24:MI:SS.FF3TZH:TZM
PROMPT - Track package version in A_PACKAGE_VERSION_TRACKING
PROMPT 
PROMPT Expected Duration: 1-2 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(-20001, 'Installation aborted by user');
    END IF;
END;
/
WHENEVER SQLERROR CONTINUE

PROMPT 
PROMPT =========================================================================
PROMPT Step 1: Update FILE_MANAGER Package Specification
PROMPT =========================================================================
@@01_MARS_1046_install_CT_MRDS_FILE_MANAGER_SPEC.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 2: Update FILE_MANAGER Package Body
PROMPT =========================================================================
@@02_MARS_1046_install_CT_MRDS_FILE_MANAGER_BODY.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 3: Track Package Version
PROMPT =========================================================================
@@track_package_versions.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 4: Verify Package Status
PROMPT =========================================================================
@@verify_packages_version.sql

PROMPT 
PROMPT =========================================================================
PROMPT MARS-1046 Installation - COMPLETED
PROMPT =========================================================================
PROMPT 
PROMPT FILE_MANAGER updated to version 3.3.1
PROMPT 
PROMPT Verification:
PROMPT   SELECT FILE_MANAGER.GET_VERSION() FROM DUAL;
PROMPT   SELECT FILE_MANAGER.GET_BUILD_INFO() FROM DUAL;
PROMPT 
PROMPT Check the log file for complete installation details:
PROMPT   &_filename
PROMPT =========================================================================

spool off
quit;
