Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Mar 16, 2022
Merged

Conversation

eusonlito
Copy link
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

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