How to check if a colum is present in table in rails migration

Viewed 14

currently rails version 5.

In it I want to write the migration in condition that migration should run only when the field does not exist in the database for a particular table.

Please suggest how to implement it in rails, Thanks in advance.

1 Answers
  def self.up
    unless ActiveRecord::Base.connection.column_exists?(:table_name, :column_name)
      add_column :table_name, :column_name, :data_type
    end
  end
Related