<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_Add_user_jawaban extends CI_Migration {

    public function up() {
        $this->dbforge->add_field([
            'user_form_id' => [
                'type' => 'BIGINT',
                'constraint' => 20,
                'unsigned' => true,
            ],
            'pertanyaan_id' => [
                'type' => 'BIGINT',
                'constraint' => 20,
                'unsigned' => true,
                'auto_increment' => true
            ],
            'pilihan_id' => [
                'type' => 'BIGINT',
                'constraint' => 20,
                'unsigned' => true,
            ],
            'nilai' => [
                'type' => 'TEXT',
                'null' => true
            ],
            'bobot' => [
                'type' => 'SMALLINT',
                'constraint' => 5,
                'null' => true,
                'default' => 0,
            ],
            'active' => [
                'type' => 'SMALLINT',
                'constraint' => 1,
                'null' => true,
                'default' => 1,
                'comment' => '0 = INACTIVE, 1 = ACTIVE'
            ],
            'created_at' => [
                'type' => 'timestamp',
                'null' => true
            ],
            'updated_at' => [
                'type' => 'timestamp',
                'null' => true
            ],
            'deleted_at' => [
                'type' => 'timestamp',
                'null' => true
            ],
            'created_by' => [
                'type' => 'BIGINT',
                'constraint' => 20,
                'unsigned' => true,
                'null' => true
            ],
            'updated_by' => [
                'type' => 'BIGINT',
                'constraint' => 20,
                'unsigned' => true,
                'null' => true
            ],
            'deleted_by' => [
                'type' => 'BIGINT',
                'constraint' => 20,
                'unsigned' => true,
                'null' => true
            ],
            'CONSTRAINT ' . $this->db->dbprefix . 'user_jawaban_user_form FOREIGN KEY (user_form_id) REFERENCES ' . $this->db->dbprefix . 'user_form(id) ON UPDATE CASCADE ON DELETE CASCADE',
            'CONSTRAINT ' . $this->db->dbprefix . 'user_jawaban_petanyaan FOREIGN KEY (pertanyaan_id) REFERENCES ' . $this->db->dbprefix . 'form_pertanyaan(id) ON UPDATE CASCADE ON DELETE CASCADE',
//            'CONSTRAINT ' . $this->db->dbprefix . 'user_jawaban_pilihan FOREIGN KEY (pilihan_id) REFERENCES ' . $this->db->dbprefix . 'form_pilihan(id) ON UPDATE CASCADE ON DELETE CASCADE'
                // cannot create foreign key because target not auto_increment
        ]);
        $this->dbforge->add_key('id', true);
        $this->dbforge->create_table('user_jawaban');
    }

    //--------------------------------------------------------------------

    public function down() {
        $this->dbforge->drop_table('user_jawaban');
    }

}
