Rake Task: DB Recreate

I have this little Rake task which I seem to end up putting into all
of my projects and I’m wondering if this might be of use to other
people as well or if there is a better way of accomplishing this.

It is especially useful during initial development when you may hose
your migrations and need to go back to fresh db where rake db:migrate
VERSION=0 will not work (because of a failure during an up migration
leaving your database in an inconsistent state).

Anyhow, here’s the task. I put it in lib/tasks/database.rake and then
type rake db:recreate

namespace :db do
desc “Drop and create the current database”
task :recreate => :environment do
abcs = ActiveRecord::Base.configurations
ActiveRecord::Base.establish_connection(abcs[RAILS_ENV])
ActiveRecord::Base.connection.recreate_database(ActiveRecord::Base.connection.current_database)
end
end

Sincerely,
Anthony E.


Email: [email protected]
Cell: 808 782-5046
Current Location: Melbourne, FL

Anthony E. wrote:

Anyhow, here’s the task. I put it in lib/tasks/database.rake and then
type rake db:recreate

This is similar to Organize Your Models — err.the_blog I saw a while back but
they were able to work out the database-specific aspects and had a check
to ensure that you were not hosing your production database.

Eric