<?php

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

class AddOnepercentToKadarupahTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::connection('mysql7')->table('kadarupah', function (Blueprint $table) {

            $isKadarUjrahExist = Schema::connection('mysql7')->hasColumn('kadarupah', 'kadarujrah');
            $isKadarOnePercentExist = Schema::connection('mysql7')->hasColumn('kadarupah', 'kadaronepercent');

            if (!$isKadarUjrahExist) {
                $table->decimal('kadarujrah', 5, 2);
            }
            if (!$isKadarOnePercentExist) {
                $table->decimal('kadaronepercent', 5, 2);
            }

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        $columns = ['kadarujrah', 'kadaronepercent'];

        $columns_exist = array_filter($columns, function ($column) {
            return Schema::connection('mysql7')->hasColumn('kadarupah', $column);
        });

        Schema::connection('mysql7')->table('kadarupah', function (Blueprint $table) use($columns_exist) {
            $table->dropColumn($columns_exist);

        });
    }
}
