Skip to content

Commit

Permalink
Improved code
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Nov 7, 2024
1 parent e9dbe34 commit 7716f45
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/strong_migrations/safe_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@ def safe_by_default_method?(method)

def safe_add_index(*args, **options)
disable_transaction
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 CONCURRENTLY #{connection.quote_table_name(index_name)}")
end
else
@migration.add_index(*args, **options.merge(algorithm: :concurrently))
end
add_index_or_reindex(*args, **options.merge(algorithm: :concurrently))
end

def safe_remove_index(*args, **options)
Expand Down Expand Up @@ -126,6 +119,17 @@ def in_transaction?
connection.open_transactions > 0
end

def add_index_or_reindex(*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)
end
end

def invalid_index_name(*args, **options)
# ensures has same options as existing index
return nil unless connection.index_exists?(*args, **options.merge(valid: false))
Expand Down

0 comments on commit 7716f45

Please sign in to comment.