Undistraction
Undistraction

Reputation: 43579

Force Rake Task To Run in Specific Rails Environment

I need to run a series of Rake tasks from another Rake task. The first three tasks need to be run in the development environment, but the final task needs to be run in the staging environment. The task has a dependency on :environment which causes the Rails development environment to be loaded before the tasks run.

However, I need the final task to be executed in the staging environment.

Passing a RAILS_ENV=staging flag before invoking the rake task is no good as the environment has already loaded at this point and all this will do is set the flag, not load the staging environment.

Is there a way I can force a rake task in a specific environment?

Upvotes: 33

Views: 27173

Answers (2)

Matt Schwartz
Matt Schwartz

Reputation: 3394

The way I solved it was to add a dependency to set the rails env before the task is invoked:

namespace :foo do
  desc "Our custom rake task"
  task :bar => ["db:test:set_test_env", :environment] do
      puts "Custom rake task"
      # Do whatever here...
      puts Rails.env
  end
end


namespace :db do
  namespace :test do
    desc "Custom dependency to set test environment"
    task :set_test_env do # Note that we don't load the :environment task dependency
      Rails.env = "test"
    end
  end
end 

Upvotes: 16

kddeisz
kddeisz

Reputation: 5182

I've accomplished this kind of this before, albeit not in the most elegant of ways:

task :prepare do
  system("bundle exec rake ... RAILS_ENV=development")      
  system("bundle exec rake ... RAILS_ENV=development")
  system("bundle exec rake ... RAILS_ENV=test")
  system("bundle exec rake ... RAILS_ENV=test")
  system("bundle exec rake ... RAILS_ENV=test")
  system("bundle exec rake ... RAILS_ENV=test")
end

It's always worked for me. I'd be curious to know if there was a better way.

Upvotes: 21

Related Questions