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

Properly handle cached queries with too many bind parameters

This commit is contained in:
Matthew Dunbar 2019-02-14 23:07:01 -05:00
parent c43c839847
commit 795c0f8205
2 changed files with 16 additions and 0 deletions

View file

@ -96,6 +96,12 @@ module ActiveRecord
if @query_cache_enabled && !locked?(arel)
arel = arel_from_relation(arel)
sql, binds = to_sql_and_binds(arel, binds)
if binds.length > bind_params_length
sql, binds = unprepared_statement { to_sql_and_binds(arel) }
preparable = false
end
cache_sql(sql, name, binds) { super(sql, name, binds, preparable: preparable) }
else
super

View file

@ -44,6 +44,16 @@ if ActiveRecord::Base.connection.prepared_statements
assert_equal 0, topics.count
end
def test_too_many_binds_with_query_cache
Topic.connection.enable_query_cache!
bind_params_length = @connection.send(:bind_params_length)
topics = Topic.where(id: (1 .. bind_params_length + 1).to_a)
assert_equal Topic.count, topics.count
topics = Topic.where.not(id: (1 .. bind_params_length + 1).to_a)
assert_equal 0, topics.count
end
def test_bind_from_join_in_subquery
subquery = Author.joins(:thinking_posts).where(name: "David")
scope = Author.from(subquery, "authors").where(id: 1)