Skip to content

Commit

Permalink
Handle NULLS [NOT] DISTINCT when getting indexes in PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Dec 7, 2022
1 parent 90a272a commit 54e3582
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ def indexes(table_name) # :nodoc:
oid = row[4]
comment = row[5]
valid = row[6]

using, expressions, where = inddef.scan(/ USING (\w+?) \((.+?)\)(?: WHERE (.+))?\z/m).flatten
using, expressions, where = inddef.scan(/ USING (\w+?) \((.+?)\)(?: NULLS(?: NOT)? DISTINCT)?(?: WHERE (.+))?\z/m).flatten

orders = {}
opclasses = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,20 @@ def test_invalid_index
end
end

def test_index_with_not_distinct_nulls
skip if ActiveRecord::Base.connection.database_version < 15_00_00

with_example_table do
@connection.execute(<<~SQL)
CREATE UNIQUE INDEX index_ex_on_data ON ex (data) NULLS NOT DISTINCT WHERE number > 0
SQL

index = @connection.indexes(:ex).first
assert_equal true, index.unique
assert_match("number", index.where)
end
end

def test_columns_for_distinct_zero_orders
assert_equal "posts.id",
@connection.columns_for_distinct("posts.id", [])
Expand Down

0 comments on commit 54e3582

Please sign in to comment.