简体   繁体   中英

environment change in rake task

I am developing Rails v2.3 app with MySQL database and mysql2 gem . I faced a weird situation which is about changing the environment in rake task.

(all my setting and configurations for environment and database are correct, no problem for that.)

Here is my simple story :

I have a rake task like following:

namespace :db do
   task :do_something => :environment do
       #1. run under 'development' environment
       my_helper.run_under_development_env

       #2. change to 'custom' environment 
       RAILS_ENV='custom'

       Rake::Task['db:create']
       Rake::Task['db:migrate']

       #3. change back to 'development' environment
       RAILS_ENV='development'

       #4. But it still run in 'customer' environment, why?
       my_helper.run_under_development_env 
   end

end

The rake task is quite simple, what it does is:

1. Firstly, run a method from my_helper under " development " environment

2. Then, change to " custom " environment and run db:create and db:migrate

until now, everything is fine, the environment did change to " custom "

3. Then, change it back again to " development " environment

4. run helper method again under " development " environment

But, though I have changed the environment back to " development " in step 3, the last method still run in " custom " environment, why? and how to get rid of it?

--- PS ---

I have also checked a post related with environment change here , and tried to use the solution there (in step 2):

#2. change to 'custom' database
ActiveRecord::Base.establish_connection('custom')
Rake::Task['db:create']
Rake::Task['db:migrate']

to change the database connection instead of changing environment but, the db:create and db:migrate will still run under " development " database, though the linked post said it should run for " custom " database... weird

--------------- important update ---------------------

I just realize that the code in step 2:

#2. change to 'custom' environment 
RAILS_ENV='custom'

Rake::Task['db:create']
Rake::Task['db:migrate']

it changes environment to " custom " only if the Rake::Task['db:create'] get called, if I comment out Rake::Task['db:create'] line, code will still run under ' development ':

#2. change to 'custom' environment 
RAILS_ENV='custom'

#Rake::Task['db:create']
#CODE WILL RUN STILL UNDER 'development' environment.

Why Rake::Task['db:create'] affects environment change in my case...?

I realize this question is from over a month ago, but what they heck - it's Christmas

it seems like running each rake task in its own process will simplify things when switching environments?

namespace :db do
    task :do_something => :environment do
        unless Rails.env.development? then
           raise "Can only run under development environment, but specified env was #{Rails.env}"
        end 

        #1. run under 'development' environment
        my_helper.run_under_development_env

        #2. do the giggity with custom environment
        command = "bundle exec rake db:create RAILS_ENV=custom"    
        result = %x[#{command}]
        raise "rake task failed..........\n#{result}" if result.include?('rake aborted!')

        command = "bundle exec rake db:migrate RAILS_ENV=custom"    
        result = %x[#{command}]
        raise "rake task failed..........\n#{result}" if result.include?('rake aborted!')


        #3. back to development
        my_helper.run_under_development_env 
    end
end

just type after the rake task RAILS_ENV='production'

in your case

rake db:do_something RAILS_ENV='custom'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM