Skip to content

Commit 4e35d9f

Browse files
committed
Fix remove_index to be able to remove expression indexes
Fixes rails#26635, rails#26641.
1 parent e8ba0c0 commit 4e35d9f

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ def index_name(table_name, options) #:nodoc:
766766
raise ArgumentError, "You must specify the index name"
767767
end
768768
else
769-
index_name(table_name, column: options)
769+
index_name(table_name, index_name_options(options))
770770
end
771771
end
772772

@@ -1120,18 +1120,14 @@ def update_table_definition(table_name, base) #:nodoc:
11201120
end
11211121

11221122
def add_index_options(table_name, column_name, comment: nil, **options) # :nodoc:
1123-
if column_name.is_a?(String) && /\W/.match?(column_name)
1124-
column_names = column_name
1125-
else
1126-
column_names = Array(column_name)
1127-
end
1123+
column_names = index_column_names(column_name)
11281124

11291125
options.assert_valid_keys(:unique, :order, :name, :where, :length, :internal, :using, :algorithm, :type)
11301126

11311127
index_type = options[:type].to_s if options.key?(:type)
11321128
index_type ||= options[:unique] ? "UNIQUE" : ""
11331129
index_name = options[:name].to_s if options.key?(:name)
1134-
index_name ||= index_name(table_name, index_name_options(column_names))
1130+
index_name ||= index_name(table_name, column_names)
11351131

11361132
if options.key?(:algorithm)
11371133
algorithm = index_algorithms.fetch(options[:algorithm]) {
@@ -1208,13 +1204,13 @@ def index_name_for_remove(table_name, options = {}) # :doc:
12081204

12091205
if options.is_a?(Hash)
12101206
checks << lambda { |i| i.name == options[:name].to_s } if options.key?(:name)
1211-
column_names = Array(options[:column]).map(&:to_s)
1207+
column_names = index_column_names(options[:column])
12121208
else
1213-
column_names = Array(options).map(&:to_s)
1209+
column_names = index_column_names(options)
12141210
end
12151211

1216-
if column_names.any?
1217-
checks << lambda { |i| i.columns.join("_and_") == column_names.join("_and_") }
1212+
if column_names.present?
1213+
checks << lambda { |i| index_name(table_name, i.columns) == index_name(table_name, column_names) }
12181214
end
12191215

12201216
raise ArgumentError, "No name or columns specified" if checks.none?
@@ -1261,8 +1257,16 @@ def create_alter_table(name)
12611257
AlterTable.new create_table_definition(name)
12621258
end
12631259

1260+
def index_column_names(column_names)
1261+
if column_names.is_a?(String) && /\W/.match?(column_names)
1262+
column_names
1263+
else
1264+
Array(column_names)
1265+
end
1266+
end
1267+
12641268
def index_name_options(column_names)
1265-
if column_names.is_a?(String)
1269+
if column_names.is_a?(String) && /\W/.match?(column_names)
12661270
column_names = column_names.scan(/\w+/).join("_")
12671271
end
12681272

activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,12 @@ def test_expression_index
263263

264264
def test_index_with_opclass
265265
with_example_table do
266-
@connection.add_index "ex", "data varchar_pattern_ops", name: "with_opclass"
267-
index = @connection.indexes("ex").find { |idx| idx.name == "with_opclass" }
266+
@connection.add_index "ex", "data varchar_pattern_ops"
267+
index = @connection.indexes("ex").find { |idx| idx.name == "index_ex_on_data_varchar_pattern_ops" }
268268
assert_equal "data varchar_pattern_ops", index.columns
269+
270+
@connection.remove_index "ex", "data varchar_pattern_ops"
271+
assert_not @connection.indexes("ex").find { |idx| idx.name == "index_ex_on_data_varchar_pattern_ops" }
269272
end
270273
end
271274

0 commit comments

Comments
 (0)