覆盖 database.yml 的策略? [英] Strategies for overriding database.yml?

查看:17
本文介绍了覆盖 database.yml 的策略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的环境中,部署服务器有很多在 database.yml 中的连接信息.也就是说,他们知道自己是开发、测试还是生产服务器,并且他们知道各自的数据库连接信息.

In my environment, the deployment servers have much of the connection information that is in the database.yml. That is, they know whether they are development, test or production servers, and they know their respective database connection information.

例如,我可以将此信息封装在 Server 类中,以便检索信息:

I can encapsulate this information in a Server class for example, so that I can retrieve the information:

Server["environment"] #=> production
Server["db_host"] #=> db5.example.com
Server["db_password"] #=> [a decrypted password]

等等.我想部署一个 Rails 应用程序并让它根据服务器设置自动配置.最好的方法是什么?

and so on. I would like to deploy a Rails application and have it autoconfigure based on the Server settings. What is the best way to do this?

一种方法是在我的 database.yml 中使用 Erb:

One way to do this is Erb in my database.yml:

<%= Server["environment"] %>: 
  adapter: oracle_enhanced
  host: <%= Server["db_host"] %>
  username: db_user 
  password: <%= Server["password"] %>

我对这样做并不太兴奋,但它会奏效.在这种情况下,我会将定义服务器类的server.rb"放在哪里——在 yml 中需要它?在 ActiveRecord 加载 database.yml 之后加载 app/initializers.

I'm not too thrilled about doing it this way, but it would work. In this case, where would I put the 'server.rb' that defines the Server class--require it here in the yml? app/initializers gets loaded after ActiveRecord loads database.yml.

另一种可能的解决方案是以某种方式覆盖 railties 的数据库初始化程序:

Another possible solution is to somehow override railties' database initializer:

# File railties/lib/initializer.rb, line 903
def database_configuration
  require 'erb'
  YAML::load(ERB.new(IO.read(database_configuration_file)).result)
end

只有在 config.frameworks 中定义了 :active_record 时才会调用上述内容.我不确定如何在 Rails 启动序列中尽早覆盖它.

The above is called only if :active_record is defined in config.frameworks. I'm not sure how I would go about overriding this early enough in the Rails startup sequence.

也许第三种选择是从 config.frameworks 中删除 :active_record,然后稍后创建连接,比如在应用初始化程序中?恐怕这可能会产生很多意想不到的副作用.

Maybe a third option is to remove :active_record from config.frameworks, and then create the connection later, say in the app initializers? I'm afraid this may have lots of unintended side effects.

希望有一些我没有找到的简单而明显的东西,例如允许我选择退出 database.yml 并以编程方式提供备用配置的 ActiveRecord 功能.p>

I'm hoping that there's something simple and obvious that I haven't found, such as an ActiveRecord feature that allows me to opt out of database.yml and provide alternate config programmatically.

推荐答案

这里有两个技巧可能会有所帮助.一个是您已经触及的内容,即对加载到数据库配置中的例程进行猴子修补,这当然是值得探索的事情.Ruby 的好处是你几乎可以修补任何你不喜欢的东西并用更好的东西代替它.这里的责任是新版本的 Rails 可能使用不同的配置机制,您的补丁将导致一切崩溃.也许这是值得付出的代价.

There's two tricks which might help here. One is what you've touched on, that being monkey-patching the routine that loads in the database configuration, and that's certainly something worth exploring. The nice thing about Ruby is you can pretty much patch out anything you don't like and replace it with something better. The liability here is that a newer version of Rails might use a different mechanism for configuration and your patch will cause everything to break. Maybe that's a price worth paying.

另一种方法是将 database.yml 文件保存在服务器上而不是存储库中,并使用某种部署脚本将其链接到适当的位置.这种方法的优点是您的版本控制系统中没有重要的密码,您可以更新服务器的配置而无需修补应用程序.

The other approach is to keep the database.yml file on the server instead of in your repository, and have some kind of deployment script that links it in to the appropriate location. The advantage to this approach is you don't have important passwords floating around in your version control system and you can update a server's configuration without having to patch the application.

这篇关于覆盖 database.yml 的策略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆