Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drop test and development database in one rake task?

I tried to drop the test and development databases from one rake task like this:

task :regenerate do
  Rails.env = "test"
  Rake::Task["db:drop"].invoke
  Rails.env = "development"
  Rake::Task["db:drop"].invoke
end

The test database was dropped successfully. But the development database was not dropped.

Any ideas on how to make this work?

NB: This is on Rails 3.2.3

UPDATE:

Very odd, but reversing the order works:

task :regenerate do
  Rails.env = "development"
  Rake::Task["db:drop"].invoke
  Rails.env = "test"
  Rake::Task["db:drop"].invoke
end

What is going on?!

like image 295
Zabba Avatar asked Jan 16 '23 22:01

Zabba


1 Answers

You can write it like this:

namespace :db do
  desc "Database custom drop"
  task :mydrop do
    system("rake db:drop RAILS_ENV=test")
    system("rake db:drop RAILS_ENV=development")
  end
end
like image 200
zolter Avatar answered Jan 30 '23 18:01

zolter