<?php

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

class UpdateMediaTable extends Migration
{
    /**
     * The name of the table.
     * 
     * @var string
     */
    protected $table_name = 'media';

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table($this->table_name, function (Blueprint $table) {

            if($this->hasColumn('media_target')) {
                $table->string('media_target')->nullable()->change();
            } else {
                $table->string('media_target')->nullable();
            }
            
            if($this->hasColumn('media_type')) {
                $table->string('media_type')->default('image')->change();
            } else {
                $table->string('media_type')->default('image');
            }
            
            if($this->hasColumn('media_title')) {
                $table->string('media_title')->nullable()->change();
            } else {
                $table->string('media_title')->nullable();
            }
            
            if($this->hasColumn('media_url')) {
                $table->string('media_url')->nullable()->change();
            } else {
                $table->string('media_url')->nullable();
            }

            if($this->hasColumn('media_content')) {
                $table->text('media_content')->nullable()->change();
            } else {
                $table->text('media_content')->nullable();
            }

            if($this->hasColumn('media_desc')) {
                $table->text('media_desc')->nullable()->change();
            } else {
                $table->text('media_desc')->nullable();
            }

        });
    }

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

     /**
     * Check if table has column.
     * 
     * @param string $column_name
     */
    public function hasColumn($column_name) {
        return Schema::hasColumn($this->table_name, $column_name);
    }
}
