Rebuild from migrations
November 27th, 2007
rake db:reset rebuilds from your schema file, which is great, unless you happen to store some data in your migrations, such as creating an admin account for the system. Then you’d like to rebuild from your migrations. To do so, you’d have to rake db:drop, rake db:create and rake db:migrate. Not a big deal, but in early development, sometimes you find yourself changing previous migration files, etc, so you might be doing this more than once – it can get old.
I also like to make sure that my test db is up to date when I recreate the db from migrations and then I like to annotate my models and fixtures (both in rails default test and rspec). Below is a small and simple snip so that you can do just that by typing rake db:rebuild.
I’m personally using an edited version of annotate_models that will also annotate my rspec models and fixtures. I sent this over to Dave Thomas, but I don’t think he has had the time to add it into his plugin. For my use, I just have it sitting on one of my svn servers.
1 2 3 4 |
namespace :db do desc "Drops and recreates the database from your migrations for the current environment and then annotates your models." task :rebuild => ["db:drop", "db:create", "db:migrate", "db:test:clone", "annotate_models"] end |


November 28th, 2007 at 08:17 PM
I have a little safety built into mine:
return unless %w[development test staging].include? RAILS_ENV
That way it can’t run the task in production mode. Any idea how you would do this with your method?
December 1st, 2007 at 09:40 PM
Ryan: this task doesn’t include the safety net but rather assumes, perhaps not the best thing to do, that the developer knows what they are doing. It follows the same principle as rake db:reset.
December 28th, 2007 at 08:41 PM
hi this is bhanu ruby on rails developer and looking forward to work with you if you do require any help i can work with you and i did develop manyw eb applications