ruby-on-railsdata-migration

Rails - Skip data_migrations in specific environments


I am writing a data_migration for which I am using data-migrate gem. I have a situation where I should skip one specific data_migration in Production environment alone. I can do it like this

class BackfillNonStatutoryResultsWithAssessmentPeriod < ActiveRecord::Migration[7.0]
  def up 
    unless RAILS_ENV == "production"
     # Do something
    end
  end
end

 def down
   raise ActiveRecord::IrreversibleMigration
  end
end

What is the best and efficient way to achieve this?


Solution

  • I think your code looks fine, but I would improve it a little bit

    class BackfillNonStatutoryResultsWithAssessmentPeriod < ActiveRecord::Migration[7.0]
      def up
        return if Rails.env.production?
        # Do something
      end
    
      def down
       raise ActiveRecord::IrreversibleMigration
      end
    end