Skip to content

Commit 57855c9

Browse files
johnnyshieldspcomandeo-mongo
committed
MONGOID-5144 Add a deprecation warning when using the legacy query cache (#5042)
* MONGOID-5144 Add a deprecation warning when using the legacy query cache. * fix the feature/test with old drivers * Update query_cache_spec.rb * account for instance variable removal failing better * Minor improvement in a spec Co-authored-by: shields <[email protected]> Co-authored-by: Oleg Pudeyev <[email protected]> Co-authored-by: Dmitry Rybakov <[email protected]>
1 parent b7c0eb8 commit 57855c9

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/mongoid/query_cache.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@ module Mongoid
77
#
88
# @since 4.0.0
99
module QueryCache
10-
class << self
10+
# @api private
11+
LEGACY_WARNING = <<~DOC
12+
You are using the legacy Mongoid query cache which has known issues.
13+
Please upgrade the `mongo' gem to at least 2.14.0 to use the improved driver query cache.
14+
Refer to: https://docs.mongodb.com/mongoid/current/tutorials/mongoid-queries/#the-improved-driver-query-cache
15+
DOC
1116

17+
class << self
1218
# Get the cached queries.
1319
#
1420
# @example Get the cached queries from the current thread.
@@ -86,6 +92,10 @@ def cache(&block)
8692
if defined?(Mongo::QueryCache)
8793
Mongo::QueryCache.cache(&block)
8894
else
95+
@legacy_query_cache_warned ||= begin
96+
Mongoid.logger.warn(LEGACY_WARNING)
97+
true
98+
end
8999
enabled = QueryCache.enabled?
90100
QueryCache.enabled = true
91101
begin

spec/mongoid/query_cache_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,27 @@
2424
SessionRegistry.instance.verify_sessions_ended!
2525
end
2626

27+
let(:reset_legacy_qc_warning) do
28+
begin
29+
Mongoid::QueryCache.remove_instance_variable('@legacy_query_cache_warned')
30+
rescue NameError
31+
# raised if the instance variable wasn't set
32+
end
33+
end
34+
2735
describe '#cache' do
2836
context 'with driver query cache' do
2937
min_driver_version '2.14'
3038

39+
it 'does not log a deprecation warning' do
40+
reset_legacy_qc_warning
41+
42+
expect_any_instance_of(Logger).to_not receive(:warn).with(
43+
described_class::LEGACY_WARNING
44+
)
45+
described_class.cache { }
46+
end
47+
3148
context 'when query cache is not enabled' do
3249
before do
3350
Mongoid::QueryCache.enabled = false
@@ -180,6 +197,13 @@
180197
context 'with mongoid query cache' do
181198
max_driver_version '2.13'
182199

200+
it 'logs a deprecation warning' do
201+
reset_legacy_qc_warning
202+
203+
expect_any_instance_of(Logger).to receive(:warn).with(described_class::LEGACY_WARNING)
204+
described_class.cache { }
205+
end
206+
183207
context 'when query cache is not enabled' do
184208
before do
185209
Mongoid::QueryCache.enabled = false

0 commit comments

Comments
 (0)