<?php

use Phalcon\Mvc\Model\Migration;
class InstansiSubTipeMigration_441 extends Migration {
    public function up() {
        self::$connection->execute("
            DROP TABLE IF EXISTS jaga.instansi_sub_tipe;
            CREATE TABLE jaga.instansi_sub_tipe (
                id serial primary key,
                id_tipe integer NOT NULL,
                nama character varying (100) NOT NULL,
                created_at timestamp(6) DEFAULT now(),
                updated_at timestamp(6),
                deleted_at timestamp(6)
            );
        ");

        self::$connection->execute("
            ALTER TABLE jaga.instansi ADD COLUMN IF NOT EXISTS id_sub_tipe integer;
        ");

        self::$connection->execute("
            INSERT INTO jaga.instansi_sub_tipe (id_tipe, nama) VALUES (2, 'Kabupaten');
            INSERT INTO jaga.instansi_sub_tipe (id_tipe, nama) VALUES (2, 'Kota');
        ");

        $sql = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'update_instansi.sql');
        self::$connection->execute($sql);
    }

    public function down() {
        self::$connection->execute("DROP TABLE IF EXISTS jaga.instansi_sub_tipe;");
        self::$connection->execute("ALTER TABLE jaga.instansi DROP COLUMN IF EXISTS id_sub_tipe;");
    }
}
