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

Tags: rails

 

Comments

Nate Klaiber

November 27th, 2007

Very nice. I could have used this in a previous project. We were dealing with legacy data and I always had to drop/create the table before running migrations. This would have made that a much easier process.

I must say, working from a legacy system, in a team based environment, was very hard on migrations. We had to keep communication to prevent running into number conflicts or the like. Keeping in sync was a task.

 

Commenting has been turned off.