Skip to content

Commit d2d30e5

Browse files
committed
Merge pull request rsim#2070 from koic/build_subselect_doesnt_have_ordering
`build_subselect` does not have ordering
1 parent 06b3acf commit d2d30e5

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

lib/active_record/connection_adapters/oracle_enhanced_adapter.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,3 +746,26 @@ def bind_string(name, value)
746746
module ActiveRecord
747747
autoload :OracleEnhancedProcedures, "active_record/connection_adapters/oracle_enhanced/procedures"
748748
end
749+
750+
# Backport #2070 into relese60 branch by adding some dirty monkey patch
751+
# Because #2002 has been merged to release61 branch or newer, not available for release60 branch.
752+
module Arel # :nodoc: all
753+
module Visitors
754+
class Oracle < Arel::Visitors::ToSql
755+
private
756+
def build_subselect(key, o)
757+
stmt = super
758+
stmt.orders = [] # `orders` will never be set to prevent `ORA-00907`.
759+
stmt
760+
end
761+
end
762+
class Oracle12 < Arel::Visitors::ToSql
763+
private
764+
def build_subselect(key, o)
765+
stmt = super
766+
stmt.orders = [] # `orders` will never be set to prevent `ORA-00907`.
767+
stmt
768+
end
769+
end
770+
end
771+
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)