<?php 

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

/**
 * Class DiskusiLikeMigration_123
 */
class DiskusiLikeMigration_123 extends Migration
{
    /**
     * Define the table structure
     *
     * @return void
     */
    public function morph()
    {
        $this->morphTable('diskusi_like', [
                'columns' => [
                    new Column(
                        'id',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'notNull' => true,
                            'autoIncrement' => true,
                            'first' => true
                        ]
                    ),
                    new Column(
                        'user_id',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'notNull' => true,
                            'after' => 'id'
                        ]
                    ),
                    new Column(
                        'tenant_id',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'default' => "-1234",
                            'notNull' => true,
                            'after' => 'user_id'
                        ]
                    ),
                    new Column(
                        'diskusi_id',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'notNull' => true,
                            'after' => 'tenant_id'
                        ]
                    )
                ],
                'indexes' => [
                    new Index('diskusi_like_pkey', ['id'], 'PRIMARY KEY')
                ],
                'references' => [
                    new Reference(
                        'diskusi_like_diskusi_id_fkey',
                        [
                            'referencedTable' => 'diskusi',
                            'referencedSchema' => 'jaga',
                            'columns' => ['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_like 
			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;
			ALTER TABLE jaga.diskusi_like
			ADD COLUMN created_at timestamp without time zone NOT NULL DEFAULT now();
		');
    }

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

    }

}
