-- Job Arranger upgrade table SQL for MySQL (Ver 7.2.2)  - 2026/03/03 -

-- Copyright (C) 2012-2014 FitechForce, Inc. All Rights Reserved.
-- Copyright (C) 2024 Daiwa Institute of Research Business Innovation Ltd. All Rights Reserved.

DROP PROCEDURE IF EXISTS AddTestRunTimeColumnIfNotExists;

DELIMITER $$

CREATE PROCEDURE AddTestRunTimeColumnIfNotExists()
BEGIN
    DECLARE col_exists INT DEFAULT 0;

    /* ==============================
       Table 1: ja_2_run_jobnet_table
       ============================== */
    SELECT COUNT(*)
    INTO col_exists
    FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'ja_2_run_jobnet_table'
      AND COLUMN_NAME = 'test_run_time';

    IF col_exists = 0 THEN
        ALTER TABLE ja_2_run_jobnet_table
        ADD COLUMN test_run_time BIGINT UNSIGNED NOT NULL DEFAULT 0;
    END IF;


    /* ==============================
       Table 2: ja_2_ran_jobnet_table
       ============================== */
    SELECT COUNT(*)
    INTO col_exists
    FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'ja_2_ran_jobnet_table'
      AND COLUMN_NAME = 'test_run_time';

    IF col_exists = 0 THEN
        ALTER TABLE ja_2_ran_jobnet_table
        ADD COLUMN test_run_time BIGINT UNSIGNED NOT NULL DEFAULT 0;
    END IF;

END$$

DELIMITER ;

-- Execute
CALL AddTestRunTimeColumnIfNotExists();

-- Optional cleanup
DROP PROCEDURE IF EXISTS AddTestRunTimeColumnIfNotExists;

CREATE INDEX `ja_job_idx_join_filter` ON `ja_2_run_job_table` (`inner_job_id`,`inner_jobnet_id`,`status`);
CREATE INDEX `ja_jend_exists_idx` ON `ja_2_run_job_table` (`end_time`,`inner_job_id`,`inner_jobnet_id`,`status`);
CREATE INDEX `ja_job_status0_idx` ON `ja_2_run_job_table` (`status`,`inner_job_id`,`inner_jobnet_id`);
CREATE INDEX `ja_job_start_status3_idx` ON `ja_2_run_job_table` (`inner_jobnet_id`,`status`,`inner_job_id`,`end_time`);

CREATE INDEX `ja_2_run_flow_idx_jobnet_start_end` ON `ja_2_run_flow_table` (`inner_jobnet_id`,`start_inner_job_id`,`end_inner_job_id`);
CREATE INDEX `ja_2_run_flow_idx_end` ON `ja_2_run_flow_table` (`end_inner_job_id`,`start_inner_job_id`);
