<?php 

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

/**
 * Class DiskusiMigration_117
 */
class DiskusiMigration_117 extends Migration
{
    /**
     * Define the table structure
     *
     * @return void
     */
    public function morph()
    {
        $this->morphTable('diskusi', [
                'columns' => [					
					new Column(
                        'id',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'notNull' => true,
                            'autoIncrement' => true,
                            'first' => true
                        ]
                    ),
                    new Column(
                        'uuid',
                        [
                            'type' => Column::TYPE_CHAR,
                            'notNull' => true,
                            'size' => 36,
                            'after' => 'kategori_diskusi_id',
							'default' => 'jaga.uuid_generate_v4()'
							
                        ]
                    ),
                    new Column(
                        'title',
                        [
                            'type' => Column::TYPE_VARCHAR,
                            'notNull' => true,
                            'size' => 255,
                            'first' => true
                        ]
                    ),
                    new Column(
                        'content',
                        [
                            'type' => Column::TYPE_TEXT,
                            'notNull' => true,
                            'size' => 1,
                            'after' => 'title'
                        ]
                    ),
                    new Column(
                        'user_id',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'notNull' => true,
                            'after' => 'content'
                        ]
                    ),
                    new Column(
                        'tenant_id',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'default' => "-1234",
                            'notNull' => true,
                            'after' => 'user_id'
                        ]
                    ),
                    new Column(
                        'proof_url',
                        [
                            'type' => Column::TYPE_VARCHAR,
                            'size' => 255,
                            'after' => 'tenant_id'
                        ]
                    ),
                    new Column(
                        'generated_url',
                        [
                            'type' => Column::TYPE_VARCHAR,
                            'size' => 255,
                            'after' => 'proof_url'
                        ]
                    ),
                    new Column(
                        'is_anonymous',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'default' => "0",
                            'notNull' => true,
                            'after' => 'generated_url'
                        ]
                    ),
                    new Column(
                        'kategori_diskusi_id',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'notNull' => true,
                            'after' => 'is_anonymous'
                        ]
                    )
                ],
                'indexes' => [
                    new Index('diskusi_pkey', ['id'], 'PRIMARY KEY')
                ],
				'references' => [
                    new Reference(
                        'diskusi_kategori_diskusi_id_fkey',
                        [
                            'referencedTable' => 'kategori_diskusi',
                            'referencedSchema' => 'jaga',
                            'columns' => ['kategori_diskusi_id'],
                            'referencedColumns' => ['id'],
                            'onUpdate' => 'NO ACTION',
                            'onDelete' => 'NO ACTION'
                        ]
                    )
                ]
            ]
        );
    }

    /**
     * Run the migrations
     *
     * @return void
     */
    public function up()
    {
		self::$connection->execute('ALTER TABLE jaga.diskusi DROP COLUMN uuid');
		self::$connection->execute('ALTER TABLE jaga.diskusi ADD COLUMN uuid uuid NOT NULL DEFAULT jaga.uuid_generate_v4()');
		self::$connection->execute('ALTER TABLE jaga.diskusi 
			ADD FOREIGN KEY (tenant_id, user_id)
			REFERENCES public.um_user (um_tenant_id, um_id) MATCH SIMPLE
			ON UPDATE NO ACTION
			ON DELETE NO ACTION');
    }

    /**
     * Reverse the migrations
     *
     * @return void
     */
    public function down()
    {
		self::$connection->execute('DROP TABLE diskusi');
    }

}
