2021_06_07_08638_create_keywords_table.php 2.56 KB
<?php

use App\Models\Keyword;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateKeywordsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (Schema::hasTable('goal_ad_groups') && Schema::hasColumn('goal_ad_groups', 'campaign_external_id')) {

            Schema::create('goal_ad_groups', function (Blueprint $table) {
                $table->renameColumn('campaign_external_id', 'dictionary_campaign_external_id');
            });

        }

        if (Schema::hasTable('ad_groups') && !Schema::hasColumn('ad_groups', 'keywords_loaded_at')) {

            Schema::create('ad_groups', function (Blueprint $table) {
                $table->timestamp('keywords_loaded_at')->nullable();
            });

        }

        Schema::create('keywords', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('external_id');
            $table->bigInteger('ad_group_external_id');
            $table->bigInteger('campaign_external_id');
            $table->bigInteger('ad_group_id')->unsigned();
            $table->bigInteger('campaign_id')->unsigned();
            $table->text('keyword')->nullable();
            $table->string('user_param_1', 255)->nullable();
            $table->string('user_param_2', 255)->nullable();
            $table->bigInteger('bid')->nullable();
            $table->bigInteger('context_bid')->nullable();
            $table->enum('state', [
                Keyword::STATE_OFF,
                Keyword::STATE_ON,
                Keyword::STATE_SUSPENDED,
            ])->nullable();
            $table->enum('status', [
                Keyword::STATUS_ACCEPTED,
                Keyword::STATUS_DRAFT,
                Keyword::STATUS_REJECTED,
                Keyword::STATUS_UNKNOWN,
            ])->nullable();
            $table->enum('serving_status', [
                Keyword::SERVING_STATUS_ELIGIBLE,
                Keyword::SERVING_STATUS_RARELY_SERVED,
            ])->nullable();
            $table->timestamp('updated_self')->nullable();
            $table->timestamps();

            $table->foreign('ad_group_id')->references('id')->on('ad_groups')
                ->cascadeOnDelete();
            $table->foreign('campaign_id')->references('id')->on('campaigns')
                ->cascadeOnDelete();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('keywords');
    }
}