<?php 

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

/**
 * Class ProdukMigration_115
 */
class ProdukMigration_115 extends Migration
{
    /**
     * Define the table structure
     *
     * @return void
     */
    public function morph()
    {				
		self::$connection->execute('ALTER TABLE jaga.produk disable trigger all;
		DROP TABLE produk');
        $this->morphTable('produk', [
                'columns' => [
					new Column(
                        'id',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'notNull' => true,
                            'autoIncrement' => true,
                            'after' => 'sort_order'
                        ]
                    ),
                    new Column(
                        'nama',
                        [
                            'type' => Column::TYPE_VARCHAR,
                            'notNull' => true,
                            'size' => 255,
                            'first' => true
                        ]
                    ),
                    new Column(
                        'icon',
                        [
                            'type' => Column::TYPE_VARCHAR,
                            'notNull' => true,
                            'size' => 255,
                            'after' => 'nama'
                        ]
                    ),
                    new Column(
                        'is_active',
                        [
                            'type' => Column::TYPE_BOOLEAN,
                            'default' => "true",
                            'after' => 'icon'
                        ]
                    ),
                    new Column(
                        'sort_order',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'notNull' => true,
                            'autoIncrement' => true,
                            'after' => 'is_active'
                        ]
                    ),                    
                    new Column(
                        'uuid',
                        [
                            'type' => Column::TYPE_CHAR,
                            'default' => "uuid_generate_v4()",
                            'notNull' => true,
                            'size' => 36,
                            'after' => 'id'
                        ]
                    )
                ],
                'indexes' => [                    
					new Index('produk_pkey', ['id'], 'PRIMARY KEY')
                ],
            ]
        );
    }

    /**
     * Run the migrations
     *
     * @return void
     */
    public function up()
    {
		self::$connection->execute('ALTER TABLE jaga.produk DROP COLUMN uuid');

		self::$connection->execute('ALTER TABLE jaga.produk ADD COLUMN uuid uuid NOT NULL DEFAULT jaga.uuid_generate_v4()');
    }

    /**
     * Reverse the migrations
     *
     * @return void
     */
    public function down()
    {

    }

    /**
     * This method is called after the table was created
     *
     * @return void
     */
     public function afterCreateTable()
     {		
        $this->batchInsert('produk', [
                'nama',
                'icon'                
            ]
        );
		self::$connection->execute('ALTER TABLE jaga.produk enable trigger all;			
		');
     }
}
