问题描述
在我的环境中,部署服务器在 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 类的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 功能.>
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的策略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!