/**
 * migrate_testcases
 */
function migrate_testcases(&$dbHandler, $tableSet)
{
    // TL 1.8
    // --
    // -- Table structure for table "tcversions"
    // --
    // CREATE TABLE "tcversions" (
    //   "id" BIGINT NOT NULL DEFAULT '0' REFERENCES nodes_hierarchy (id),
    //   "tc_external_id" INT NULL,
    //   "version" INTEGER NOT NULL DEFAULT '1',
    //   "summary" TEXT NULL DEFAULT NULL,
    //   "steps" TEXT NULL DEFAULT NULL,
    //   "expected_results" TEXT NULL DEFAULT NULL,
    //   "importance" INT2 NOT NULL DEFAULT '2',
    //   "author_id" BIGINT NULL DEFAULT NULL REFERENCES users (id),
    //   "creation_ts" TIMESTAMP NOT NULL DEFAULT now(),
    //   "updater_id" BIGINT NULL DEFAULT NULL REFERENCES users (id),
    //   "modification_ts" TIMESTAMP NULL,
    //   "active" INT2 NOT NULL DEFAULT '1',
    //   "is_open" INT2 NOT NULL DEFAULT '1',
    //   "execution_type" INT2 NOT NULL DEFAULT '1',
    //   PRIMARY KEY ("id")
    // );
    // TL 1.9
    // --
    // -- Table structure for table "tcversions"
    // --
    // CREATE TABLE /*prefix*/tcversions(
    //   "id" BIGINT NOT NULL DEFAULT '0' REFERENCES  /*prefix*/nodes_hierarchy (id),
    //   "tc_external_id" INT NULL,
    //   "version" INTEGER NOT NULL DEFAULT '1',
    //   "layout" INTEGER NOT NULL DEFAULT '1',
    //   "summary" TEXT NULL DEFAULT NULL,
    //   "preconditions" TEXT NULL DEFAULT NULL,
    //   "importance" INT2 NOT NULL DEFAULT '2',
    //   "author_id" BIGINT NULL DEFAULT NULL REFERENCES  /*prefix*/users (id),
    //   "creation_ts" TIMESTAMP NOT NULL DEFAULT now(),
    //   "updater_id" BIGINT NULL DEFAULT NULL REFERENCES  /*prefix*/users (id),
    //   "modification_ts" TIMESTAMP NULL,
    //   "active" INT2 NOT NULL DEFAULT '1',
    //   "is_open" INT2 NOT NULL DEFAULT '1',
    //   "execution_type" INT2 NOT NULL DEFAULT '1',
    //   PRIMARY KEY ("id")
    // );
    //
    //
    // --
    // -- Table structure for table "tcsteps"
    // --
    // CREATE TABLE /*prefix*/tcsteps (
    //   "id" BIGINT NOT NULL DEFAULT '0' REFERENCES /*prefix*/nodes_hierarchy (id),
    //   "step_number" INT NOT NULL DEFAULT '1',
    //   "actions" TEXT NULL DEFAULT NULL,
    //   "expected_results" TEXT NULL DEFAULT NULL,
    //   "active" INT2 NOT NULL DEFAULT '1',
    //   "execution_type" INT2 NOT NULL DEFAULT '1',
    //   PRIMARY KEY ("id")
    // );
    // do test cases exist?
    $sql = " SELECT TCV.id,NHTC.name AS name FROM {$tableSet['tcversions']} TCV " . " JOIN {$tableSet['nodes_hierarchy']} NHV ON NHV.id=TCV.id " . " JOIN {$tableSet['nodes_hierarchy']} NHTC ON NHTC.id=NHV.parent_id ";
    echo 'Step - Test Case Migration - STARTED <br> ';
    $itemSet = $dbHandler->get_recordset($sql);
    if (!is_null($itemSet) && count($itemSet) > 0) {
        echo 'Working - Test Case Migration <br> ';
        $tree_mgr = new tree($dbHandler);
        $node_types_descr_id = $tree_mgr->get_available_node_types();
        $node_types_id_descr = array_flip($node_types_descr_id);
        // STEP 1 - Populate in bulk mode tcsteps table.
        //
        // ALL FIELDS - tcversions 1.8
        // 1.8 id,tc_external_id,version,summary,steps,expected_results,importance,author_id,creation_ts,updater_id,modification_ts,active,is_open,execution_type,
        // 1.9 id,tc_external_id,version,summary,importance,author_id,creation_ts,updater_id,modification_ts,active,is_open,execution_type,
        //
        // 1.9 tcsteps
        // id,step_number,actions,expected_results,active,execution_type
        //
        // NEEDED FIELDS
        $sql = " INSERT INTO {$tableSet['tcsteps']} " . " (id,actions,expected_results,active,execution_type) " . " SELECT id,steps,expected_results,active,execution_type " . " FROM {$tableSet['tcversions']}";
        $dbHandler->exec_query($sql);
        // STEP 2 - Create nodes for tcsteps on nodes_hierarchy table
        foreach ($itemSet as $dummy => $item_info) {
            // BUGID 4174 - do not show each migrated test case on migration log
            //echo "Test Case:" . $item_info['name'] . " Version DBID: {$item_info['id']} <br>";
            $item_id = $tree_mgr->new_node($item_info['id'], $node_types_descr_id['testcase_step']);
            if ($item_id > 0) {
                $sql = " UPDATE {$tableSet['tcsteps']} " . " SET id = {$item_id} WHERE id={$item_info['id']}";
                $dbHandler->exec_query($sql);
            } else {
                echo "<b> **** <br> CRITIC ***** <br>";
                echo "MIGRATION FAILURE - Unable to create Test Case Step NODE on nodes hierarchy <br></b>";
            }
        }
        // STEP 3 - Remove fields from tcversions
        $sql = "ALTER TABLE {$tableSet['tcversions']} DROP COLUMN steps";
        $dbHandler->exec_query($sql);
        $sql = "ALTER TABLE {$tableSet['tcversions']} DROP COLUMN expected_results ";
        $dbHandler->exec_query($sql);
    }
    echo 'Step - Test Case Migration - Finished !! <br><br> ';
}
/**
 * migrate_testcases
 */
function migrate_testcases(&$dbHandler, $tableSet)
{
    // TL 1.8
    // --
    // -- Table structure for table "tcversions"
    // --
    // CREATE TABLE "tcversions" (
    //   "id" BIGINT NOT NULL DEFAULT '0' REFERENCES nodes_hierarchy (id),
    //   "tc_external_id" INT NULL,
    //   "version" INTEGER NOT NULL DEFAULT '1',
    //   "summary" TEXT NULL DEFAULT NULL,
    //   "steps" TEXT NULL DEFAULT NULL,
    //   "expected_results" TEXT NULL DEFAULT NULL,
    //   "importance" INT2 NOT NULL DEFAULT '2',
    //   "author_id" BIGINT NULL DEFAULT NULL REFERENCES users (id),
    //   "creation_ts" TIMESTAMP NOT NULL DEFAULT now(),
    //   "updater_id" BIGINT NULL DEFAULT NULL REFERENCES users (id),
    //   "modification_ts" TIMESTAMP NULL,
    //   "active" INT2 NOT NULL DEFAULT '1',
    //   "is_open" INT2 NOT NULL DEFAULT '1',
    //   "execution_type" INT2 NOT NULL DEFAULT '1',
    //   PRIMARY KEY ("id")
    // );
    // TL 1.9
    // --
    // -- Table structure for table "tcversions"
    // --
    // CREATE TABLE /*prefix*/tcversions(
    //   "id" BIGINT NOT NULL DEFAULT '0' REFERENCES  /*prefix*/nodes_hierarchy (id),
    //   "tc_external_id" INT NULL,
    //   "version" INTEGER NOT NULL DEFAULT '1',
    //   "layout" INTEGER NOT NULL DEFAULT '1',
    //   "summary" TEXT NULL DEFAULT NULL,
    //   "preconditions" TEXT NULL DEFAULT NULL,
    //   "importance" INT2 NOT NULL DEFAULT '2',
    //   "author_id" BIGINT NULL DEFAULT NULL REFERENCES  /*prefix*/users (id),
    //   "creation_ts" TIMESTAMP NOT NULL DEFAULT now(),
    //   "updater_id" BIGINT NULL DEFAULT NULL REFERENCES  /*prefix*/users (id),
    //   "modification_ts" TIMESTAMP NULL,
    //   "active" INT2 NOT NULL DEFAULT '1',
    //   "is_open" INT2 NOT NULL DEFAULT '1',
    //   "execution_type" INT2 NOT NULL DEFAULT '1',
    //   PRIMARY KEY ("id")
    // );
    //
    //
    // --
    // -- Table structure for table "tcsteps"
    // --
    // CREATE TABLE /*prefix*/tcsteps (
    //   "id" BIGINT NOT NULL DEFAULT '0' REFERENCES /*prefix*/nodes_hierarchy (id),
    //   "step_number" INT NOT NULL DEFAULT '1',
    //   "actions" TEXT NULL DEFAULT NULL,
    //   "expected_results" TEXT NULL DEFAULT NULL,
    //   "active" INT2 NOT NULL DEFAULT '1',
    //   "execution_type" INT2 NOT NULL DEFAULT '1',
    //   PRIMARY KEY ("id")
    // );
    echo __FUNCTION__;
    // do test cases exist?
    $sql = "SELECT id FROM {$tableSet['tcversions']}";
    $itemSet = $dbHandler->get_recordset($sql);
    if (!is_null($itemSet) && count($itemSet) > 0) {
        $tree_mgr = new tree($dbHandler);
        $node_types_descr_id = $tree_mgr->get_available_node_types();
        $node_types_id_descr = array_flip($node_types_descr_id);
        // STEP 1 - Populate in bulk mode tcsteps table.
        //
        // ALL FIELDS - tcversions 1.8
        // 1.8 id,tc_external_id,version,summary,steps,expected_results,importance,author_id,creation_ts,updater_id,modification_ts,active,is_open,execution_type,
        // 1.9 id,tc_external_id,version,summary,importance,author_id,creation_ts,updater_id,modification_ts,active,is_open,execution_type,
        //
        // 1.9 tcsteps
        // id,step_number,actions,expected_results,active,execution_type
        //
        // NEEDED FIELDS
        $sql = " INSERT INTO {$tableSet['tcsteps']} " . " (id,actions,expected_results,active,execution_type) " . " SELECT id,steps,expected_results,active,execution_type " . " FROM {$tableSet['tcversions']}";
        $dbHandler->exec_query($sql);
        // STEP 2 - Create nodes for tcsteps on nodes_hierarchy table
        foreach ($itemSet as $dummy => $item_info) {
            $item_id = $tree_mgr->new_node($item_info['id'], $node_types_descr_id['testcase_step']);
            $sql = " UPDATE {$tableSet['tcsteps']} " . " SET id = {$item_id} WHERE id={$item_info['id']}";
            $dbHandler->exec_query($sql);
        }
        // STEP 3 - Remove fields from tcversions
        $sql = "ALTER TABLE {$tableSet['tcversions']} " . "DROP steps, DROP expected_results ";
        $dbHandler->exec_query($sql);
    }
}