Skip to content

Commit 1c968b4

Browse files
authored
Merge pull request rails#26655 from kamipo/fix_remove_expression_index
Fix `remove_index` to be able to remove expression indexes
2 parents fab91c6 + 4e35d9f commit 1c968b4

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
@@ -768,7 +768,7 @@ def index_name(table_name, options) #:nodoc:
768768
raise ArgumentError, "You must specify the index name"
769769
end
770770
else
771-
index_name(table_name, column: options)
771+
index_name(table_name, index_name_options(options))
772772
end
773773
end
774774

@@ -1123,18 +1123,14 @@ def update_table_definition(table_name, base) #:nodoc:
11231123
end
11241124

11251125
def add_index_options(table_name, column_name, comment: nil, **options) # :nodoc:
1126-
if column_name.is_a?(String) && /\W/.match?(column_name)
1127-
column_names = column_name
1128-
else
1129-
column_names = Array(column_name)
1130-
end
1126+
column_names = index_column_names(column_name)
11311127

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

11341130
index_type = options[:type].to_s if options.key?(:type)
11351131
index_type ||= options[:unique] ? "UNIQUE" : ""
11361132
index_name = options[:name].to_s if options.key?(:name)
1137-
index_name ||= index_name(table_name, index_name_options(column_names))
1133+
index_name ||= index_name(table_name, column_names)
11381134

11391135
if options.key?(:algorithm)
11401136
algorithm = index_algorithms.fetch(options[:algorithm]) {
@@ -1214,13 +1210,13 @@ def index_name_for_remove(table_name, options = {})
12141210

12151211
if options.is_a?(Hash)
12161212
checks << lambda { |i| i.name == options[:name].to_s } if options.key?(:name)
1217-
column_names = Array(options[:column]).map(&:to_s)
1213+
column_names = index_column_names(options[:column])
12181214
else
1219-
column_names = Array(options).map(&:to_s)
1215+
column_names = index_column_names(options)
12201216
end
12211217

1222-
if column_names.any?
1223-
checks << lambda { |i| i.columns.join("_and_") == column_names.join("_and_") }
1218+
if column_names.present?
1219+
checks << lambda { |i| index_name(table_name, i.columns) == index_name(table_name, column_names) }
12241220
end
12251221

12261222
raise ArgumentError, "No name or columns specified" if checks.none?
@@ -1267,8 +1263,16 @@ def create_alter_table(name)
12671263
AlterTable.new create_table_definition(name)
12681264
end
12691265

1266+
def index_column_names(column_names)
1267+
if column_names.is_a?(String) && /\W/.match?(column_names)
1268+
column_names
1269+
else
1270+
Array(column_names)
1271+
end
1272+
end
1273+
12701274
def index_name_options(column_names)
1271-
if column_names.is_a?(String)
1275+
if column_names.is_a?(String) && /\W/.match?(column_names)
12721276
column_names = column_names.scan(/\w+/).join("_")
12731277
end
12741278

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)