Create idempotent migrations

When you want to write destructive migrations please use if_exists: true when you’re trying to remove a table and check for a table existence when you want to add or remove a column ActiveRecord::Base.connection.table_exists?

Case 1:

def up
  drop_table :kittens, if_exists: true
end


Case 2:

def up
  return unless ActiveRecord::Base.connection.table_exists?(:kittens)
  add_column :kittens, :kind, :integer
end


Ali

Get our stories delivered

From us to your inbox weekly.