<?php

namespace App\Database\Migrations\Data;

use CodeIgniter\Database\Migration;

class KelasData extends Migration
{
    public function up()
    {
        $fields = [
            'kelas_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'unique' => true],
            'tingkat_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'null' => true],
            'jurusan_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'null' => true],
            'tahun_pelajaran_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'null' => true],
        ];

        $this->forge->addField($fields);
        $this->forge->addForeignKey('tingkat_id', 'm_tingkat', 'id', false, 'SET NULL');
        $this->forge->addForeignKey('jurusan_id', 'm_jurusan', 'id', false, 'SET NULL');
        $this->forge->addForeignKey('tahun_pelajaran_id', 'tahun_pelajaran', 'id', false, 'SET NULL');
        $this->forge->addForeignKey('kelas_id', 'm_kelas', 'id', false, 'CASCADE');
        $this->forge->createTable('kelas_data', true);
    }

    public function down()
    {
        // drop constraints first to prevent errors
        if ($this->db->DBDriver != 'SQLite3') {
            try {
                $this->forge->dropForeignKey('kelas_data', 'kelas_id');
            } catch (\Throwable $th) {
                //throw $th;
            }
            $this->forge->dropForeignKey('kelas_data', 'kelas_data_tingkat_id_foreign');
            $this->forge->dropForeignKey('kelas_data', 'kelas_data_jurusan_id_foreign');
            $this->forge->dropForeignKey('kelas_data', 'kelas_data_tahun_pelajaran_id_foreign');
        }
        $this->forge->dropTable('kelas_data', true);
    }
}
