Davigor
Davigor

Reputation: 403

Simple way to run rails migrations for a remote database

I have a rails app and want to split off my Postgres database to a remote, managed one rather than the standard local one. It seemed easy enough to configure this— however, now I am trying to run my migrations against this new db and it’s proving more difficult. I’m using Mina to deploy, which calls rake db:migrate as part of the deployment. It does not run the migrations, however, as it says that all migrations are up to date, and my create calls can’t find the tables, so I assume the migrations have not run on the remote db.

Whst’s the best way to accomplish this? Every other answer I’ve found involves adding something like ActiveRecord::Base.establish_connection(db_params) command to the top of every migration and every model. This seems absurd— I have probably a 75 migrations at this point. Is there no better way? Is this even the right approach, or could I also use the generated schemes file somehow?

Upvotes: 3

Views: 1340

Answers (1)

Eyeslandic
Eyeslandic

Reputation: 14900

You can set up your database credentials in database.yml with something like this.

remote:
  adapter: postgresql
  host: your.remote.host
  database: yourdb
  username: user
  password: pass
  pool: 5
  timeout: 5000
  locale: en_US.UTF8

Then run your migrations like

RAILS_ENV=remote rails db:migrate

Upvotes: 3

Related Questions