Why might “rake db:migrate” and “rake db:rollback” not roll back more than one migration at a time?

StackOverflow https://stackoverflow.com/questions/8635325

  •  30-03-2021
  •  | 
  •  

Question

I'm getting some unexpected behaviour with rake db:migrate and rake db:rollback. Am hoping someone might be able to shed some light for me.

I made a mess of my migrations by adding a conflicting one in, and I'm trying to migrate back to 0 so that I can migrate up again and continue knowing that everything is going to work OK.

I was running rake db:migrate version=0 and just getting dumped back at the command line, with no migrations reported and no effects on my database. I do have a Rakefile, and my database.yml file is set up with correct login credentials.

Playing around, I found I was able to go rake db:rollback step=1 with no problems: the appropriate actions would happen as per the migrations. However, rake db:rollback step=2 (or step=3 or step=5 -- anything other than 1) would only ever do one migration at a time.

Used mysql in the terminal to drop the database entirely. Recreated it, then went rake db:migrate. All the tables got built as they should have. I assumed whatever was broken was fixed.

So I went rake db:migrate version=0.... and again nothing happened. Tables still exist in the database where it should've gone back to being empty.

Does anyone have any ideas? Have pretty much scratched a hole in my scalp here at this point.

After manually dropping the database and running my migrations, rake db:migrate version=0 --dry-run --trace gives me the response:

** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute (dry run) environment
** Execute (dry run) db:migrate

My migrations at this point are all just replications of the RailsTutorial.org ones, which is what I've been working off until now as a learning exercise; they do all have self.down methods and they all work when I step it back one at a time.

The only other thing that might be relevant is that I completed that tutorial on a different computer (my PC); I pushed it up to github and cloned it onto my Mac in order to use the tutorial as a starting point for my own work. The app does seem to run okay in my browser on the Mac (same as on the PC), although I've been following a myriad of tutorials to get the Rails environment (RVM) set up on the Mac for the first time and I've installed different gems to do so. Could my problem possibly stem from here?

Thanks for any advice!

Was it helpful?

Solution

This article might help you.

Basically, you can tweak db:rollback task.

namespace :db do
  desc 'Rolls the schema back to the previous version. Specify the number of steps with STEP=n'
  task :rollback => :environment do
    step = ENV['STEP'] ? ENV['STEP'].to_i : 1
    version = ActiveRecord::Migrator.current_version - step
    ActiveRecord::Migrator.migrate('db/migrate/', version)
  end
end

So now you can do this:

rake db:rollback STEP=3
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top