Skip to content

Commit 17c9a09

Browse files
committed
Merge remote-tracking branch 'remotes/origin/master' into MONGOID-5136-super-touch
2 parents cbb2b5c + 9e28a6f commit 17c9a09

File tree

7 files changed

+42
-28
lines changed

7 files changed

+42
-28
lines changed

docs/reference/compatibility.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ specified Mongoid versions.
3131
:class: compatibility-large no-padding
3232

3333
* - Mongoid
34-
- Driver 2.14-2.10
34+
- Driver 2.15-2.10
3535
- Driver 2.9-2.7
3636

3737
* - 7.3

lib/mongoid/criteria/queryable/selectable.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ def not(*criteria)
552552
end
553553
_mongoid_expand_keys(new_s).each do |k, v|
554554
k = k.to_s
555-
if c.selector[k] || k[0] == ?$
555+
if c.selector[k] || k.start_with?('$')
556556
c = c.send(:__multi__, [{'$nor' => [{k => v}]}], '$and')
557557
else
558558
if v.is_a?(Hash)
@@ -831,7 +831,7 @@ def expr_query(criterion)
831831
clone.tap do |query|
832832
normalized.each do |field, value|
833833
field_s = field.to_s
834-
if field_s[0] == ?$
834+
if field_s.start_with?('$')
835835
# Query expression-level operator, like $and or $where
836836
query.add_operator_expression(field_s, value)
837837
else

lib/mongoid/criteria/queryable/storable.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def add_field_expression(field, value)
3737
raise ArgumentError, "Field must be a string: #{field}"
3838
end
3939

40-
if field[0] == ?$
40+
if field.start_with?('$')
4141
raise ArgumentError, "Field cannot be an operator (i.e. begin with $): #{field}"
4242
end
4343

@@ -47,7 +47,7 @@ def add_field_expression(field, value)
4747
if value.is_a?(Hash) && selector[field].is_a?(Hash) &&
4848
value.keys.all? { |key|
4949
key_s = key.to_s
50-
key_s[0] == ?$ && !selector[field].key?(key_s)
50+
key_s.start_with?('$') && !selector[field].key?(key_s)
5151
}
5252
then
5353
# Multiple operators can be combined on the same field by
@@ -184,7 +184,7 @@ def add_operator_expression(operator, op_expr)
184184
raise ArgumentError, "Operator must be a string: #{operator}"
185185
end
186186

187-
unless operator[0] == ?$
187+
unless operator.start_with?('$')
188188
raise ArgumentError, "Operator must begin with $: #{operator}"
189189
end
190190

@@ -219,7 +219,7 @@ def add_one_expression(field, value)
219219
raise ArgumentError, "Field must be a string: #{field}"
220220
end
221221

222-
if field[0] == ?$
222+
if field.start_with?('$')
223223
add_operator_expression(field, value)
224224
else
225225
add_field_expression(field, value)

lib/mongoid/extensions.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
# frozen_string_literal: true
22

3-
class BSON::ObjectId
4-
def as_json(options = nil)
5-
{ "$oid" => to_s }
6-
end
7-
end
8-
93
class BSON::Document
104
# We need to override this as ActiveSupport creates a new Object, instead of a new Hash
115
# see https://github.com/rails/rails/commit/f1bad130d0c9bd77c94e43b696adca56c46a66aa

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/extensions_spec.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,6 @@
22

33
require "spec_helper"
44

5-
describe BSON::ObjectId do
6-
7-
describe "#as_json" do
8-
9-
let(:object_id) do
10-
described_class.new
11-
end
12-
13-
it "returns the $oid plus string" do
14-
expect(object_id.as_json).to eq("$oid" => object_id.to_s)
15-
end
16-
end
17-
end
18-
195
describe BSON::Document do
206

217
describe "#symbolize_keys" do

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)