Monday, December 19, 2011

Rake task to drop, create, migrate then seed your development database

When developing a Ruby on Rails application you will often want to drop, create, migrate then seed your development database. Justin French wrote a blog post describing how to write a custom rake task to do just that. I had to modify the code to work properly in my Rails 3 (3.1.x) development environment, so I thought I'd share.

# Rake task to drop, create, migrate then seed your development database
# with fixture data
# Based on http://justinfrench.com/index.php?id=232

namespace :db do
  desc "Raise an error unless the current Rails environment is development"
  task :development_environment_only do
    raise "Development only!" unless Rails.env.development?
  end

  desc "Drop, create, migrate then seed development database"
  task :devreset => [
    'environment', 
    'db:development_environment_only', 
    'db:drop', 
    'db:create', 
    'db:migrate', 
    'db:seed'
    ]

end

No comments:

Post a Comment

ShareThis