Arslan Ali
Arslan Ali

Reputation: 17812

How to run migrations on a different database server through Heroku?

We need to add the credentials of a database in database.yml file under a different environment like remote_database:

remote_database:
  adapter:
  encoding:
  username:
  ...

And after adding all this, running the following command from the local terminal does the job:

RAILS_ENV=remote_database rails db:migrate

I'm trying to accomplish the same thing on Heroku. I have pushed the changes in config/database.yml, and I'm trying to execute the following command:

RAILS_ENV=remote_database heroku run rake db:migrate
# or
heroku run rake db:migrate RAILS_ENV=remote_database

Seems like Heroku is completely ignoring RAILS_ENV or the settings for remote_database env in config/database.yml file. Heroku always makes the changes in the regular database server connected with it that can be found at DATABASE_URL.

Is there a way to run the migrations on a different database server through Heroku?

Upvotes: 1

Views: 181

Answers (1)

Nam Tran
Nam Tran

Reputation: 468

Heroku injects database.yml and overrides it completely with Rails under 4.1 version or overrides partially and allows a way for us to prevent overriding from Rails 4.1. Check the complete explanation about Rails database connection behaviour on Heroku article

So, in your case

  • If you are using Rails 4.1+: you may try to add url key to your database.yml as described in Active Record 4.1+ Escape Valve section of above link.
  • If you are using Rails under 4.1 version: override database connection by an initializer. See Heroku article ("Otherwise if you are using an older version of Rails you will need to use an initializer" section)

Upvotes: 1

Related Questions