<?php

namespace App\Database\Migrations\Elearning;

use CodeIgniter\Database\Migration;

class RPP extends Migration
{
    public function up()
    {

        $fields = [
            'id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'file' => ['type' => 'text', 'null' => true,],
            'user_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'kompetensi_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'tahun_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'created_at datetime default current_timestamp',
            'updated_at datetime default current_timestamp',
        ];
        $this->forge->addField($fields);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('kompetensi_id', 'kompetensi_dasar', 'id', false, 'CASCADE');
        $this->forge->addForeignKey('tahun_id', 'tahun_pelajaran', 'id', false, 'CASCADE');
        $this->forge->addForeignKey('user_id', 'users', 'id', false, 'CASCADE');
        $this->forge->createTable('rpp', true);
    }

    public function down()
    {
        // drop constraints first to prevent errors
        if ($this->db->DBDriver != 'SQLite3') {
            $this->forge->dropForeignKey('rpp', 'rpp_kompetensi_id_foreign');
            $this->forge->dropForeignKey('rpp', 'rpp_tahun_id_foreign');
            $this->forge->dropForeignKey('rpp', 'rpp_user_id_foreign');
        }
        $this->forge->dropTable('rpp', true);
    }
}
