Skip to content

Commit

Permalink
Merge pull request #46659 from fatkodima/indexes-nulls-not-distinct
Browse files Browse the repository at this point in the history
Handle `NULLS [NOT] DISTINCT` when getting indexes in PostgreSQL
  • Loading branch information
byroot committed Jan 6, 2023
2 parents dcb4f4d + 54e3582 commit f2e0844
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
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
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 f2e0844

Please sign in to comment.