ankane/multiverse

[Heroku] migrate command not picking the right database when DATABASE_URL is set

EdgarOrtegaRamirez opened this issue · 8 comments

Hello! First of all, thanks for this amazing lib, It is really helpful.

I've made a deployment to a Heroku app which has set the DATABASE_URL and the additional CATALOG_DATABASE_URL, but when migrating migrations are run on the main database and the catalog db is untouched.

Do you know if this is a known issue?

Hey @EdgarOrtegaRamirez, a similar issue was reported in #16, but haven't been able to reproduce. A few questions:

  1. Does it work in development?
  2. What version of Rails and Multiverse are you on? (make sure it's the latest Multiverse - 0.2.1)
  3. Are migrations for the catalog database in db/catalog/migrate?
  4. Do the migrations just not run, or do they run on the main database instead?
  5. What does your config/database.yml look like?

@ankane Hey, thanks for the quick answer! This was my setup when I submitted the issue:

  1. Yes, it is working pretty well on my local machine.
  2. Rails 5.2.0 and Multiverse 0.2.0 (from rubygems)
  3. Yes, migrations are in the db/catalog/migrate/ folder
  4. They run, but on the main database.
  5. my database.yml at that moment was this:
default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV['DB_USERNAME'] %>
  password: <%= ENV['DB_PASSWORD'] %>

development:
  <<: *default
  database: app_development

test:
  <<: *default
  database: app_test

staging:
  <<: *default
  database: app_staging
  pool: <%= ENV['DB_POOL'] %>

production:
  <<: *default
  database: app_production
  pool: <%= ENV['DB_POOL'] %>

catalog_development:
  <<: *default
  database: catalog_development

catalog_test:
  <<: *default
  database: catalog_test

catalog_staging:
  <<: *default
  database: catalog_staging

catalog_production:
  <<: *default
url: <%= ENV['CATALOG_DATABASE_URL'] %>

I managed to solve the issue by renaming DATABASE_URL -> APP_DATABASE_URL and changing the database.yml file as shown below:

diff --git a/config/database.yml b/config/database.yml
index be79c1b4..377c7cb2 100644
--- a/config/database.yml
+++ b/config/database.yml
@@ -15,12 +15,12 @@ test:
 
 staging:
   <<: *default
-  database: app_staging
+  url: <%= ENV['APP_DATABASE_URL'] %>
   pool: <%= ENV['DB_POOL'] %>
 
 production:
   <<: *default
-  database: app_production
+  url: <%= ENV['APP_DATABASE_URL'] %>
   pool: <%= ENV['DB_POOL'] %>
 
catalog_development:
@@ -33,7 +33,7 @@ catalog_test:
 
 catalog_staging:
   <<: *default
-  database: catalog_staging
+  url: <%= ENV['CATALOG_DATABASE_URL'] %>
 
 catalog_production:
   <<: *default
-  database: catalog_production
+  url: <%= ENV['CATALOG_DATABASE_URL'] %>

The problem was the url indentation was off, so it wasn't affecting your catalog_production config.

I'm pretty sure that was a bad paste, sorry.

Was able to reproduce this again but on the CodeShip CI, DATABASE_URL was set in the configuration, I removed it and then it picked the right database configuration.

When duplicate connection information is provided the environment variable will take precedence:
from: https://edgeguides.rubyonrails.org/configuring.html#connection-preference

This seems like a known issue...

Config is merged with ENV["DATABASE_URL"] if you don't have a url key for that environment. That's just how it's designed in Rails.