Skip to content

Commit

Permalink
Improved code
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Nov 8, 2024
1 parent 9c6ba16 commit babab99
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions lib/strong_migrations/checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,28 +247,19 @@ def retry_lock_timeouts?(method)

# REINDEX INDEX CONCURRENTLY leaves a new invalid index if it fails, so use remove_index instead
def remove_invalid_index_if_needed(*args, **options)
index_name = invalid_index_name(*args, **options)
return if index_name.nil?

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

def invalid_index_name(*args, **options)
# avoid errors with index_exists?
return nil if args.size != 2

# ensures has same options as existing index
return nil unless connection.index_exists?(*args, **options.merge(valid: false))
# check args to avoid errors with index_exists?
return unless args.size == 2 && connection.index_exists?(*args, **options.merge(valid: false))

table, columns = args
index_name = options.fetch(:name, connection.index_name(table, columns))

# valid option is ignored for Active Record < 7.1, so check name as well
return nil unless ar_version >= 7.1 || adapter.index_invalid?(table, index_name)
return if ar_version < 7.1 && !adapter.index_invalid?(table, index_name)

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

0 comments on commit babab99

Please sign in to comment.