Skip to content

Commit 6c0f897

Browse files
authored
Merge pull request rails#50410 from fatkodima/fix-async-queries-with-query-cache
Fix async queries to work with query cache
2 parents bf725a7 + f68727d commit 6c0f897

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ def select_all(arel, name = nil, binds = [], preparable: nil, async: false) # :n
106106
sql, binds, preparable = to_sql_and_binds(arel, binds, preparable)
107107

108108
if async
109-
lookup_sql_cache(sql, name, binds) || super(sql, name, binds, preparable: preparable, async: async)
109+
result = lookup_sql_cache(sql, name, binds) || super(sql, name, binds, preparable: preparable, async: async)
110+
FutureResult::Complete.new(result)
110111
else
111112
cache_sql(sql, name, binds) { super(sql, name, binds, preparable: preparable, async: async) }
112113
end

activerecord/test/cases/asynchronous_queries_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_async_query_cache
4848

4949
@connection.select_all "SELECT * FROM posts"
5050
result = @connection.select_all "SELECT * FROM posts", async: true
51-
assert_equal ActiveRecord::Result, result.class
51+
assert_equal ActiveRecord::FutureResult::Complete, result.class
5252
ensure
5353
ActiveRecord::Base.asynchronous_queries_tracker.finalize_session
5454
@connection.disable_query_cache!

activerecord/test/cases/calculations_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,13 @@ def test_calculation_with_polymorphic_relation
12371237
assert_async_equal part.id, ShipPart.joins(:trinkets).async_sum(:id)
12381238
end
12391239

1240+
def test_calculation_with_query_cache
1241+
ShipPart.cache do
1242+
count = ShipPart.count
1243+
assert_async_equal count, ShipPart.async_count
1244+
end
1245+
end
1246+
12401247
def test_pluck_joined_with_polymorphic_relation
12411248
part = ShipPart.create!(name: "has trinket")
12421249
part.trinkets.create!

0 commit comments

Comments
 (0)