Skip to content

Commit ee554b1

Browse files
johnnyshieldspcomandeo-mongo
authored
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 48b9694 commit ee554b1

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
@@ -4,8 +4,14 @@ module Mongoid
44

55
# A cache of database queries on a per-request basis.
66
module QueryCache
7-
class << self
7+
# @api private
8+
LEGACY_WARNING = <<~DOC
9+
You are using the legacy Mongoid query cache which has known issues.
10+
Please upgrade the `mongo' gem to at least 2.14.0 to use the improved driver query cache.
11+
Refer to: https://docs.mongodb.com/mongoid/current/tutorials/mongoid-queries/#the-improved-driver-query-cache
12+
DOC
813

14+
class << self
915
# Get the cached queries.
1016
#
1117
# @example Get the cached queries from the current thread.
@@ -73,6 +79,10 @@ def cache(&block)
7379
if defined?(Mongo::QueryCache)
7480
Mongo::QueryCache.cache(&block)
7581
else
82+
@legacy_query_cache_warned ||= begin
83+
Mongoid.logger.warn(LEGACY_WARNING)
84+
true
85+
end
7686
enabled = QueryCache.enabled?
7787
QueryCache.enabled = true
7888
begin

spec/mongoid/query_cache_spec.rb

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

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

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

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

0 commit comments

Comments
 (0)