<?php 

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

/**
 * Class DiskusiMentionUserMigration_119
 */
class DiskusiMentionUserMigration_119 extends Migration
{
    /**
     * Define the table structure
     *
     * @return void
     */
    public function morph()
    {
        $this->morphTable('diskusi_mention_user', [
                '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,
                            'notNull' => true,
                            'after' => 'user_id'
                        ]
                    ),
                    new Column(
                        'user_mentioned_id',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'notNull' => true,
                            'after' => 'tenant_id'
                        ]
                    ),
                    new Column(
                        'tenant_mentioned_id',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'notNull' => true,
                            'after' => 'user_mentioned_id'
                        ]
                    ),
                    new Column(
                        'diskusi_id',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'notNull' => true,
                            'after' => 'tenant_mentioned_id'
                        ]
                    )
                ],
                'indexes' => [
                    new Index('diskusi_mention_user_pkey', ['id'], 'PRIMARY KEY')
                ],
                'references' => [
                    new Reference(
                        'diskusi_mention_user_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_mention_user
			ADD COLUMN created_at timestamp without time zone NOT NULL DEFAULT now();
			ALTER TABLE jaga.diskusi_mention_user
			ADD COLUMN updated_at timestamp without time zone NOT NULL DEFAULT now();
			ALTER TABLE jaga.diskusi_mention_user 
			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_mention_user 
			ADD FOREIGN KEY (tenant_mentioned_id, user_mentioned_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_mention_user');
    }

}
