diff --git a/lib/strong_migrations/safe_methods.rb b/lib/strong_migrations/safe_methods.rb index 89560454..0bd43a8a 100644 --- a/lib/strong_migrations/safe_methods.rb +++ b/lib/strong_migrations/safe_methods.rb @@ -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) @@ -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