-- ============================================================================
-- MARS-1409 Package Version Tracking
-- ============================================================================
-- Purpose: Record package versions in A_PACKAGE_VERSION_TRACKING table
-- ============================================================================

SET SERVEROUTPUT ON SIZE UNLIMITED

PROMPT
PROMPT ============================================================================
PROMPT Recording Package Versions
PROMPT ============================================================================

DECLARE
    v_file_manager_version  VARCHAR2(50);
    v_file_manager_build    VARCHAR2(100);
    v_env_manager_version   VARCHAR2(50);
    v_env_manager_build     VARCHAR2(100);
    v_file_archiver_version VARCHAR2(50);
    v_file_archiver_build   VARCHAR2(100);
    v_data_exporter_version VARCHAR2(50);
    v_data_exporter_build   VARCHAR2(500);
BEGIN
    -- Get FILE_MANAGER version
    BEGIN
        v_file_manager_version := CT_MRDS.FILE_MANAGER.GET_VERSION();
        v_file_manager_build   := CT_MRDS.FILE_MANAGER.GET_BUILD_INFO();
        
        DBMS_OUTPUT.PUT_LINE('FILE_MANAGER Version: ' || v_file_manager_version);
        DBMS_OUTPUT.PUT_LINE('FILE_MANAGER Build: ' || v_file_manager_build);
    EXCEPTION
        WHEN OTHERS THEN
            DBMS_OUTPUT.PUT_LINE('WARNING: Could not retrieve FILE_MANAGER version');
    END;
    
    -- Get ENV_MANAGER version
    BEGIN
        v_env_manager_version := CT_MRDS.ENV_MANAGER.GET_VERSION();
        v_env_manager_build   := CT_MRDS.ENV_MANAGER.GET_BUILD_INFO();
        
        DBMS_OUTPUT.PUT_LINE('ENV_MANAGER Version: ' || v_env_manager_version);
        DBMS_OUTPUT.PUT_LINE('ENV_MANAGER Build: ' || v_env_manager_build);
    EXCEPTION
        WHEN OTHERS THEN
            DBMS_OUTPUT.PUT_LINE('WARNING: Could not retrieve ENV_MANAGER version');
    END;

    -- Get FILE_ARCHIVER version
    BEGIN
        v_file_archiver_version := CT_MRDS.FILE_ARCHIVER.GET_VERSION();
        v_file_archiver_build   := CT_MRDS.FILE_ARCHIVER.GET_BUILD_INFO();
        
        DBMS_OUTPUT.PUT_LINE('FILE_ARCHIVER Version: ' || v_file_archiver_version);
        DBMS_OUTPUT.PUT_LINE('FILE_ARCHIVER Build: ' || v_file_archiver_build);
    EXCEPTION
        WHEN OTHERS THEN
            DBMS_OUTPUT.PUT_LINE('WARNING: Could not retrieve FILE_ARCHIVER version');
    END;

    -- Get DATA_EXPORTER version
    BEGIN
        v_data_exporter_version := CT_MRDS.DATA_EXPORTER.GET_VERSION();
        v_data_exporter_build   := CT_MRDS.DATA_EXPORTER.GET_BUILD_INFO();
        
        DBMS_OUTPUT.PUT_LINE('DATA_EXPORTER Version: ' || v_data_exporter_version);
        DBMS_OUTPUT.PUT_LINE('DATA_EXPORTER Build: ' || v_data_exporter_build);
    EXCEPTION
        WHEN OTHERS THEN
            DBMS_OUTPUT.PUT_LINE('WARNING: Could not retrieve DATA_EXPORTER version');
    END;
    
    -- Insert version records into A_PACKAGE_VERSION_TRACKING
    BEGIN
        EXECUTE IMMEDIATE 'INSERT INTO CT_MRDS.A_PACKAGE_VERSION_TRACKING
            (PACKAGE_OWNER, PACKAGE_NAME, PACKAGE_TYPE, PACKAGE_VERSION,
             PACKAGE_BUILD_DATE, PACKAGE_AUTHOR, TRACKING_DATE, TRACKED_BY_USER, TRACKED_BY_MODULE)
            VALUES (:1, :2, :3, :4, :5, :6, SYSTIMESTAMP, USER, :7)'
        USING 'CT_MRDS', 'FILE_MANAGER', 'BOTH', v_file_manager_version,
              '', '', 'MARS-1409';
        
        EXECUTE IMMEDIATE 'INSERT INTO CT_MRDS.A_PACKAGE_VERSION_TRACKING
            (PACKAGE_OWNER, PACKAGE_NAME, PACKAGE_TYPE, PACKAGE_VERSION,
             PACKAGE_BUILD_DATE, PACKAGE_AUTHOR, TRACKING_DATE, TRACKED_BY_USER, TRACKED_BY_MODULE)
            VALUES (:1, :2, :3, :4, :5, :6, SYSTIMESTAMP, USER, :7)'
        USING 'CT_MRDS', 'ENV_MANAGER', 'BOTH', v_env_manager_version,
              '', '', 'MARS-1409';
        
        EXECUTE IMMEDIATE 'INSERT INTO CT_MRDS.A_PACKAGE_VERSION_TRACKING
            (PACKAGE_OWNER, PACKAGE_NAME, PACKAGE_TYPE, PACKAGE_VERSION,
             PACKAGE_BUILD_DATE, PACKAGE_AUTHOR, TRACKING_DATE, TRACKED_BY_USER, TRACKED_BY_MODULE)
            VALUES (:1, :2, :3, :4, :5, :6, SYSTIMESTAMP, USER, :7)'
        USING 'CT_MRDS', 'FILE_ARCHIVER', 'BOTH', v_file_archiver_version,
              '', '', 'MARS-1409';
        
        EXECUTE IMMEDIATE 'INSERT INTO CT_MRDS.A_PACKAGE_VERSION_TRACKING
            (PACKAGE_OWNER, PACKAGE_NAME, PACKAGE_TYPE, PACKAGE_VERSION,
             PACKAGE_BUILD_DATE, PACKAGE_AUTHOR, TRACKING_DATE, TRACKED_BY_USER, TRACKED_BY_MODULE)
            VALUES (:1, :2, :3, :4, :5, :6, SYSTIMESTAMP, USER, :7)'
        USING 'CT_MRDS', 'DATA_EXPORTER', 'BOTH', v_data_exporter_version,
              '', '', 'MARS-1409';
        
        COMMIT;
        DBMS_OUTPUT.PUT_LINE('Package version tracking recorded successfully');
    EXCEPTION
        WHEN OTHERS THEN
            DBMS_OUTPUT.PUT_LINE('ERROR: Could not record version tracking - ' || SQLERRM);
            RAISE;
    END;
END;
/

PROMPT
PROMPT ============================================================================
PROMPT Version Tracking Complete
PROMPT ============================================================================
