1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix index_exists? for PostgreSQL expression indexes

Previously Rails expected indexes to be an array of columns, but for
PostgreSQL a expression index can just be a string of text. Handle this
by forcing `Index#columns` to be an Array inside `index_exists?`.

Closes #36739
This commit is contained in:
Stan Hu 2019-07-23 15:06:21 -07:00
parent d3327ca40e
commit ec0dc76c06
2 changed files with 5 additions and 3 deletions

View file

@ -100,7 +100,7 @@ module ActiveRecord
def index_exists?(table_name, column_name, options = {}) def index_exists?(table_name, column_name, options = {})
column_names = Array(column_name).map(&:to_s) column_names = Array(column_name).map(&:to_s)
checks = [] checks = []
checks << lambda { |i| i.columns == column_names } checks << lambda { |i| Array(i.columns) == column_names }
checks << lambda { |i| i.unique } if options[:unique] checks << lambda { |i| i.unique } if options[:unique]
checks << lambda { |i| i.name == options[:name].to_s } if options[:name] checks << lambda { |i| i.name == options[:name].to_s } if options[:name]

View file

@ -253,9 +253,11 @@ module ActiveRecord
def test_expression_index def test_expression_index
with_example_table do with_example_table do
@connection.add_index "ex", "mod(id, 10), abs(number)", name: "expression" expr = "mod(id, 10), abs(number)"
@connection.add_index "ex", expr, name: "expression"
index = @connection.indexes("ex").find { |idx| idx.name == "expression" } index = @connection.indexes("ex").find { |idx| idx.name == "expression" }
assert_equal "mod(id, 10), abs(number)", index.columns assert_equal expr, index.columns
assert_equal true, @connection.index_exists?("ex", expr, name: "expression")
end end
end end