Migrating up and down quickly
Thursday, August 27th, 2009I frequently make a migration, migrate, run my tests and realize there’s a bug with my migration, or I forgot something, or whatever. It’s kind of annoying to have to type:
rake db:migrate rake db:migrate RAILS_ENV=test # run tests, watch tests fail rake db:rollback rake db:rollback RAILS_ENV=test # fix code and repeat
… over and over and over. So I wrote a couple little rake tasks:
task :mig do dev_and_test("migrate") end task :roll do dev_and_test("rollback") end def dev_and_test(what) ["development", "test"].each do |env| system "rake db:#{what} RAILS_ENV=#{env}" end end
Now I can just do:
rake mig # run tests, fail rake roll #fix code, rinse, repeat
Much nicer.