Skip to content

[9.x] Added whenTableHasColumn and whenTableDoesntHaveColumn on Schema Builder#41517

Merged
taylorotwell merged 2 commits into
laravel:9.xfrom
eusonlito:patch-1
Mar 16, 2022
Merged

[9.x] Added whenTableHasColumn and whenTableDoesntHaveColumn on Schema Builder#41517
taylorotwell merged 2 commits into
laravel:9.xfrom
eusonlito:patch-1

Conversation

@eusonlito
Copy link
Copy Markdown
Contributor

@eusonlito eusonlito commented Mar 16, 2022

On some systems, database schema is updated from multiple sources. This functions simplify the table columns update on migrations.

  • Before:
<?php declare(strict_types=1);

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

return new class extends Migration
{
    /**
     * @return void
     */
    public function up(): void
    {
        if (!Schema::hasColumn('product', 'order')) {
            Schema::table('product', function (Blueprint $table) {
                $table->unsignedInteger('order')->default(0);
            });
        }

        if (!Schema::hasColumn('product_attribute', 'order')) {
            Schema::table('product_attribute', function (Blueprint $table) {
                $table->unsignedInteger('order')->default(0);
            });
        }
    }

    /**
     * @return void
     */
    public function down(): void
    {
        if (Schema::hasColumn('product', 'order')) {
            Schema::table('product', function (Blueprint $table) {
                $table->dropColumn('order');
            });
        }

        if (Schema::hasColumn('product_attribute', 'order')) {
            Schema::table('product_attribute', function (Blueprint $table) {
                $table->dropColumn('order');
            });
        }
    }
};
  • After
<?php declare(strict_types=1);

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

return new class extends Migration
{
    /**
     * @return void
     */
    public function up(): void
    {
        Schema::whenTableDoesntHaveColumn('product', 'order', function (Blueprint $table) {
            $table->unsignedInteger('order')->default(0);
        });

        Schema::whenTableDoesntHaveColumn('product_attribute', 'order', function (Blueprint $table) {
            $table->unsignedInteger('order')->default(0);
        });
    }

    /**
     * @return void
     */
    public function down(): void
    {
        Schema::whenTableHasColumn('product', 'order', function (Blueprint $table) {
            $table->dropColumn('order');
        });

        Schema::whenTableHasColumn('product_attribute', 'order', function (Blueprint $table) {
            $table->dropColumn('order');
        });
    }
};

@taylorotwell taylorotwell merged commit 2cc6d1b into laravel:9.x Mar 16, 2022
@github-actions
Copy link
Copy Markdown

Thanks for submitting a PR!

In order to review and merge PRs most efficiently, we require that all PRs grant maintainer edit access before we review them. For information on how to do this, see the relevant GitHub documentation.

@eusonlito eusonlito changed the title [9.x] Added whenTableHasColumn and whenTableHasNotColumn on Schema Builder [9.x] Added whenTableHasColumn and whenTableDoesntHaveColumn on Schema Builder Mar 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants