简体   繁体   English

如何与多个Heroku Rails应用程序共享第二个Heroku数据库

[英]How to share a second Heroku DB with multiple Heroku Rails apps

I have two Heroku apps, one for staging, one for production. 我有两个Heroku应用程序,一个用于登台,一个用于生产。 Each has its own separate, primary DB. 每个数据库都有自己的单独主数据库。 I have a 2nd Heroku DB that needs to be accessed by both staging and production. 我有一个第二Heroku的DB需要由双方临时和生产进行访问。

Running Rails 4.1.0.rc1, when I try to use 'establish_connection' and a URL I've configured with all the correct values for the secondary DB, I end up connecting to the primary DB instead. 运行Rails 4.1.0.rc1时,当我尝试使用“ Establishment_connection”和为辅助数据库配置了所有正确值的URL时,我最终连接到了主数据库。 Afterwards, if I examine ActiveRecord::Base.configurations, I see a new key/value pair that's been added. 之后,如果我检查ActiveRecord :: Base.configurations,则会看到已添加的新键/值对。 The key is the URL and the value appears to be a copy of the configuration values for the 'production' key. 关键是URL,该值似乎是“生产”关键的配置值的副本。

All my research indicates that all I need to do is 我所有的研究表明,我要做的就是

User.establish_connection ENV['SECONDARY_DATABASE_URL']

This method works fine, locally, but fails on Heroku. 此方法在本地可以正常使用,但在Heroku上失败。

This hack works, but seems like a bad idea to me: 这种hack有效,但对我来说似乎是个坏主意:

url = ENV['MC_CONVERSION_DATABASE_URL']

if url
  uri = URI url
  db_config = {
    'adapter'     => 'postgresql',
    'username'    => uri.user,
    'password'    => uri.password,
    'port'        => uri.port,
    'database'    => uri.path[1..-1], # remove leading '/'
    'host'        => uri.host
  }
else
  # in the dev environment, use one of the configs from database.yml
  db_config = :mc_conversion
end

McUser.establish_connection db_config
McMod.establish_connection db_config

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

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