Skip to content
Browse files

Fixed `columns_for_distinct` of postgresql adapter

  • Loading branch information...
1 parent e90f0e0 commit 1cb52a1733e693fdc76cf92b5408c204b29edcce @nkondratyev nkondratyev committed Aug 14, 2013
View
5 activerecord/CHANGELOG.md
@@ -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.
Example:
View
2 activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
@@ -475,7 +475,7 @@ def columns_for_distinct(columns, orders) #:nodoc:
# Convert Arel node to string
s = s.to_sql unless s.is_a?(String)
# 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}" }
[super, *order_columns].join(', ')
View
11 activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb
@@ -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"])
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
assert_raise TypeError do
@connection.send(:log, nil) { @connection.execute(nil) }

0 comments on commit 1cb52a1

Please sign in to comment.
Something went wrong with that request. Please try again.