<?php
namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class RenameDictToMasterTables extends Migration {

    public function up() {
        $db = \Config\Database::connect();

        $db->query('SET FOREIGN_KEY_CHECKS=0');

        $tables = $db->listTables();
        if (in_array('dict_depts', $tables)) {
            $db->query('RENAME TABLE dict_depts TO master_depts');
        }
        if (in_array('dict_controls', $tables)) {
            $db->query('RENAME TABLE dict_controls TO master_controls');
        }
        if (in_array('dict_tests', $tables)) {
            $db->query('RENAME TABLE dict_tests TO master_tests');
        }

        $db->query('SET FOREIGN_KEY_CHECKS=1');
    }

    public function down() {
        $db = \Config\Database::connect();

        $db->query('SET FOREIGN_KEY_CHECKS=0');

        $tables = $db->listTables();
        if (in_array('master_depts', $tables)) {
            $db->query('RENAME TABLE master_depts TO dict_depts');
        }
        if (in_array('master_controls', $tables)) {
            $db->query('RENAME TABLE master_controls TO dict_controls');
        }
        if (in_array('master_tests', $tables)) {
            $db->query('RENAME TABLE master_tests TO dict_tests');
        }

        $db->query('SET FOREIGN_KEY_CHECKS=1');
    }
}
