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

查看:157
本文介绍了策略覆盖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.

我可以封装在例如服务器级别的此信息,这样我就可以检索信息:

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中:

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,它定义了服务器类 - 要求它在这里的阳明?应用程序/初始化ActiveRecord的负载后的database.yml被加载。

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

以上是所谓的只有:active_record在config.frameworks定义。我不知道我怎么会去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.

也许第三个选择是的删除的:active_record从config.frameworks,然后再创建连接,例如在应用程序初始化?恐怕这可能有很多意想不到的副作用。

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.

我的希望的是有一些简单而明显的,我还没有找到,如ActiveRecord的功能,可以让我选择退出的database.yml和编程提供备用配置。

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的好处是,你可以pretty的多少补丁出来任何你不喜欢的,它更好的东西取代。这里的责任是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天全站免登陆