if ActiveRecord.gem_version >= Gem::Version.new('5.0')
  class AddUniqueIndexToFriendships < ActiveRecord::Migration[4.2]; end
else
  class AddUniqueIndexToFriendships < ActiveRecord::Migration; end
end

AddUniqueIndexToFriendships.class_eval do
  disable_ddl_transaction!

  def self.up
    add_index :friendships, [:friendable_id, :friend_id], unique: true, algorithm: :concurrently
  end

  def self.down
    remove_index :friendships, [:friendable_id, :friend_id]
  end
end

if ActiveRecord.gem_version >= Gem::Version.new('5.0')
  class AddUniqueIndexToFriendships < ActiveRecord::Migration[4.2]; end
else
  class AddUniqueIndexToFriendships < ActiveRecord::Migration; end
end

AddUniqueIndexToFriendships.class_eval do
  disable_ddl_transaction!

  def self.up
    add_index :friendships, [:friendable_id, :friend_id], unique: true, algorithm: :concurrently unless index_exists? (:friendable_id, :friend_id)
  end

  def self.down
    remove_index :friendships, [:friendable_id, :friend_id]
  end
end

Check if unique index already exists
Check if unique index already exists

Add the unique index unless it already exists between friendable_id and friend_id.