From da45790e2e97d17dd124d512b8e25b690cf5fbd9 Mon Sep 17 00:00:00 2001 From: Molly Struve Date: Mon, 28 Oct 2019 14:27:31 -0500 Subject: [PATCH] Add guard clause to migration to check for index before attempting to add or remove it (#4644) --- ...5185619_add_old_username_index_to_users.rb | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/db/migrate/20191025185619_add_old_username_index_to_users.rb b/db/migrate/20191025185619_add_old_username_index_to_users.rb index dc3ec36d0..2edc69f72 100644 --- a/db/migrate/20191025185619_add_old_username_index_to_users.rb +++ b/db/migrate/20191025185619_add_old_username_index_to_users.rb @@ -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