<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class MoveKeychainTaskRelationFromTasksTableToTaskKeychainsTable extends Migration {

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		$columns = collect(DB::select('SHOW COLUMNS FROM tasks'))->pluck('Field')->toArray();
		if (in_array('keychain_id', $columns)) {
			DB::statement("INSERT INTO `task_keychains` (`task_id`, `keychain_id`, `created_at`, `updated_at`) SELECT `id`, `keychain_id`, `created_at`, `updated_at` FROM `tasks` WHERE `keychain_id` != 0  ON DUPLICATE KEY UPDATE `task_id` = VALUES(`task_id`), `keychain_id` = VALUES(`keychain_id`), `updated_at` = VALUES(`updated_at`), `created_at` = VALUES(`created_at`)");
		}
	}

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

}
