laravel 4 artisan -- how to rollback to a specific migration state?

29,814

Solution 1

Laravel 5.3

With Laravel 5.3 there is no need for heavy scripting. As it allows to rollback given number of migrations.

php artisan migrate:rollback --step=1

Here's the manual page for reference.

Solution 2

I am afraid, you cannot do this directly.

You can: 1, Rollback The Last Migration Operation (all migrations ran in the last batch)

php artisan migrate:rollback

2, Rollback all migrations

php artisan migrate:reset

3, Rollback all migrations and run them all again

php artisan migrate:refresh  
php artisan migrate:refresh --seed

In your situation, modify b.php and it's up() method, then execute artisan migrate:refresh command.

Solution 3

There's a way to hack it by manually editing the database. In the migrations table change the batch column by giving the last migration a different batch number. Be aware that they are in increasing order, so edit accordingly. This tracks which migrations were applied separately.

Then run artisan:rollback and it will undo the last "batch".

So if you want to separate them all, then start from the top and give each 1,2,3,4,5 and so on... You can see that it is easily scriptable, and you can make an artisan command if you wish to separate all your migrations.

Solution 4

In my experience. I never do migrate:rollback. I would usually create another migration that does all the changes i need to "undo/rollback" the previous migrations.

This way you can be flexible if you want to rollback 2-x steps back, you can just create a new migration to effect the changes you want and then run the new migration by php artisan migrate.

Solution 5

In fact, there is not this feature (yet). surprisingly

The best idea, is create a new file backtob.php and make its up call the down of your other migrate files. To avoid copy and paste, you can do something like this:

class BacktoB {
  public function up () {
     // the database is in the after D state //
     $migrateD = new D();
     $migrateD->down();
     // the database is in the after C state //
     $migrateC = new C();
     $migrateC->down();
     // the database is in the before C state //
     // before C = B //
  }
  public function down () {
     // the database is in the B state //
     $migrateC = new C();
     $migrateC->up();
     // the database is in the after C state //
     $migrateD = new D();
     $migrateD->up();
     // the database is in the after D state //
  }
}

As you can see, you can create the up and down calling the up and down of those migrations what you want to revert.

It is not the ideal, but it is what we can do.

Share:
29,814

Related videos on Youtube

do.
Author by

do.

Updated on November 17, 2020

Comments

  • do.
    do. over 3 years

    Say i got a.php, b.php, c.php and d.php migration classes files. How to rollback to a specific migration state, the state defined within b.php for example, with artisan command ?

    • Laurence
      Laurence almost 11 years
      Can you just run "php artisan migrate:rollback" twice - or do you want to specifically go to b.php?
    • do.
      do. almost 11 years
      I've thought about running "php artisan migrate:rollback" several times. But what if I have a.php, b.php and c.php at the time and run through them altogether and then create&migrate d.php. The b.php would never be rolled back for this situation so I want something to make it specifically rolling back to b.php.
  • Shay
    Shay about 9 years
    That's the better solution imo, nice hack!
  • code-8
    code-8 about 9 years
    How can I edit my batch column ? It seems lock and grey-out ? Can anyone please kindly advise ?
  • Caleb Jacobo
    Caleb Jacobo over 8 years
    This is what I do. Good answer.
  • Vigs
    Vigs over 8 years
    Caution: php artisan migrate:rollback apparently will now rollback all your migrations.
  • Vigs
    Vigs over 8 years
    To revises my comment above - if you have a large migration of many tables etc, and you run migrate:rollback it will rollback all of the actions from the last migration, not just the last action. I incorrectly assumed it would only rollback the last action, not the whole migration.
  • Marty
    Marty over 7 years
    As of 5.3 (released after this answer was posted), you can set the number of steps you would like to rollback, e.g. php artisan migrate:rollback --step=5 as you can see listed here: laravel.com/docs/5.3/migrations#rolling-back-migrations . @Yauheni Prakopchyk mentions it below.
  • GTS Joe
    GTS Joe over 3 years
    This is the best answer, as it's actually current. Most other answers are outdated.