Skip to content

Commit be7b922

Browse files
author
Aidan Haran
committed
Fix removal of invalid ordering from select statements
1 parent 5f0c423 commit be7b922

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- [#880](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/880) Handle any default column class when deduplicating
1212
- [#861](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/861) Fix Rails 6.1 database config
1313
- [#885](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/885) Fix the quoting of ActiveModel attributes
14+
- [#890](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/890) Fix removal of invalid ordering from select statements
1415

1516
#### Changed
1617

lib/arel/visitors/sqlserver.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,11 @@ def visit_Arel_Nodes_OuterJoin(o, collector)
184184
end
185185
end
186186

187-
def collect_in_clause(left, right, collector)
188-
if Array === right
189-
right.each { |node| remove_invalid_ordering_from_select_statement(node) }
187+
def visit_Arel_Nodes_In(o, collector)
188+
if Array === o.right
189+
o.right.each { |node| remove_invalid_ordering_from_select_statement(node) }
190190
else
191-
remove_invalid_ordering_from_select_statement(right)
191+
remove_invalid_ordering_from_select_statement(o.right)
192192
end
193193

194194
super

test/cases/aido.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
require "cases/helper_sqlserver"
3+
require "models/developer"
4+
require "models/computer"
5+
require "models/project"
6+
require "models/company"
7+
require "models/ship"
8+
require "models/pirate"
9+
require "models/car"
10+
require "models/bulb"
11+
require "models/author"
12+
require "models/image"
13+
require "models/post"
14+
require "models/drink_designer"
15+
require "models/chef"
16+
require "models/department"
17+
require "models/club"
18+
require "models/membership"
19+
20+
class AidoTestSQLServer < ActiveRecord::TestCase
21+
fixtures :accounts, :companies, :developers, :projects, :developers_projects,
22+
:ships, :pirates, :authors, :author_addresses, :books, :memberships, :clubs
23+
24+
def setup
25+
Account.destroyed_account_ids.clear
26+
end
27+
28+
def test_dependence_with_missing_association
29+
30+
# byebug
31+
32+
Account.destroy_all
33+
firm = Firm.find(1)
34+
assert_nil firm.account
35+
36+
$DEBUG = true
37+
38+
firm.destroy
39+
end
40+
end

test/cases/in_clause_test_sqlserver.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,31 @@ class InClauseTestSQLServer < ActiveRecord::TestCase
3333
assert_includes posts.to_sql, "ORDER BY [authors].[name]"
3434
assert_equal 8, posts.length
3535
end
36+
37+
it "removes ordering from 'not' subqueries" do
38+
authors_subquery = Author.where.not(name: ["Mary", "Bob"]).order(:name)
39+
posts = Post.where(author: authors_subquery)
40+
41+
assert_includes authors_subquery.to_sql, "ORDER BY [authors].[name]"
42+
assert_not_includes posts.to_sql, "ORDER BY [authors].[name]"
43+
assert_equal 5, posts.length
44+
end
45+
46+
it "does not remove ordering from 'not' subquery that includes a limit" do
47+
authors_subquery = Author.where.not(name: ["Ronan", "Mary", "Bob"]).order(:name).limit(2)
48+
posts = Post.where(author: authors_subquery)
49+
50+
assert_includes authors_subquery.to_sql, "ORDER BY [authors].[name]"
51+
assert_includes posts.to_sql, "ORDER BY [authors].[name]"
52+
assert_equal 5, posts.length
53+
end
54+
55+
it "does not remove ordering from 'not' subquery that includes an offset" do
56+
authors_subquery = Author.where.not(name: ["David", "Ronan", "Cian"]).order(:name).offset(1)
57+
posts = Post.where(author: authors_subquery)
58+
59+
assert_includes authors_subquery.to_sql, "ORDER BY [authors].[name]"
60+
assert_includes posts.to_sql, "ORDER BY [authors].[name]"
61+
assert_equal 3, posts.length
62+
end
3663
end

0 commit comments

Comments
 (0)