0

I have two Heroku apps, one for staging, one for production. Each has its own separate, primary DB. I have a 2nd Heroku DB that needs to be accessed by both staging and production.

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. Afterwards, if I examine ActiveRecord::Base.configurations, I see a new key/value pair that's been added. The key is the URL and the value appears to be a copy of the configuration values for the 'production' key.

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.

mdn
  • 3
  • 2

1 Answers1

0

This hack works, but seems like a bad idea to me:

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
mdn
  • 3
  • 2