Skip to content

Commit 662f543

Browse files
authored
Merge pull request #2070 from koic/build_subselect_doesnt_have_ordering
`build_subselect` does not have ordering
2 parents e064530 + 31c244f commit 662f543

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

lib/arel/visitors/oracle.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,17 @@ def is_distinct_from(o, collector)
248248
collector = visit [o.left, o.right, 0, 1], collector
249249
collector << ")"
250250
end
251+
252+
# Oracle will occur an error `ORA-00907: missing right parenthesis`
253+
# when using `ORDER BY` in `UPDATE` or `DELETE`'s subquery.
254+
#
255+
# This method has been overridden based on the following code.
256+
# https://github.com/rails/rails/blob/v6.1.0.rc1/activerecord/lib/arel/visitors/to_sql.rb#L815-L825
257+
def build_subselect(key, o)
258+
stmt = super
259+
stmt.orders = [] # `orders` will never be set to prevent `ORA-00907`.
260+
stmt
261+
end
251262
end
252263
end
253264
end

lib/arel/visitors/oracle12.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,17 @@ def is_distinct_from(o, collector)
155155
collector = visit [o.left, o.right, 0, 1], collector
156156
collector << ")"
157157
end
158+
159+
# Oracle will occur an error `ORA-00907: missing right parenthesis`
160+
# when using `ORDER BY` in `UPDATE` or `DELETE`'s subquery.
161+
#
162+
# This method has been overridden based on the following code.
163+
# https://github.com/rails/rails/blob/v6.1.0.rc1/activerecord/lib/arel/visitors/to_sql.rb#L815-L825
164+
def build_subselect(key, o)
165+
stmt = super
166+
stmt.orders = [] # `orders` will never be set to prevent `ORA-00907`.
167+
stmt
168+
end
158169
end
159170
end
160171
end

spec/active_record/connection_adapters/oracle_enhanced_adapter_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,45 @@ class ::TestEmployee2 < ActiveRecord::Base
150150
end
151151
end
152152

153+
describe "`has_many` assoc has `dependent: :delete_all` with `order`" do
154+
before(:all) do
155+
schema_define do
156+
create_table :test_posts do |t|
157+
t.string :title
158+
end
159+
create_table :test_comments do |t|
160+
t.integer :test_post_id
161+
t.string :description
162+
end
163+
add_index :test_comments, :test_post_id
164+
end
165+
class ::TestPost < ActiveRecord::Base
166+
has_many :test_comments, -> { order(:id) }, dependent: :delete_all
167+
end
168+
class ::TestComment < ActiveRecord::Base
169+
belongs_to :test_post
170+
end
171+
TestPost.transaction do
172+
post = TestPost.create!(title: "Title")
173+
TestComment.create!(test_post_id: post.id, description: "Description")
174+
end
175+
end
176+
177+
after(:all) do
178+
schema_define do
179+
drop_table :test_comments
180+
drop_table :test_posts
181+
end
182+
Object.send(:remove_const, "TestPost")
183+
Object.send(:remove_const, "TestComment")
184+
ActiveRecord::Base.clear_cache!
185+
end
186+
187+
it "should not occur `ActiveRecord::StatementInvalid: OCIError: ORA-00907: missing right parenthesis`" do
188+
expect { TestPost.first.destroy }.not_to raise_error
189+
end
190+
end
191+
153192
describe "eager loading" do
154193
before(:all) do
155194
schema_define do

0 commit comments

Comments
 (0)