Skip to content

Commit

Permalink
Switched back to remove_index
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Nov 7, 2024
1 parent a1f0ad4 commit 425a651
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions lib/strong_migrations/safe_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ def safe_by_default_method?(method)

def safe_add_index(*args, **options)
disable_transaction
add_index_or_reindex(*args, **options.merge(algorithm: :concurrently))
remove_index_if_needed(*args, **options.merge(algorithm: :concurrently))
@migration.add_index(*args, **options.merge(algorithm: :concurrently))
end

def safe_remove_index(*args, **options)
Expand Down Expand Up @@ -119,14 +120,12 @@ def in_transaction?
connection.open_transactions > 0
end

def add_index_or_reindex(*args, **options)
# REINDEX INDEX CONCURRENTLY leaves a new invalid index if it fails, so use remove_index instead
def remove_index_if_needed(*args, **options)
if direction == :up && (index_name = invalid_index_name(*args, **options))
@migration.safety_assured do
# TODO pass index schema for extra safety?
@migration.execute("REINDEX INDEX #{connection.index_algorithm(options[:algorithm])} #{connection.quote_table_name(index_name)}")
end
else
@migration.add_index(*args, **options)
@migration.say("Removing invalid index")
# TODO pass index schema for extra safety?
@migration.remove_index(args[0], **{name: index_name}.merge(options.slice(:algorithm)))
end
end

Expand Down

0 comments on commit 425a651

Please sign in to comment.