ziming/laravel-specific-migrate-fresh

Migrate Fresh for Specific Tables Only

0.2 2024-04-02 04:15 UTC

This package is auto-updated.

Last update: 2024-04-29 17:47:31 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Want php artisan migrate:fresh but not drop all the tables? This is for you.

Support me

Donations are welcomed.

Installation

You can install the package via composer:

composer require ziming/laravel-specific-migrate-fresh

You can publish the config file with:

php artisan vendor:publish --tag="specific-migrate-fresh-config"

This is the contents of the published config file:

return [

    /*
     * There are 2 modes 'exclude' and 'include'.
     *
     * 'exclude' mode will drop all tables except the tables you specified in the 'excluded_tables' array.
     * 'include' mode will drop only the tables you specified in the 'included_tables' array.
     */
    'mode' => env('SPECIFIC_MIGRATE_FRESH_MODE', 'exclude'),

    /*
     * This will be used if mode is 'exclude'.
     *
     * If mode is 'exclude', the database tables in this array will be excluded
     * from getting dropped.
     */
    'excluded_tables' => [
        //
    ],

    /*
     * This will be used if mode is 'include'.
     *
     * If mode is 'include', only the database tables in this array will be dropped
     */
    'included_tables' => [
        //
    ],
];

Usage

Go to the config file, choose your preferred mode ('exclude' or 'include').

Fill in the relevant array ('excluded_tables' or 'included_tables').

Go to your migrations and for the tables you do not want to drop, in the up() method, add in a conditional check to skip the migration if the table still exists. You need to do this so that php artisan migrate will not throw an error about the table or column already exist later.

<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (Schema::hasTable('some_giant_table_i_do_not_want_to_drop')) {
            return;
        }

        Schema::create('some_giant_table_i_do_not_want_to_drop', function (Blueprint $table) {
            $table->id();
            // Other columns
        }
}

And call the command below. The --seed option is optional.

php artisan migrate:specific-fresh --seed

The end of this command simply just call the php artisan migrate --seed command at the end.

If you do not want to call your DatabaseSeeder. Leave out --seed from your command.

php artisan migrate:specific-fresh

Testing

PRs are welcomed on this

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.