简体   繁体   English

rake任务中的环境变化

[英]environment change in rake task

I am developing Rails v2.3 app with MySQL database and mysql2 gem . 我正在使用MySQL数据库和mysql2 gem开发Rails v2.3应用程序。 I faced a weird situation which is about changing the environment in rake task. 我遇到了一个奇怪的情况,那就是在rake任务中改变环境

(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: 我有一个像下面这样的rake任务:

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: rake任务非常简单,它的作用是:

1. Firstly, run a method from my_helper under " development " environment 1.首先,在“ 开发 ”环境下运行my_helper的方法

2. Then, change to " custom " environment and run db:create and db:migrate 2.然后,切换到“ 自定义 ”环境并运行db:createdb:migrate

until now, everything is fine, the environment did change to " custom " 到现在为止,一切都很好,环境确实变成了“ 习俗

3. Then, change it back again to " development " environment 3.然后,再次变回“ 开发 ”环境

4. run helper method again under " development " environment 4.在“ 开发 ”环境下再次运行辅助方法

But, though I have changed the environment back to " development " in step 3, the last method still run in " custom " environment, why? 但是,虽然我已经在第3步中将环境改回“ 开发 ”,但最后一种方法仍然在“ 自定义 ”环境中运行,为什么? and how to get rid of it? 以及如何摆脱它?

--- PS --- --- PS ---

I have also checked a post related with environment change here , and tried to use the solution there (in step 2): 我还在这里检查了与环境变化相关的帖子,并尝试在那里使用解决方案(在步骤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 更改数据库连接而不是更改环境,但db:createdb:migrate仍将在“ 开发 ”数据库下运行,尽管链接的帖子说它应该运行“ 自定义 ”数据库...怪异

--------------- important update --------------------- ---------------重要更新---------------------

I just realize that the code in step 2: 我只是意识到第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 ': 只有当 Rake::Task['db:create']被调用时, 才会将环境更改为“ custom ”,如果我注释掉Rake::Task['db:create']行,代码仍会在' 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...? 为什么Rake::Task['db:create']在我的情况下会影响环境变化......?

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? 看起来在自己的流程中运行每个rake任务会在切换环境时简化一些事情吗?

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' 只需在rake任务后输入RAILS_ENV ='production'

in your case 在你的情况下

rake db:do_something RAILS_ENV='custom' rake db:do_something RAILS_ENV ='custom'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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