<?php

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

/**
 * Class DiskusiCommentMigration_129
 */
class DiskusiCommentMigration_129 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.cerita_komentar where not exists (
          select 1
          from jaga.cerita
          where jaga.cerita.id = jaga.cerita_komentar.cerita_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.cerita_id), 0, komentar, 0, created, last_update from jaga.cerita_komentar A
					where komentar is not null
				);
			'
    );
  }

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

  }

}
