<?php 

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

/**
 * Class KategoriDiskusiMigration_116
 */
class KategoriDiskusiMigration_116 extends Migration
{
    /**
     * Define the table structure
     *
     * @return void
     */
    public function morph()
    {
        $this->morphTable('kategori_diskusi', [
                'columns' => [
                    new Column(
                        'id',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'notNull' => true,
                            'autoIncrement' => true,
                            'first' => true
                        ]
                    ),
                    new Column(
                        'title',
                        [
                            'type' => Column::TYPE_VARCHAR,
                            'notNull' => true,
                            'size' => 255,
                            'after' => 'id'
                        ]
                    ),
                    new Column(
                        'created_at',
                        [
                            'type' => Column::TYPE_TIMESTAMP,
                            'default' => "2018-11-13 14:30:02.218961",
                            'notNull' => true,
                            'after' => 'title'
                        ]
                    ),
                    new Column(
                        'updated_at',
                        [
                            'type' => Column::TYPE_TIMESTAMP,
                            'default' => "2018-11-13 14:30:02.218961",
                            'after' => 'created_at'
                        ]
                    ),
                    new Column(
                        'uuid',
                        [
                            'type' => Column::TYPE_CHAR,
                            'default' => "uuid_generate_v4()",
                            'notNull' => true,
                            'size' => 36,
                            'after' => 'updated_at'
                        ]
                    ),
                    new Column(
                        'produk_id',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'notNull' => true,
                            'after' => 'uuid'
                        ]
                    )
                ],
                'indexes' => [
                    new Index('kategori_diskusi_pkey', ['id'], 'PRIMARY KEY')
                ],
				'references' => [
                    new Reference(
                        'kategori_diskusi_produk_id_fkey',
                        [
                            'referencedTable' => 'produk',
                            'referencedSchema' => 'jaga',
                            'columns' => ['produk_id'],
                            'referencedColumns' => ['id'],
                            'onUpdate' => 'NO ACTION',
                            'onDelete' => 'NO ACTION'
                        ]
                    )
                ]
            ]
        );
    }

    /**
     * Run the migrations
     *
     * @return void
     */
    public function up()
    {
		self::$connection->execute('ALTER TABLE jaga.kategori_diskusi DROP COLUMN uuid');
		self::$connection->execute('ALTER TABLE jaga.kategori_diskusi ADD COLUMN uuid uuid NOT NULL DEFAULT jaga.uuid_generate_v4()');
    }

    /**
     * Reverse the migrations
     *
     * @return void
     */
    public function down()
    {
		self::$connection->execute('DROP TABLE jaga.kategori_diskusi');
    }
	
	/**
     * This method is called after the table was created
     *
     * @return void
     */
     public function afterCreateTable()
     {
        $this->batchInsert('kategori_diskusi', [
                'title',
                'produk_id'                
            ]
        );
     }
}
