<?php 

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

/**
 * Class DiskusiHashtagMigration_118
 */
class DiskusiHashtagMigration_118 extends Migration
{
    /**
     * Define the table structure
     *
     * @return void
     */
    public function morph()
    {
        $this->morphTable('diskusi_hashtag', [
                '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(
                        'hashtag',
                        [
                            'type' => Column::TYPE_VARCHAR,
                            'notNull' => true,
                            'size' => 255,
                            'after' => 'tenant_id'
                        ]
                    )
                ],
                'indexes' => [
                    new Index('diskusi_hashtag_pkey', ['id'], 'PRIMARY KEY')
                ],
            ]
        );
    }

    /**
     * Run the migrations
     *
     * @return void
     */
    public function up()
    {
		self::$connection->execute('ALTER TABLE jaga.diskusi_hashtag
			ADD COLUMN created_at timestamp without time zone NOT NULL DEFAULT now();
			ALTER TABLE jaga.diskusi_hashtag
			ADD COLUMN updated_at timestamp without time zone NOT NULL DEFAULT now();
			ALTER TABLE jaga.diskusi_hashtag 
			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_hashtag');
    }

}
