Skip to content

Commit

Permalink
Fixed columns_for_distinct of postgresql adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
nkondratyev committed Aug 15, 2013
1 parent e90f0e0 commit 1cb52a1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,8 @@
* Fixed `columns_for_distinct` of postgresql adapter to work correctly
with orders without sort direction modifiers.

*Nikolay Kondratyev*

* Assign inet/cidr attribute with `nil` value for invalid address. * Assign inet/cidr attribute with `nil` value for invalid address.


Example: Example:
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ def columns_for_distinct(columns, orders) #:nodoc:
# Convert Arel node to string # Convert Arel node to string
s = s.to_sql unless s.is_a?(String) s = s.to_sql unless s.is_a?(String)
# Remove any ASC/DESC modifiers # Remove any ASC/DESC modifiers
s.gsub(/\s+(ASC|DESC)\s*(NULLS\s+(FIRST|LAST)\s*)?/i, '') s.gsub(/\s+(?:ASC|DESC)?\s*(?:NULLS\s+(?:FIRST|LAST)\s*)?/i, '')
}.reject(&:blank?).map.with_index { |column, i| "#{column} AS alias_#{i}" } }.reject(&:blank?).map.with_index { |column, i| "#{column} AS alias_#{i}" }


[super, *order_columns].join(', ') [super, *order_columns].join(', ')
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -259,6 +259,17 @@ def test_columns_for_distinct_with_nulls
assert_equal "posts.title, posts.updater_id AS alias_0", @connection.columns_for_distinct("posts.title", ["posts.updater_id desc nulls last"]) assert_equal "posts.title, posts.updater_id AS alias_0", @connection.columns_for_distinct("posts.title", ["posts.updater_id desc nulls last"])
end end


def test_columns_for_distinct_without_order_specifiers
assert_equal "posts.title, posts.updater_id AS alias_0",
@connection.columns_for_distinct("posts.title", ["posts.updater_id"])

assert_equal "posts.title, posts.updater_id AS alias_0",
@connection.columns_for_distinct("posts.title", ["posts.updater_id nulls last"])

assert_equal "posts.title, posts.updater_id AS alias_0",
@connection.columns_for_distinct("posts.title", ["posts.updater_id nulls first"])
end

def test_raise_error_when_cannot_translate_exception def test_raise_error_when_cannot_translate_exception
assert_raise TypeError do assert_raise TypeError do
@connection.send(:log, nil) { @connection.execute(nil) } @connection.send(:log, nil) { @connection.execute(nil) }
Expand Down

0 comments on commit 1cb52a1

Please sign in to comment.