1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #38172 from sinsoku/or_with_annotate

Allow relations with different SQL comments in the `or` method
This commit is contained in:
Ryuta Kamizono 2020-05-24 23:27:38 +09:00 committed by GitHub
commit 660c491e54
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 1 deletions

View file

@ -1447,7 +1447,7 @@ module ActiveRecord
end end
end end
STRUCTURAL_OR_METHODS = Relation::VALUE_METHODS - [:extending, :where, :having, :unscope, :references] STRUCTURAL_OR_METHODS = Relation::VALUE_METHODS - [:extending, :where, :having, :unscope, :references, :annotate, :optimizer_hints]
def structurally_incompatible_values_for_or(other) def structurally_incompatible_values_for_or(other)
values = other.values values = other.values
STRUCTURAL_OR_METHODS.reject do |method| STRUCTURAL_OR_METHODS.reject do |method|

View file

@ -44,5 +44,26 @@ if supports_optimizer_hints?
posts.unscope(:optimizer_hints).load posts.unscope(:optimizer_hints).load
end end
end end
def test_optimizer_hints_with_or
assert_sql(%r{\ASELECT /\*\+ NO_RANGE_OPTIMIZATION\(posts index_posts_on_author_id\) \*/}) do
Post.optimizer_hints("NO_RANGE_OPTIMIZATION(posts index_posts_on_author_id)")
.or(Post.all).load
end
queries = capture_sql do
Post.optimizer_hints("NO_RANGE_OPTIMIZATION(posts index_posts_on_author_id)")
.or(Post.optimizer_hints("NO_ICP(posts)")).load
end
assert_equal 1, queries.length
assert_includes queries.first, "NO_RANGE_OPTIMIZATION(posts index_posts_on_author_id)"
assert_not_includes queries.first, "NO_ICP(posts)"
queries = capture_sql do
Post.all.or(Post.optimizer_hints("NO_ICP(posts)")).load
end
assert_equal 1, queries.length
assert_not_includes queries.first, "NO_ICP(posts)"
end
end end
end end

View file

@ -48,5 +48,24 @@ if supports_optimizer_hints?
posts.unscope(:optimizer_hints).load posts.unscope(:optimizer_hints).load
end end
end end
def test_optimizer_hints_with_or
assert_sql(%r{\ASELECT /\*\+ SeqScan\(posts\) \*/}) do
Post.optimizer_hints("SeqScan(posts)").or(Post.all).load
end
queries = capture_sql do
Post.optimizer_hints("SeqScan(posts)").or(Post.optimizer_hints("IndexScan(posts)")).load
end
assert_equal 1, queries.length
assert_includes queries.first, "/*+ SeqScan(posts) */"
assert_not_includes queries.first, "/*+ IndexScan(posts) */"
queries = capture_sql do
Post.all.or(Post.optimizer_hints("IndexScan(posts)")).load
end
assert_equal 1, queries.length
assert_not_includes queries.first, "/*+ IndexScan(posts) */"
end
end end
end end

View file

@ -139,6 +139,14 @@ module ActiveRecord
end end
end end
def test_or_with_annotate
quoted_posts = Regexp.escape(Post.quoted_table_name)
assert_match %r{#{quoted_posts} /\* foo \*/\z}, Post.annotate("foo").or(Post.all).to_sql
assert_match %r{#{quoted_posts} /\* foo \*/\z}, Post.annotate("foo").or(Post.annotate("foo")).to_sql
assert_match %r{#{quoted_posts} /\* foo \*/\z}, Post.annotate("foo").or(Post.annotate("bar")).to_sql
assert_match %r{#{quoted_posts} /\* foo \*/ /\* bar \*/\z}, Post.annotate("foo", "bar").or(Post.annotate("foo")).to_sql
end
def test_structurally_incompatible_values def test_structurally_incompatible_values
assert_nothing_raised do assert_nothing_raised do
Post.includes(:author).includes(:author).or(Post.includes(:author)) Post.includes(:author).includes(:author).or(Post.includes(:author))