Add guard clause to migration to check for index before attempting to add or remove it (#4644)
This commit is contained in:
parent
b1899b7e2e
commit
da45790e2e
1 changed files with 18 additions and 3 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue