<?php

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

/**
 * Class DiskusiCommentMigration_136
 */
class DiskusiCommentMigration_136 extends Migration
{
    /**
     * Define the table structure
     *
     * @return void
     */
    public function morph()
    {

    }

    /**
     * Run the migrations
     *
     * @return void
     */
    public function up()
    {
        self::$connection->execute('
            delete from jaga.masukan_komentar where not exists (
            select 1
            from jaga.masukan
            where jaga.masukan.id = jaga.masukan_komentar.masukan_id
            )
        ');

        self::$connection->execute(
            '
				insert into jaga.diskusi_comment(user_id, diskusi_id, level, comment, comment_id, created_at, updated_at)
				(
					select coalesce((select um_id from public.um_user where uuid_user=A.user_id), 1),
					(select id from jaga.diskusi where uuid=A.masukan_id), 0, isi, 0, created, last_update from jaga.masukan_komentar A
					where isi is not null
				);
			'
        );
    }

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

    }

}
