<?php 

use Phalcon\Db\Column;
use Phalcon\Db\Index;
use Phalcon\Db\Reference;
use Phalcon\Mvc\Model\Migration;

/**
 * Class MasterKotaKabupatenMigration_103
 */
class MasterKotaKabupatenMigration_103 extends Migration
{
    /**
     * Define the table structure
     *
     * @return void
     */
    public function morph()
    {
        $this->morphTable('master_kota_kabupaten', [
                'columns' => [
                    new Column(
                        'kota_kabupaten_code',
                        [
                            'type' => Column::TYPE_CHAR,
                            'notNull' => true,
                            'size' => 4,
                            'first' => true
                        ]
                    ),
                    new Column(
                        'kota_kabupaten_name',
                        [
                            'type' => Column::TYPE_VARCHAR,
                            'notNull' => true,
							'size' => 255,
                            'after' => 'kota_kabupaten_code'
                        ]
                    ),
                    new Column(
                        'provinsi_code',
                        [
                            'type' => Column::TYPE_CHAR,
                            'size' => 2,
                            'after' => 'kota_kabupaten_name'
                        ]
                    )
                ],
                'indexes' => [
                    new Index('master_kota_kabupaten_pkey', ['kota_kabupaten_code'], 'PRIMARY KEY')
                ],
                'references' => [
                    new Reference(
                        'master_kota_kabupaten_provinsi_code_fkey',
                        [
                            'referencedTable' => 'master_provinsi',
                            'referencedSchema' => 'jaga',
                            'columns' => ['provinsi_code'],
                            'referencedColumns' => ['provinsi_code'],
                            'onUpdate' => 'CASCADE',
                            'onDelete' => 'NO ACTION'
                        ]
                    )
                ],
            ]
        );
    }

    /**
     * Run the migrations
     *
     * @return void
     */
    public function up()
    {
		self::$connection->execute('ALTER TABLE jaga.master_kota_kabupaten
			ADD COLUMN created_at timestamp without time zone NOT NULL DEFAULT now();
			ALTER TABLE jaga.master_kota_kabupaten
			ADD COLUMN updated_at timestamp without time zone NOT NULL DEFAULT now();
			ALTER TABLE jaga.master_kota_kabupaten disable trigger all;			
			');
        $this->batchInsert('master_kota_kabupaten', [
                'kota_kabupaten_code',
                'kota_kabupaten_name',
                'provinsi_code',
                'created_at',
                'updated_at'
            ]
        );
		
		self::$connection->execute('
			ALTER TABLE jaga.master_kota_kabupaten enable trigger all;
			
		');
    }

    /**
     * Reverse the migrations
     *
     * @return void
     */
    public function down()
    {
        $this->batchDelete('master_kota_kabupaten');
    }

}
