Rollback one specific migration in Laravel

2019-01-10 05:59发布

问题:

I want

to rollback only :

Rolled back: 2015_05_15_195423_alter_table_web_directories


I run

php artisan migrate:rollback, 3 of my migration are rolling back.

Rolled back: 2015_05_15_195423_alter_table_web_directories
Rolled back: 2015_05_13_135240_create_web_directories_table
Rolled back: 2015_05_13_134411_create_contacts_table

I delete

both of my web_directories and my contacts table unintentionally. Sadly, I never want that to happen, and if I can rollback only that specific one, this disaster will never happen.


I hope

someone can teach me how to prevent this from happening again.

Any recommendation will be much appreciated.

回答1:

If you look in your migrations table, then you’ll see each migration has a batch number. So when you roll back, it rolls back each migration that was part of the last batch.

If you only want to roll back the very last migration, then just increment the batch number by one. Then next time you run the rollback command, it’ll only roll back that one migration as it’s in a “batch” of its own.



回答2:

Laravel 5.3+

Rollback one step. Natively.

php artisan migrate:rollback --step=1

And here's the manual page: docs.


Laravel 5.2 and before

No way to do without some hassle. For details, check Martin Bean's answer.



回答3:

If you can't do what is told by @Martin Bean, then you can try another trick.

Create a new migration and on that file in up() method insert what's in down() method of the migration you want to rollback and in down() method insert what's in up() method.

e.g if your original migration is like this

public function up()
{
    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id')->unsigned();
        $table->string('name');
    });
}
public function down()
{
    Schema::drop('users');
}

then in new migration file do this

public function up()
{
    Schema::drop('users');
}
public function down()
{
    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id')->unsigned();
        $table->string('name');
    });
}

and then run the migrate, it will delete the table. and if you again want that back just rollback it.



回答4:

Every time you rollback you get the last batch of migration. use the command

php artisan migrate:rollback --step=1


回答5:

It might be a little late to answer this question but here's a very good, clean and efficient way to do it I feel. I'll try to be as thorough as possible.

Before creating your migrations create different directories like so:

    database
       | 
       migrations
            |
            batch_1
            batch_2
            batch_3

Then, when creating your migrations run the following command (using your tables as an example):

     php artisan make:migration alter_table_web_directories --path=database/migrations/batch_1

or

     php artisan make:migration alter_table_web_directories --path=database/migrations/batch_2

or

     php artisan make:migration alter_table_web_directories --path=database/migrations/batch_3

The commands above will make the migration file within the given directory path. Then you can simply run the following command to migrate your files via their assigned directories.

    php artisan migrate alter_table_web_directories --path=database/migrations/batch_1

*Note: You can change batch_1 to batch_2 or batch_3 or whatever folder name you're storing the migration files in. As long as it remains within the database/migrations directory or some specified directory.

Next if you need to rollback your specific migrations you can rollback by batch as shown below:

    php artisan migrate:rollback --step=1
                    or try
php artisan migrate:rollback alter_table_web_directories --path=database/migrations/batch_1

or

    php artisan migrate:rollback --step=2
                    or try
php artisan migrate:rollback alter_table_web_directories --path=database/migrations/batch_2

or

    php artisan migrate:rollback --step=3
                    or try
php artisan migrate:rollback alter_table_web_directories --path=database/migrations/batch_3

Using these techniques will allow you more flexibility and control over your database(s) and any modifications made to your schema.



回答6:

Migrate tables one by one.

Change the batch number of the migration you want to rollback to the highest.

Run migrate:rollback.

May not be the most comfortable way to deal with larger projects.



回答7:

Rollback one step. Natively.

php artisan migrate:rollback --step=1

Rollback two step. Natively.

php artisan migrate:rollback --step=2


回答8:

Use command "php artisan migrate:rollback --step=1" to rollback migration to 1 step back.

For more info check the link :- https://laravel.com/docs/master/migrations#running-migrations



回答9:

INSERT INTO homestead.bb_migrations (`migration`, `batch`)  VALUES ('2016_01_21_064436_create_victory_point_balance_table', '2')

something like this



回答10:

If you want modify original migration file and migrate it again, you can use this package to migrate. (Applicable to Laravel 5.4 or later)

First, install package in your Laravel project:

composer require caloskao/migrate-specific

Register command at app/Console/Kernel.php :

protected $commands = [
    \CalosKao\MigrateSpecific::class
];

Now, run this command to migrate your file

php artisan migrate:specific database/migrations/table.php


回答11:

As stated in the Laravel manual, you may roll back specific number of migrations using the --step option

php artisan migrate:rollback --step=5