<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

class AlterTransactionBookingId extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (!Schema::hasColumn('transactions', 'owner_id')) {
            Schema::table('transactions', function (Blueprint $table) {
                $table->morphs('owner');
            });

            //In-case the model name lengthy
            Schema::table('transactions', function (Blueprint $table) {
                $table->string('owner_type', 250)->change();
            });

            DB::statement("UPDATE transactions SET owner_type='App\\\\Models\\\\Booking', owner_id = booking_id");

            Schema::table('transactions', function (Blueprint $table) {
                $table->dropForeign('transactions_booking_id_foreign');
                $table->dropColumn('booking_id');
            });
        }
    }

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