Skip to content

Commit

Permalink
Improved code
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Nov 9, 2024
1 parent 4963a65 commit 53113d3
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/strong_migrations/checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,7 @@ def perform(method, *args, &block)

def perform_method(method, *args)
if StrongMigrations.remove_invalid_indexes && direction == :up && method == :add_index && postgresql?
@skip_retries = true
begin
remove_invalid_index_if_needed(*args)
ensure
@skip_retries = false
end
remove_invalid_index_if_needed(*args)
end
yield
end
Expand Down Expand Up @@ -248,6 +243,16 @@ def retry_lock_timeouts?(method)
)
end

def without_retries
previous_value = @skip_retries
begin
@skip_retries = true
yield
ensure
@skip_retries = previous_value
end
end

# REINDEX INDEX CONCURRENTLY leaves a new invalid index if it fails, so use remove_index instead
def remove_invalid_index_if_needed(*args)
options = args.extract_options!
Expand All @@ -263,8 +268,10 @@ def remove_invalid_index_if_needed(*args)
return if ar_version < 7.1 && !adapter.index_invalid?(table, index_name)

@migration.say("Attempting to remove invalid index")
# TODO pass index schema for extra safety?
@migration.remove_index(table, **{name: index_name}.merge(options.slice(:algorithm)))
without_retries do
# TODO pass index schema for extra safety?
@migration.remove_index(table, **{name: index_name}.merge(options.slice(:algorithm)))
end
end
end
end

0 comments on commit 53113d3

Please sign in to comment.