From 242c7e8b6602f4e6e5e73ea2d6f23363ece32bd4 Mon Sep 17 00:00:00 2001 From: Yasuo Honda Date: Thu, 19 Nov 2020 18:38:53 +0900 Subject: [PATCH] Merge pull request #2070 from koic/build_subselect_doesnt_have_ordering `build_subselect` does not have ordering --- lib/arel/visitors/oracle.rb | 11 ++++++ lib/arel/visitors/oracle12.rb | 11 ++++++ .../oracle_enhanced_adapter_spec.rb | 39 +++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/lib/arel/visitors/oracle.rb b/lib/arel/visitors/oracle.rb index 573ec8240..7a5b1179a 100644 --- a/lib/arel/visitors/oracle.rb +++ b/lib/arel/visitors/oracle.rb @@ -248,6 +248,17 @@ def is_distinct_from(o, collector) collector = visit [o.left, o.right, 0, 1], collector collector << ")" end + + # Oracle will occur an error `ORA-00907: missing right parenthesis` + # when using `ORDER BY` in `UPDATE` or `DELETE`'s subquery. + # + # This method has been overridden based on the following code. + # https://github.com/rails/rails/blob/v6.1.0.rc1/activerecord/lib/arel/visitors/to_sql.rb#L815-L825 + def build_subselect(key, o) + stmt = super + stmt.orders = [] # `orders` will never be set to prevent `ORA-00907`. + stmt + end end end end diff --git a/lib/arel/visitors/oracle12.rb b/lib/arel/visitors/oracle12.rb index 55c96610a..443772372 100644 --- a/lib/arel/visitors/oracle12.rb +++ b/lib/arel/visitors/oracle12.rb @@ -155,6 +155,17 @@ def is_distinct_from(o, collector) collector = visit [o.left, o.right, 0, 1], collector collector << ")" end + + # Oracle will occur an error `ORA-00907: missing right parenthesis` + # when using `ORDER BY` in `UPDATE` or `DELETE`'s subquery. + # + # This method has been overridden based on the following code. + # https://github.com/rails/rails/blob/v6.1.0.rc1/activerecord/lib/arel/visitors/to_sql.rb#L815-L825 + def build_subselect(key, o) + stmt = super + stmt.orders = [] # `orders` will never be set to prevent `ORA-00907`. + stmt + end end end end diff --git a/spec/active_record/connection_adapters/oracle_enhanced_adapter_spec.rb b/spec/active_record/connection_adapters/oracle_enhanced_adapter_spec.rb index e5ddeb974..10933c05e 100644 --- a/spec/active_record/connection_adapters/oracle_enhanced_adapter_spec.rb +++ b/spec/active_record/connection_adapters/oracle_enhanced_adapter_spec.rb @@ -150,6 +150,45 @@ class ::TestEmployee2 < ActiveRecord::Base end end + describe "`has_many` assoc has `dependent: :delete_all` with `order`" do + before(:all) do + schema_define do + create_table :test_posts do |t| + t.string :title + end + create_table :test_comments do |t| + t.integer :test_post_id + t.string :description + end + add_index :test_comments, :test_post_id + end + class ::TestPost < ActiveRecord::Base + has_many :test_comments, -> { order(:id) }, dependent: :delete_all + end + class ::TestComment < ActiveRecord::Base + belongs_to :test_post + end + TestPost.transaction do + post = TestPost.create!(title: "Title") + TestComment.create!(test_post_id: post.id, description: "Description") + end + end + + after(:all) do + schema_define do + drop_table :test_comments + drop_table :test_posts + end + Object.send(:remove_const, "TestPost") + Object.send(:remove_const, "TestComment") + ActiveRecord::Base.clear_cache! + end + + it "should not occur `ActiveRecord::StatementInvalid: OCIError: ORA-00907: missing right parenthesis`" do + expect { TestPost.first.destroy }.not_to raise_error + end + end + describe "eager loading" do before(:all) do schema_define do