diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-05-30 11:51:14 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-05-30 11:51:14 +0200 |
commit | 6c2b569660c9216595d02ce6412e3bdcc7293317 (patch) | |
tree | e7d4ada8e9b88f2fa32278e47c82ba807d4be229 /activerecord | |
parent | f1eddea1e3f6faf93581c43651348f48b2b7d8bb (diff) | |
parent | 1cb52a1733e693fdc76cf92b5408c204b29edcce (diff) | |
download | rails-6c2b569660c9216595d02ce6412e3bdcc7293317.tar.gz rails-6c2b569660c9216595d02ce6412e3bdcc7293317.tar.bz2 rails-6c2b569660c9216595d02ce6412e3bdcc7293317.zip |
Merge pull request #11896 from nkondratyev/fix_pg_columns_for_distinct
Fixed #columns_for_distinct of postgresql adapter
Conflicts:
activerecord/CHANGELOG.md
Diffstat (limited to 'activerecord')
3 files changed, 17 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 9d722c961d..1ed45d8a18 100644 --- a/activerecord/CHANGELOG.md +++ b/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* + * PostgreSQL `reset_pk_sequence!` respects schemas. Fixes #14719. *Yves Senn* diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb index dc994cb7be..0eaaa97454 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb @@ -497,7 +497,7 @@ module ActiveRecord # 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(', ') diff --git a/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb b/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb index 960e33b329..aaa874af7d 100644 --- a/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb +++ b/activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb @@ -355,6 +355,17 @@ module ActiveRecord 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) } |