<?php 

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

/**
 * Class DiskusiAttachmentMigration_118
 */
class DiskusiAttachmentMigration_118 extends Migration
{
    /**
     * Define the table structure
     *
     * @return void
     */
    public function morph()
    {
        $this->morphTable('diskusi_attachment', [
                'columns' => [
                    new Column(
                        'id',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'notNull' => true,
                            'autoIncrement' => true,
                            'first' => true
                        ]
                    ),
                    new Column(
                        'diskusi_id',
                        [
                            'type' => Column::TYPE_INTEGER,
                            'notNull' => true,
                            'after' => 'uuid'
                        ]
                    ),
                    new Column(
                        'filename',
                        [
                            'type' => Column::TYPE_VARCHAR,
                            'notNull' => true,
                            'size' => 255,
                            'after' => 'diskusi_id'
                        ]
                    ),
                    new Column(
                        'filetype',
                        [
                            'type' => Column::TYPE_VARCHAR,
                            'notNull' => true,
                            'size' => 255,
                            'after' => 'filename'
                        ]
                    )
                ],
                'indexes' => [
                    new Index('diskusi_attachment_pkey', ['id'], 'PRIMARY KEY')
                ],
                'references' => [
                    new Reference(
                        'diskusi_attachment_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_attachment
			ADD COLUMN created_at timestamp without time zone NOT NULL DEFAULT now();
			ALTER TABLE jaga.diskusi_attachment
			ADD COLUMN updated_at timestamp without time zone NOT NULL DEFAULT now();
			ALTER TABLE jaga.diskusi_attachment ADD COLUMN uuid uuid NOT NULL DEFAULT jaga.uuid_generate_v4();');
			
    }

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

}
