Rails run migration only for development environment

Viewed 1563

Is there a way to run migrations just on the development database.

I use Heroku Connect where I sync certain tables in Postgres with certain tables in Salesforce.

Presently I did some hack where I made some migrations that mirror tables in Postgres that I am syncing with Salesforce.

Then I remove the migration files (put them in a separate folder), before I push to heroku. This needs to be done because the migrations files will mess with how heroku connect works. Just for context. The models look something like this.

class MyModel < ApplicationRecord
  self.table_name = 'salesforce.someTable__c' unless Rails.env.development?
end

So cannot really do migrations for my_models tables in db.

Problem is any time I have to change the nature of the syncing, I need to bring those files back in, roll back the db, update those original migration files, migrate the database, then remove those migration files.

Not really the best solution.

So back to the question. Is there a way to just run migrations on development, such that when I run migrations on production it will skip those migrations that are only for development. Something like:

if Rails.env.development?
  ...
else
  ...
end

or

if Rails.env.production?
  ...
else
  ...
end
1 Answers

Heroku maintains an environment variable called: RAILS_ENV, and the same variable is maintained by Rails in development too, so you can go for it.

allowed_env = 'development'

if ENV['RAILS_ENV'] == allowed_env
  create_table :users do |t|
  end
end

On Heroku RAILS_ENV is either set to staging or production.

Related