Add guard clause to migration to check for index before attempting to add or remove it (#4644)

This commit is contained in:
Molly Struve 2019-10-28 14:27:31 -05:00 committed by Ben Halpern
parent b1899b7e2e
commit da45790e2e

View file

@ -1,8 +1,23 @@
class AddOldUsernameIndexToUsers < ActiveRecord::Migration[5.2]
disable_ddl_transaction!
def change
add_index :users, :old_username, algorithm: :concurrently
add_index :users, :old_old_username, algorithm: :concurrently
def up
if indexes(:users).none? { |idx| idx.columns.map(&:to_sym) == [:old_username] }
add_index :users, :old_username, algorithm: :concurrently
end
if indexes(:users).none? { |idx| idx.columns.map(&:to_sym) == [:old_old_username] }
add_index :users, :old_old_username, algorithm: :concurrently
end
end
def down
if indexes(:users).any? { |idx| idx.columns.map(&:to_sym) == [:old_username] }
remove_index :users, :old_username
end
if indexes(:users).any? { |idx| idx.columns.map(&:to_sym) == [:old_old_username] }
remove_index :users, :old_old_username
end
end
end