231

After adding migration files in the db/migrate folder and running rake db:migrate, I want get back to the previous step, I think using VERSION=n is the right way to do that, but I don't know the correct value of n to use. Is there any command to check the current n value?

It would be great if anyone could provide full instructions on how to use rake db:migrate.

6 Answers 6

421

For starters

rake db:rollback will get you back one step

then

rake db:rollback STEP=n

Will roll you back n migrations where n is the number of recent migrations you want to rollback.

More references here.

0
117

Roll back the most recent migration:

rake db:rollback

Roll back the n most recent migrations:

rake db:rollback STEP=n

You can find full instructions on the use of Rails migration tasks for rake on the Rails Guide for running migrations.


Here's some more:

  • rake db:migrate - Run all migrations that haven't been run already
  • rake db:migrate VERSION=20080906120000 - Run all necessary migrations (up or down) to get to the given version
  • rake db:migrate RAILS_ENV=test - Run migrations in the given environment
  • rake db:migrate:redo - Roll back one migration and run it again
  • rake db:migrate:redo STEP=n - Roll back the last n migrations and run them again
  • rake db:migrate:up VERSION=20080906120000 - Run the up method for the given migration
  • rake db:migrate:down VERSION=20080906120000 - Run the down method for the given migration

And to answer your question about where you get a migration's version number from:

The version is the numerical prefix on the migration's filename. For example, to migrate to version 20080906120000 run

$ rake db:migrate VERSION=20080906120000

(From Running Migrations in the Rails Guides)

1
  • 2
    When rake db:rollback wasn't working to rollback the most recent migration, I had to use rake db:migrate VERSION=<second last version date> and it worked fine
    – Omni
    Commented Mar 26, 2015 at 3:57
8

Best way is running Particular migration again by using down or up(in rails 4. It's change)

rails db:migrate:up VERSION=timestamp

Now how you get the timestamp. Go to this path

/db/migrate

Identify migration file you want to revert.pick the timestamp from that file name.

1
  • While this doesn't answer the question, it is indeed the best way. Rolling back by steps can cause mistakes, especially when working on software in a group.
    – steel
    Commented Aug 24, 2016 at 21:26
6

If the version is 20150616132425, then use:

rails db:migrate:down VERSION=20150616132425
3

Other people have already answered you how to rollback, but you also asked how you could identify the version number of a migration.

  • rake db:migrate:status gives a list of your migrations version, name and status (up or down)
  • Your can also find the migration file, which contain a timestamp in the filename, that is the version number. Migrations are located in folder: /db/migrate
1
  • By the way if you run the status command, you might see lines like: ********** NO FILE ********** Those migrations is not existing on your current branch if you use git. Commented Oct 20, 2017 at 8:32
-3
  try {
        $result=DB::table('users')->whereExists(function ($Query){
            $Query->where('id','<','14162756');
            $Query->whereBetween('password',[14162756,48384486]);
            $Query->whereIn('id',[3,8,12]);
        });
    }catch (\Exception $error){
        Log::error($error);
        DB::rollBack(1);
        return redirect()->route('bye');
    }
1
  • This question already contains multiple answers and an accepted answer. Can you explain (by editing your answer) where your answer differs from the other answers? Also know that Code-only answers are not useful in the long run.
    – 7uc1f3r
    Commented Oct 3, 2020 at 8:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.