--=============================================================================================================================
-- MARS-1096: Extend A_PROCESS_LOG.log_message and Enable Daily Interval Partitioning
--=============================================================================================================================
-- Purpose: 
-- - Extend log_message column from VARCHAR2(4000) to VARCHAR2(20000)
-- - Convert table to daily interval partitioning on log_timestamp
-- - Preserve all existing data during conversion
-- Author: Grzegorz Michalski
-- Date: 2025-12-03
-- Version: 1.0.0
--=============================================================================================================================

SET SERVEROUTPUT ON

PROMPT ========================================================================
PROMPT MARS-1096: Extend column and enable partitioning
PROMPT ========================================================================

-- 1) Extend column size (safe operation, preserves data)
ALTER TABLE CT_MRDS.A_PROCESS_LOG MODIFY (log_message VARCHAR2(20000));

-- 2) Convert table to interval partitioning by day on log_timestamp
-- Strategy: Create new partitioned table, copy ALL data, rename tables
-- This preserves all existing records during conversion

DECLARE
    v_has_partitions NUMBER := 0;
    v_row_count NUMBER := 0;
BEGIN
    -- Check if already partitioned
    SELECT COUNT(*) INTO v_has_partitions
    FROM user_tab_partitions
    WHERE table_name = 'A_PROCESS_LOG';

    IF v_has_partitions = 0 THEN
        -- Count existing data
        EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM CT_MRDS.A_PROCESS_LOG' INTO v_row_count;
        DBMS_OUTPUT.PUT_LINE('Existing records to migrate: ' || v_row_count);

        -- Create new partitioned table with different name
        EXECUTE IMMEDIATE q'[CREATE TABLE CT_MRDS.A_PROCESS_LOG_NEW (
            a_process_log_key   NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
            guid                    VARCHAR2(32),
            Username                VARCHAR2(128),
            Osuser                  VARCHAR2(128),
            Machine                 VARCHAR2(64),
            Module                  VARCHAR2(64),
            process_name            VARCHAR2(200),
            procedure_name          VARCHAR2(200),
            procedure_parameters    VARCHAR2(20000),
            log_level               VARCHAR2(10),
            log_message             VARCHAR2(20000),
            log_timestamp           TIMESTAMP DEFAULT SYSTIMESTAMP
        )
        PARTITION BY RANGE (log_timestamp)
        INTERVAL (NUMTODSINTERVAL(1,'DAY'))
        (
          PARTITION p_init VALUES LESS THAN (TIMESTAMP '2025-01-01 00:00:00')
        )]';

        -- Copy ALL existing data (Oracle auto-creates partitions per timestamp)
        EXECUTE IMMEDIATE 'INSERT /*+ APPEND */ INTO CT_MRDS.A_PROCESS_LOG_NEW (
            guid, Username, Osuser, Machine, Module, process_name, procedure_name,
            procedure_parameters, log_level, log_message, log_timestamp
        ) SELECT guid, Username, Osuser, Machine, Module, process_name, procedure_name,
                 procedure_parameters, log_level, log_message, log_timestamp
          FROM CT_MRDS.A_PROCESS_LOG';
        
        COMMIT;

        -- Verify data copied successfully
        EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM CT_MRDS.A_PROCESS_LOG_NEW' INTO v_row_count;
        DBMS_OUTPUT.PUT_LINE('Records migrated to partitioned table: ' || v_row_count);

        -- Swap tables: rename old to backup, new to original name
        EXECUTE IMMEDIATE 'ALTER TABLE CT_MRDS.A_PROCESS_LOG RENAME TO A_PROCESS_LOG_OLD';
        EXECUTE IMMEDIATE 'ALTER TABLE CT_MRDS.A_PROCESS_LOG_NEW RENAME TO A_PROCESS_LOG';

        DBMS_OUTPUT.PUT_LINE('Table conversion complete. Old table preserved as A_PROCESS_LOG_OLD');
        DBMS_OUTPUT.PUT_LINE('Review and manually DROP TABLE CT_MRDS.A_PROCESS_LOG_OLD PURGE when verified');
    ELSE
        DBMS_OUTPUT.PUT_LINE('Table already partitioned. Skipping conversion.');
    END IF;
END;
/

PROMPT SUCCESS: A_PROCESS_LOG extended and partitioned

--=============================================================================================================================
-- End of Script
--=============================================================================================================================
