How do I force RAILS_ENV in a rake task?
For this particular task, you only need to change the DB connection, so as Adam pointed out, you can do this:
namespace :db do
namespace :test do
task :reset do
ActiveRecord::Base.establish_connection('test')
Rake::Task['db:drop'].invoke
Rake::Task['db:create'].invoke
Rake::Task['db:migrate'].invoke
ActiveRecord::Base.establish_connection(ENV['RAILS_ENV']) #Make sure you don't have side-effects!
end
end
end
If your task is more complicated, and you need other aspects of ENV, you are safest spawning a new rake process:
namespace :db do
namespace :test do
task :reset do
system("rake db:drop RAILS_ENV=test")
system("rake db:create RAILS_ENV=test")
system("rake db:migrate RAILS_ENV=test")
end
end
end
or
namespace :db do
namespace :test do
task :reset do
if (ENV['RAILS_ENV'] == "test")
Rake::Task['db:drop'].invoke
Rake::Task['db:create'].invoke
Rake::Task['db:migrate'].invoke
else
system("rake db:test:reset RAILS_ENV=test")
end
end
end
end
How do I force RAILS_ENV in a rake task?
For this particular task, you only need to change the DB connection, so as Adam pointed out, you can do this:
namespace :db do
namespace :test do
task :reset do
ActiveRecord::Base.establish_connection('test')
Rake::Task['db:drop'].invoke
Rake::Task['db:create'].invoke
Rake::Task['db:migrate'].invoke
ActiveRecord::Base.establish_connection(ENV['RAILS_ENV']) #Make sure you don't have side-effects!
end
end
end
If your task is more complicated, and you need other aspects of ENV, you are safest spawning a new rake process:
namespace :db do
namespace :test do
task :reset do
system("rake db:drop RAILS_ENV=test")
system("rake db:create RAILS_ENV=test")
system("rake db:migrate RAILS_ENV=test")
end
end
end
or
namespace :db do
namespace :test do
task :reset do
if (ENV['RAILS_ENV'] == "test")
Rake::Task['db:drop'].invoke
Rake::Task['db:create'].invoke
Rake::Task['db:migrate'].invoke
else
system("rake db:test:reset RAILS_ENV=test")
end
end
end
end
Changing a rake task to work on test environment
You need to reconnect to the database. Something like
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations["test"])
Changing ENV['RAILS_ENV'] after environment is already loaded doesn't do anything.
Perhaps it would also work if you load the environment task after you change ENV, but not sure about that:
task :prepare do
ENV['RAILS_ENV'] ||= "test"
Rake::Task["environment"].invoke
Rake::Task["db:fixtures:load"].invoke
end
How to run a rake task on jenkins with string parameters?
Hi everybody I got the answer:
$RAKE_TASK may have spaces in it depending on the parameters given to the rake task, which bash/jenkins don't deal with cleanly
So, we interpolate the variables into a double-quoted string, then eval the result.
This ensures that the spaces don't get messed up.
bundle install
echo $ENVIRONMENT
eval "bundle exec rake $RAKE_TASK RAILS_ENV=$ENVIRONMENT"
It should be in the jenkins/job/configure page inside of Execute shell command
how to force rake task to run under development environment using Whenever gem
In schedule.rb
, you can specify the environment you'd like scheduled tasks to be run in:
# config/schedule.rb
set :environment, 'development'
Alternatively, you can set the environment on a per-job basis:
# config/schedule.rb
every 1.day do
runner 'Model.task', :environment => 'development'
runner 'Model.task', :environment => 'production'
end
Related Topics
How to Pick Randomly from an Array
How to Sum Array of Numbers in Ruby
How to Convert a String Object into a Hash Object
Difference Between Include and Extend in Ruby
Is There a "Do ... While" Loop in Ruby
How to Install Postgresql'S Pg Gem on Ubuntu
How to Find Which Operating System My Ruby Program Is Running On
Do..End VS Curly Braces For Blocks in Ruby
How to Get a Random Number in Ruby
Why Doesn't Ruby Support Method Overloading
How to Set Tls Context Options in Ruby (Like Openssl::Ssl::Ssl_Op_No_Sslv2)
Ruby 2.0.0P0 Irb Warning: "Dl Is Deprecated, Please Use Fiddle"
When to Use Lambda, When to Use Proc.New
Case Statement With Multiple Values in Each 'When' Block