mongoid 9.0.10 → 9.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/Rakefile +31 -0
- data/lib/config/locales/en.yml +45 -0
- data/lib/mongoid/association/depending.rb +8 -4
- data/lib/mongoid/association/nested/many.rb +38 -3
- data/lib/mongoid/association/nested/nested_buildable.rb +14 -0
- data/lib/mongoid/association/referenced/auto_save.rb +48 -6
- data/lib/mongoid/association/referenced/has_many/proxy.rb +100 -1
- data/lib/mongoid/association/relatable.rb +17 -0
- data/lib/mongoid/collection_configurable.rb +8 -0
- data/lib/mongoid/config/defaults.rb +9 -5
- data/lib/mongoid/config/encryption.rb +36 -18
- data/lib/mongoid/config.rb +72 -0
- data/lib/mongoid/contextual/aggregable/memory.rb +5 -2
- data/lib/mongoid/contextual/memory.rb +18 -7
- data/lib/mongoid/criteria/queryable/mergeable.rb +4 -0
- data/lib/mongoid/criteria/queryable/selectable.rb +109 -0
- data/lib/mongoid/encryptable.rb +46 -0
- data/lib/mongoid/errors/in_memory_regexp_timeout.rb +26 -0
- data/lib/mongoid/errors/no_encryption_schema.rb +28 -0
- data/lib/mongoid/errors.rb +2 -0
- data/lib/mongoid/field_readable.rb +70 -0
- data/lib/mongoid/indexable.rb +39 -27
- data/lib/mongoid/matchable.rb +6 -1
- data/lib/mongoid/matcher/eq_impl_with_regexp.rb +4 -9
- data/lib/mongoid/matcher/regex.rb +9 -13
- data/lib/mongoid/matcher/regexp_budget.rb +383 -0
- data/lib/mongoid/matcher.rb +1 -0
- data/lib/mongoid/persistence_context.rb +35 -0
- data/lib/mongoid/search_indexable.rb +14 -7
- data/lib/mongoid/tasks/database.rb +23 -9
- data/lib/mongoid/threaded.rb +37 -0
- data/lib/mongoid/version.rb +1 -1
- data/lib/mongoid/warnings.rb +4 -0
- data/spec/integration/app_spec.rb +201 -36
- data/spec/integration/associations/has_and_belongs_to_many_spec.rb +17 -2
- data/spec/integration/dots_and_dollars_spec.rb +12 -2
- data/spec/integration/encryption_spec.rb +149 -0
- data/spec/integration/matcher_operator_data/regex.yml +21 -0
- data/spec/integration/matcher_regexp_timeout_spec.rb +215 -0
- data/spec/integration/query_operator_guard_spec.rb +89 -0
- data/spec/mongoid/association/auto_save_spec.rb +98 -0
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +48 -0
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +145 -0
- data/spec/mongoid/attributes/nested_spec.rb +250 -30
- data/spec/mongoid/config/defaults_spec.rb +18 -0
- data/spec/mongoid/config/encryption_spec.rb +228 -0
- data/spec/mongoid/contextual/aggregable/memory_spec.rb +91 -0
- data/spec/mongoid/contextual/memory_spec.rb +177 -0
- data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +2 -0
- data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +200 -0
- data/spec/mongoid/criteria_spec.rb +2 -0
- data/spec/mongoid/encryptable_spec.rb +61 -0
- data/spec/mongoid/errors/document_not_found_spec.rb +10 -3
- data/spec/mongoid/matcher/regexp_budget_spec.rb +570 -0
- data/spec/mongoid/tasks/database_spec.rb +18 -0
- data/spec/support/crypt/models.rb +157 -0
- metadata +28 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 066d78f8b736bad08756e737ff7c0e1d0af9b97149366fc0b5b0a5997c3f1a63
|
|
4
|
+
data.tar.gz: b55ea0b166bd8d9a0f465e88507ee8db2c4cccc7166477aec4b07a94d98c0a43
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9e6529d35d0fba7786a830bec9ffc87937580c414d6925c5c14f3c799ea03599592ad943783690a29e24ae09f706b6462e42ec1058bd316a023c51d7467c457e
|
|
7
|
+
data.tar.gz: 6625aaf9412d9ef3e5141b06a019bc83c33446612c3c1b16361473fb5a3ac1a006e2f4d3c6cdf0daa36c0b2b4ee2a9702b135b380011226f219517a578e203ac
|
data/README.md
CHANGED
data/Rakefile
CHANGED
|
@@ -68,6 +68,37 @@ RSpec::Core::RakeTask.new('spec:progress') do |spec|
|
|
|
68
68
|
spec.pattern = "spec/**/*_spec.rb"
|
|
69
69
|
end
|
|
70
70
|
|
|
71
|
+
RUBOCOPABLE = %w[ examples gemfiles perf lib spec mongoid.gemspec Gemfile Rakefile upload-api-docs ].freeze
|
|
72
|
+
|
|
73
|
+
desc 'Run rubocop'
|
|
74
|
+
task rubocop: %w[ rubocop:run ]
|
|
75
|
+
|
|
76
|
+
namespace :rubocop do
|
|
77
|
+
desc 'Run rubocop on the codebase'
|
|
78
|
+
task :run do
|
|
79
|
+
Bundler.with_unbundled_env do
|
|
80
|
+
sh 'bundle', 'exec', 'rubocop', *RUBOCOPABLE, verbose: false
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
desc 'Add a git pre-commit hook that runs rubocop'
|
|
85
|
+
task :install_hook do
|
|
86
|
+
hook_path = File.join('.git', 'hooks', 'pre-commit')
|
|
87
|
+
hook_script = <<~HOOK
|
|
88
|
+
#!/usr/bin/env bash
|
|
89
|
+
set -e
|
|
90
|
+
|
|
91
|
+
echo "Running rubocop..."
|
|
92
|
+
rake rubocop
|
|
93
|
+
HOOK
|
|
94
|
+
|
|
95
|
+
File.write(hook_path, hook_script)
|
|
96
|
+
FileUtils.chmod('+x', hook_path)
|
|
97
|
+
|
|
98
|
+
puts "Git pre-commit hook installed at #{hook_path}."
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
71
102
|
desc 'Build and validate the evergreen config'
|
|
72
103
|
task eg: %w[ eg:build eg:validate ]
|
|
73
104
|
|
data/lib/config/locales/en.yml
CHANGED
|
@@ -140,6 +140,32 @@ en:
|
|
|
140
140
|
A collation option is only supported if the query is executed on a MongoDB server
|
|
141
141
|
with version >= 3.4."
|
|
142
142
|
resolution: "Remove the collation option from the query."
|
|
143
|
+
in_memory_regexp_timeout:
|
|
144
|
+
message: "Evaluating this query in memory exceeded the %{limit} second
|
|
145
|
+
limit that applies to conditions containing a regular expression."
|
|
146
|
+
summary: "Mongoid evaluates some conditions in the calling thread
|
|
147
|
+
rather than sending them to the server: queries against an embedded
|
|
148
|
+
association, removing documents from an association by condition,
|
|
149
|
+
and Document#_matches?. A regular expression in such a condition
|
|
150
|
+
runs locally. Mongoid limits the total time one in-memory
|
|
151
|
+
evaluation may spend matching, because both the cost of a single
|
|
152
|
+
match and the number of matches performed grow with the condition,
|
|
153
|
+
and a pattern built from end-user input can be made to consume an
|
|
154
|
+
unbounded amount of CPU. The limit is cumulative over the whole
|
|
155
|
+
evaluation rather than per match. Where the Ruby in use offers no
|
|
156
|
+
per-Regexp timeout the limit is enforced as wall clock over the
|
|
157
|
+
whole in-memory evaluation, so there it can be exceeded by a large
|
|
158
|
+
scan even when the pattern itself is cheap."
|
|
159
|
+
resolution: "Check whether the pattern in this query comes from user
|
|
160
|
+
input; if it does, validate it before querying. If the query is
|
|
161
|
+
legitimate and simply large, raise
|
|
162
|
+
Mongoid::Config.in_memory_regexp_time_limit, or set it to nil to
|
|
163
|
+
remove the limit entirely. The limit in force is the smaller of
|
|
164
|
+
that setting and any global Regexp.timeout the application has set,
|
|
165
|
+
so check both. On JRuby, and on MRI before 3.2, there is no
|
|
166
|
+
per-Regexp timeout, so what the limit bounds there is the elapsed
|
|
167
|
+
time of the whole in-memory evaluation rather than the time spent
|
|
168
|
+
matching."
|
|
143
169
|
invalid_around_callback:
|
|
144
170
|
message: "An around callback must contain a yield in its definition."
|
|
145
171
|
summary: "The block needs to be yielded to for around callbacks to function as intended."
|
|
@@ -515,6 +541,25 @@ en:
|
|
|
515
541
|
environment, Mongoid cannot load its configuration."
|
|
516
542
|
resolution: "Please ensure an environment is set in one of the
|
|
517
543
|
listed locations. The environment must be explicitly set."
|
|
544
|
+
no_encryption_schema:
|
|
545
|
+
message: "The encryption schema of client %{client} does not cover
|
|
546
|
+
%{namespace}."
|
|
547
|
+
summary: "%{klass} declares encrypted fields, but the namespace it
|
|
548
|
+
resolved to, %{namespace}, is not part of the automatic encryption
|
|
549
|
+
schema of client %{client}. The encryption schema is built once,
|
|
550
|
+
when the client is created, and is keyed by namespace, so a
|
|
551
|
+
database name that is only known later - a callable :database
|
|
552
|
+
option, Model.with(database:), Mongoid.override_database - is not
|
|
553
|
+
in it. Routing the model to a client that has no
|
|
554
|
+
auto_encryption_options has the same effect. Mongoid refuses the
|
|
555
|
+
operation because the driver would store the declared fields in
|
|
556
|
+
plaintext without reporting an error."
|
|
557
|
+
resolution: "Give the model a namespace that is known when the
|
|
558
|
+
client is created, and a client configured with
|
|
559
|
+
auto_encryption_options. If the model has to live in a database
|
|
560
|
+
chosen at runtime, configure one client per database, each with
|
|
561
|
+
its own auto_encryption_options, and select between them with
|
|
562
|
+
Model.with(client:) instead of switching the database."
|
|
518
563
|
no_map_reduce_output:
|
|
519
564
|
message: "No output location was specified for the map/reduce
|
|
520
565
|
operation."
|
|
@@ -12,11 +12,15 @@ module Mongoid
|
|
|
12
12
|
included do
|
|
13
13
|
class_attribute :dependents
|
|
14
14
|
|
|
15
|
+
# Note the leading underscore: without it, ActiveSupport's
|
|
16
|
+
# class_attribute would generate a helper method for this attribute
|
|
17
|
+
# that collides with one it generates for :dependents.
|
|
18
|
+
#
|
|
15
19
|
# @api private
|
|
16
|
-
class_attribute :
|
|
20
|
+
class_attribute :_dependents_owner
|
|
17
21
|
|
|
18
22
|
self.dependents = []
|
|
19
|
-
self.
|
|
23
|
+
self._dependents_owner = self
|
|
20
24
|
end
|
|
21
25
|
|
|
22
26
|
class_methods do
|
|
@@ -57,9 +61,9 @@ module Mongoid
|
|
|
57
61
|
def self.define_dependency!(association)
|
|
58
62
|
validate!(association)
|
|
59
63
|
association.inverse_class.tap do |klass|
|
|
60
|
-
if klass.
|
|
64
|
+
if klass._dependents_owner != klass
|
|
61
65
|
klass.dependents = []
|
|
62
|
-
klass.
|
|
66
|
+
klass._dependents_owner = klass
|
|
63
67
|
end
|
|
64
68
|
|
|
65
69
|
if association.dependent && !klass.dependents.include?(association)
|
|
@@ -174,7 +174,7 @@ module Mongoid
|
|
|
174
174
|
# @param [ Hash ] attrs The single document attributes to process.
|
|
175
175
|
def update_nested_relation(parent, id, attrs)
|
|
176
176
|
first = existing.first
|
|
177
|
-
converted = first ?
|
|
177
|
+
converted = convert_id(first ? first.class : association.klass, id)
|
|
178
178
|
|
|
179
179
|
if existing.where(_id: converted).exists?
|
|
180
180
|
# document exists in association
|
|
@@ -184,15 +184,50 @@ module Mongoid
|
|
|
184
184
|
else
|
|
185
185
|
update_document(doc, attrs)
|
|
186
186
|
end
|
|
187
|
-
|
|
187
|
+
elsif association.embedded?
|
|
188
|
+
raise Errors::DocumentNotFound.new(association.klass, id)
|
|
189
|
+
elsif destroyable?(attrs)
|
|
190
|
+
# A destroy of a document that is not in the association is
|
|
191
|
+
# ignored, rather than reaching for it outside the association.
|
|
192
|
+
nil
|
|
193
|
+
elsif Mongoid.allow_reparenting_via_nested_attributes?
|
|
194
|
+
Mongoid::Warnings.warn_reparenting_via_nested_attributes
|
|
195
|
+
|
|
188
196
|
# push existing document to association
|
|
189
197
|
doc = association.klass.unscoped.find(converted)
|
|
198
|
+
# find returns nil instead of raising when
|
|
199
|
+
# Mongoid.raise_not_found_error is false; a missing document
|
|
200
|
+
# must still be an error here, consistent with the other
|
|
201
|
+
# not-found branches.
|
|
202
|
+
raise Errors::DocumentNotFound.new(association.klass, id) if doc.nil?
|
|
203
|
+
|
|
190
204
|
update_document(doc, attrs)
|
|
191
|
-
existing.push(doc)
|
|
205
|
+
existing.push(doc)
|
|
206
|
+
else
|
|
207
|
+
raise Errors::DocumentNotFound.new(association.klass, not_found_params(parent, id))
|
|
192
208
|
end
|
|
193
209
|
|
|
194
210
|
parent.children_may_have_changed!
|
|
195
211
|
end
|
|
212
|
+
|
|
213
|
+
# The params to report when an id in the nested attributes could not
|
|
214
|
+
# be resolved within the association.
|
|
215
|
+
#
|
|
216
|
+
# @api private
|
|
217
|
+
#
|
|
218
|
+
# @param [ Document ] parent The parent document.
|
|
219
|
+
# @param [ String | BSON::ObjectId ] id of the related document.
|
|
220
|
+
#
|
|
221
|
+
# @return [ Hash | Object ] The params for the not found error.
|
|
222
|
+
def not_found_params(parent, id)
|
|
223
|
+
if association.is_a?(Association::Referenced::HasAndBelongsToMany)
|
|
224
|
+
# A many-to-many association has no foreign key on the child, so
|
|
225
|
+
# the id is only ever resolved within the association itself.
|
|
226
|
+
id
|
|
227
|
+
else
|
|
228
|
+
{ _id: id, association.foreign_key => parent.id }
|
|
229
|
+
end
|
|
230
|
+
end
|
|
196
231
|
end
|
|
197
232
|
end
|
|
198
233
|
end
|
|
@@ -58,12 +58,26 @@ module Mongoid
|
|
|
58
58
|
# @example Convert the id.
|
|
59
59
|
# builder.convert_id(Person, "4d371b444835d98b8b000010")
|
|
60
60
|
#
|
|
61
|
+
# Ids arriving from a form are always scalars. A Hash or an Array
|
|
62
|
+
# here means the parameters were crafted, and letting one through
|
|
63
|
+
# would turn the id into a query operator, so they are rejected.
|
|
64
|
+
#
|
|
61
65
|
# @param [ Class ] klass The class we're trying to convert for.
|
|
62
66
|
# @param [ String ] id The id, usually coming from the form.
|
|
63
67
|
#
|
|
64
68
|
# @return [ BSON::ObjectId | String | Object ] The converted id.
|
|
69
|
+
#
|
|
70
|
+
# @raise [ Errors::DocumentNotFound ] if the id is not a scalar, or
|
|
71
|
+
# cannot be converted to the type the class uses for its ids.
|
|
72
|
+
# The BSON::Error rescue is defensive: a value that reaches
|
|
73
|
+
# BSON::ObjectId.mongoize and raises there must not surface as an
|
|
74
|
+
# unhandled BSON error in the caller.
|
|
65
75
|
def convert_id(klass, id)
|
|
76
|
+
raise Errors::DocumentNotFound.new(klass, id) if id.is_a?(::Hash) || id.is_a?(::Array)
|
|
77
|
+
|
|
66
78
|
klass.using_object_ids? ? BSON::ObjectId.mongoize(id) : id
|
|
79
|
+
rescue BSON::Error
|
|
80
|
+
raise Errors::DocumentNotFound.new(klass, id)
|
|
67
81
|
end
|
|
68
82
|
|
|
69
83
|
private
|
|
@@ -32,13 +32,24 @@ module Mongoid
|
|
|
32
32
|
Threaded.exit_autosave(self)
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
-
# Check if there
|
|
35
|
+
# Check if there are changes for auto-saving. Returns true if the
|
|
36
|
+
# document is new, changed, or marked for destruction, or if any
|
|
37
|
+
# in-memory referenced child with autosave: true recursively
|
|
38
|
+
# satisfies the same condition.
|
|
36
39
|
#
|
|
37
|
-
#
|
|
38
|
-
#
|
|
39
|
-
#
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
# The seen set prevents infinite recursion when autosave associations
|
|
41
|
+
# form a cycle (e.g. a belongs_to with autosave: true whose target
|
|
42
|
+
# has a has_many with autosave: true pointing back).
|
|
43
|
+
#
|
|
44
|
+
# @param [ Document ] doc The document to check.
|
|
45
|
+
# @param [ Set ] seen Documents already visited (cycle guard).
|
|
46
|
+
#
|
|
47
|
+
# @return [ true | false ] Whether the document needs autosaving.
|
|
48
|
+
def changed_for_autosave?(doc, seen = Set.new)
|
|
49
|
+
return false unless seen.add?(doc)
|
|
50
|
+
|
|
51
|
+
doc.new_record? || doc.changed? || doc.marked_for_destruction? ||
|
|
52
|
+
autosave_children_changed?(doc, seen)
|
|
42
53
|
end
|
|
43
54
|
|
|
44
55
|
# Define the autosave method on an association's owning class for
|
|
@@ -60,6 +71,8 @@ module Mongoid
|
|
|
60
71
|
__autosaving__ do
|
|
61
72
|
if assoc_value = ivar(association.name)
|
|
62
73
|
Array(assoc_value).each do |doc|
|
|
74
|
+
next unless changed_for_autosave?(doc)
|
|
75
|
+
|
|
63
76
|
pc = doc.persistence_context? ? doc.persistence_context : persistence_context.for_child(doc)
|
|
64
77
|
doc.with(pc) do |d|
|
|
65
78
|
d.save
|
|
@@ -72,6 +85,35 @@ module Mongoid
|
|
|
72
85
|
klass.after_persist_parent save_method, unless: :autosaved?
|
|
73
86
|
end
|
|
74
87
|
end
|
|
88
|
+
|
|
89
|
+
private
|
|
90
|
+
|
|
91
|
+
# Returns true if any in-memory referenced child with autosave: true
|
|
92
|
+
# needs saving.
|
|
93
|
+
#
|
|
94
|
+
# @param [ Document ] doc The document whose children to check.
|
|
95
|
+
# @param [ Set ] seen Cycle guard passed through from changed_for_autosave?.
|
|
96
|
+
#
|
|
97
|
+
# @return [ true | false ]
|
|
98
|
+
def autosave_children_changed?(doc, seen)
|
|
99
|
+
if Mongoid.autosave_saves_unchanged_documents?
|
|
100
|
+
Mongoid::Warnings.warn_autosave_saves_unchanged_documents
|
|
101
|
+
return true
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
doc.class.relations.values.select { |a| a.autosave? && !a.embedded? }.any? do |assoc|
|
|
105
|
+
(assoc_value = doc.ivar(assoc.name)) &&
|
|
106
|
+
in_memory_docs(assoc_value).any? { |child| changed_for_autosave?(child, seen) }
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Returns the in-memory documents for an association value without
|
|
111
|
+
# triggering a database load of any unloaded documents. Association
|
|
112
|
+
# proxies expose in_memory for this purpose; a plain document (which
|
|
113
|
+
# belongs_to can store directly in the ivar) is itself in-memory.
|
|
114
|
+
def in_memory_docs(assoc_value)
|
|
115
|
+
assoc_value.respond_to?(:in_memory) ? assoc_value.in_memory : [ assoc_value ]
|
|
116
|
+
end
|
|
75
117
|
end
|
|
76
118
|
end
|
|
77
119
|
end
|
|
@@ -534,12 +534,111 @@ module Mongoid
|
|
|
534
534
|
# @return [ Integer ] The number of documents deleted.
|
|
535
535
|
def remove_all(conditions = nil, method = :delete_all)
|
|
536
536
|
selector = conditions || {}
|
|
537
|
-
|
|
537
|
+
selector.merge!(criteria.selector)
|
|
538
|
+
|
|
539
|
+
# Scanning before the delete means scanning only what is already in
|
|
540
|
+
# memory, so it is only done where there is a pattern whose cost has
|
|
541
|
+
# to be bounded. Everywhere else the delete comes first and the scan
|
|
542
|
+
# sees the survivors, which is what this association has always
|
|
543
|
+
# done.
|
|
544
|
+
#
|
|
545
|
+
# The limit is read once and carried into whichever branch is taken.
|
|
546
|
+
# Letting the branch below ask again would read it a second time,
|
|
547
|
+
# across a server round trip, and a limit that had become positive
|
|
548
|
+
# in the meantime would put a deadline on the delete_if -- a query,
|
|
549
|
+
# and an unbind of every match -- which is the arrangement this
|
|
550
|
+
# branch exists to avoid.
|
|
551
|
+
limit = Matcher::RegexpBudget.limit_for(selector)
|
|
552
|
+
return remove_all_bounded(selector, method, limit) if limit
|
|
553
|
+
|
|
554
|
+
removed = klass.send(method, selector)
|
|
555
|
+
|
|
556
|
+
# No scope around this. The scan runs after the delete, so the
|
|
557
|
+
# association this loads comes back holding only what the delete did
|
|
558
|
+
# not match, and there is no pattern here to bound in any case.
|
|
559
|
+
# Opening a scope would save _matches? from deciding once per
|
|
560
|
+
# document -- 0.48us against 1.74us for the match itself -- but a
|
|
561
|
+
# scope with nothing to bound suppresses that decision for
|
|
562
|
+
# everything nested inside it, and loading the association runs
|
|
563
|
+
# find callbacks. A pattern in a selector one of those evaluates
|
|
564
|
+
# would go unguarded, which costs more than the saving is worth on a
|
|
565
|
+
# path that has just made two round trips.
|
|
538
566
|
_target.delete_if do |doc|
|
|
539
567
|
doc._matches?(selector).tap do |b|
|
|
540
568
|
unbind_one(doc) if b
|
|
541
569
|
end
|
|
542
570
|
end
|
|
571
|
+
|
|
572
|
+
removed
|
|
573
|
+
end
|
|
574
|
+
|
|
575
|
+
# Deletes all related documents matching a selector that carries a
|
|
576
|
+
# regular expression, with the whole scan under one regexp budget.
|
|
577
|
+
#
|
|
578
|
+
# The scan runs before anything is deleted, so a budget that runs out
|
|
579
|
+
# leaves both the database and the association untouched rather than
|
|
580
|
+
# reporting a failure for a delete that has already happened. It also
|
|
581
|
+
# means the budget is closed by the time the association is mutated,
|
|
582
|
+
# so where the budget is enforced with Timeout there is no window for
|
|
583
|
+
# the exception to land in the middle of an unbind.
|
|
584
|
+
#
|
|
585
|
+
# @param [ Hash ] selector The selector to delete with.
|
|
586
|
+
# @param [ Symbol ] method The deletion method to call.
|
|
587
|
+
# @param [ Float ] limit The seconds the scan may spend, as read by
|
|
588
|
+
# the caller when it chose this path.
|
|
589
|
+
#
|
|
590
|
+
# @return [ Integer ] The number of documents deleted.
|
|
591
|
+
def remove_all_bounded(selector, method, limit)
|
|
592
|
+
# Only what the association already holds. Iterating it instead
|
|
593
|
+
# would load the whole association before the delete, and for a
|
|
594
|
+
# broad pattern that is every document the association has: /.*/ is
|
|
595
|
+
# both the cheapest pattern an attacker can supply and the one that
|
|
596
|
+
# matches everything, so bounding the cost of the matching would
|
|
597
|
+
# have been paid for with an unbounded amount of memory. Nothing is
|
|
598
|
+
# missed by not loading, because the server evaluates the same
|
|
599
|
+
# selector for the delete itself.
|
|
600
|
+
#
|
|
601
|
+
# It also means the scan runs no query, so no deadline of ours can
|
|
602
|
+
# land on one. Where the budget is enforced with Timeout, covering
|
|
603
|
+
# a query would report a slow network as a regexp timeout and could
|
|
604
|
+
# deliver the asynchronous exception inside the driver's socket
|
|
605
|
+
# read.
|
|
606
|
+
documents = _target.in_memory
|
|
607
|
+
|
|
608
|
+
matching = Matcher::RegexpBudget.open_with(limit) do
|
|
609
|
+
documents.select { |doc| doc._matches?(selector) }
|
|
610
|
+
end
|
|
611
|
+
|
|
612
|
+
removed = klass.send(method, selector)
|
|
613
|
+
|
|
614
|
+
# The ids are already known to be in memory, so the loaded and added
|
|
615
|
+
# documents can be dropped directly. Enumerable#delete would look
|
|
616
|
+
# each one up by id and stop at the first hash that has it, which
|
|
617
|
+
# removes the wrong instance when both hashes hold one for the same
|
|
618
|
+
# id. Enumerable#delete_if drops it from both, as this does, but
|
|
619
|
+
# only after load_all!, which is the load this scan exists to avoid.
|
|
620
|
+
#
|
|
621
|
+
# Which documents get unbound has always depended on which ones
|
|
622
|
+
# happened to be in memory, and scanning only what is in memory
|
|
623
|
+
# keeps that. It matters for has_and_belongs_to_many, whose
|
|
624
|
+
# unbind_one pulls the id out of the foreign key array on _base and
|
|
625
|
+
# marks it dirty; the scan used to run after the delete, against an
|
|
626
|
+
# association that a cold proxy had just reloaded as holding only
|
|
627
|
+
# the survivors, so nothing matched and nothing was unbound.
|
|
628
|
+
#
|
|
629
|
+
# TODO: unbinding should not depend on what was in memory. Every
|
|
630
|
+
# matched document leaves the association, so every one of them
|
|
631
|
+
# should be unbound, which would also stop _base's foreign key
|
|
632
|
+
# array from keeping ids that no longer resolve. That is a
|
|
633
|
+
# behaviour change for has_and_belongs_to_many and belongs in a
|
|
634
|
+
# ticket of its own, with a release note; it should not ride along
|
|
635
|
+
# on this one.
|
|
636
|
+
matching.each do |doc|
|
|
637
|
+
unbind_one(doc)
|
|
638
|
+
_target._loaded.delete(doc._id)
|
|
639
|
+
_target._added.delete(doc._id)
|
|
640
|
+
end
|
|
641
|
+
|
|
543
642
|
removed
|
|
544
643
|
end
|
|
545
644
|
|
|
@@ -172,6 +172,23 @@ module Mongoid
|
|
|
172
172
|
end
|
|
173
173
|
alias :klass :relation_class
|
|
174
174
|
|
|
175
|
+
# The class of the association target, or nil when the named class is
|
|
176
|
+
# not defined.
|
|
177
|
+
#
|
|
178
|
+
# An association may name a class that never gets defined. The
|
|
179
|
+
# association is then unusable, but its owner still has to be. Callers
|
|
180
|
+
# that walk every association of every model, rather than following the
|
|
181
|
+
# one the application asked for, use this instead of relation_class.
|
|
182
|
+
#
|
|
183
|
+
# @return [ Class | nil ] The association objects' class.
|
|
184
|
+
#
|
|
185
|
+
# @api private
|
|
186
|
+
def try_relation_class
|
|
187
|
+
relation_class
|
|
188
|
+
rescue NameError
|
|
189
|
+
nil
|
|
190
|
+
end
|
|
191
|
+
|
|
175
192
|
# The class name of the object owning this association.
|
|
176
193
|
#
|
|
177
194
|
# @return [ String ] The owning objects' class name.
|
|
@@ -22,6 +22,14 @@ module Mongoid
|
|
|
22
22
|
# @raise [ Errors::CreateCollectionFailure ] If collection creation failed.
|
|
23
23
|
# @raise [ Errors::DropCollectionFailure ] If an attempt to drop collection failed.
|
|
24
24
|
def create_collection(force: false)
|
|
25
|
+
Threaded.with_collection_management do
|
|
26
|
+
perform_create_collection(force: force)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def perform_create_collection(force:)
|
|
25
33
|
if collection_name.empty?
|
|
26
34
|
# This is most probably an anonymous class, we ignore them.
|
|
27
35
|
return
|
|
@@ -18,19 +18,23 @@ module Mongoid
|
|
|
18
18
|
case version.to_s
|
|
19
19
|
when /^[0-7]\./
|
|
20
20
|
raise ArgumentError, "Version no longer supported: #{version}"
|
|
21
|
-
|
|
21
|
+
|
|
22
|
+
when '8.0'
|
|
22
23
|
self.legacy_readonly = true
|
|
23
24
|
|
|
24
|
-
load_defaults
|
|
25
|
-
|
|
25
|
+
load_defaults '8.1'
|
|
26
|
+
|
|
27
|
+
when '8.1'
|
|
26
28
|
self.immutable_ids = false
|
|
27
29
|
self.legacy_persistence_context_behavior = true
|
|
28
30
|
self.around_callbacks_for_embeds = true
|
|
29
31
|
self.prevent_multiple_calls_of_embedded_callbacks = false
|
|
30
32
|
|
|
31
|
-
load_defaults
|
|
32
|
-
|
|
33
|
+
load_defaults '9.0'
|
|
34
|
+
|
|
35
|
+
when '9.0'
|
|
33
36
|
# All flag defaults currently reflect 9.0 behavior.
|
|
37
|
+
|
|
34
38
|
else
|
|
35
39
|
raise ArgumentError, "Unknown version: #{version}"
|
|
36
40
|
end
|
|
@@ -21,17 +21,25 @@ module Mongoid
|
|
|
21
21
|
#
|
|
22
22
|
# @return [ Hash ] The encryption schema map.
|
|
23
23
|
def encryption_schema_map(default_database, models = ::Mongoid.models)
|
|
24
|
-
visited = Set.new
|
|
25
24
|
models.each_with_object({}) do |model, map|
|
|
26
|
-
next if visited.include?(model)
|
|
27
|
-
visited << model
|
|
28
25
|
next if model.embedded?
|
|
29
|
-
next unless model.
|
|
26
|
+
next unless model.requires_encryption_schema?
|
|
30
27
|
|
|
31
28
|
database = model.storage_options.fetch(:database) { default_database }
|
|
29
|
+
# A callable database name cannot be resolved here: the documented
|
|
30
|
+
# multi-tenant idiom has no correct value while the client is being
|
|
31
|
+
# built. Interpolating the callable would produce a key that never
|
|
32
|
+
# matches any namespace, so leave the model out of the map. Writes
|
|
33
|
+
# are refused later, by PersistenceContext, rather than silently
|
|
34
|
+
# going out unencrypted.
|
|
35
|
+
next if database.respond_to?(:call)
|
|
36
|
+
|
|
32
37
|
key = "#{database}.#{model.collection_name}"
|
|
33
|
-
props = metadata_for(model).merge(properties_for(model,
|
|
34
|
-
|
|
38
|
+
props = metadata_for(model).merge(properties_for(model, [ model ]))
|
|
39
|
+
# The root of a collection schema describes the document, so it is
|
|
40
|
+
# always an object. Saying so matters when a nested schema carries
|
|
41
|
+
# encryptMetadata: mongocryptd rejects the schema otherwise.
|
|
42
|
+
map[key] = { 'bsonType' => 'object' }.merge(props) unless props.empty?
|
|
35
43
|
end
|
|
36
44
|
end
|
|
37
45
|
|
|
@@ -100,11 +108,12 @@ module Mongoid
|
|
|
100
108
|
# are marked as encrypted.
|
|
101
109
|
#
|
|
102
110
|
# @param [ Mongoid::Document ] model The model to generate the properties for.
|
|
103
|
-
# @param [
|
|
111
|
+
# @param [ Array<Mongoid::Document> ] path The models the walk is already
|
|
112
|
+
# inside of, outermost first.
|
|
104
113
|
#
|
|
105
114
|
# @return [ Hash ] The encryption properties.
|
|
106
|
-
def properties_for(model,
|
|
107
|
-
result = properties_for_fields(model).merge(properties_for_relations(model,
|
|
115
|
+
def properties_for(model, path)
|
|
116
|
+
result = properties_for_fields(model).merge(properties_for_relations(model, path))
|
|
108
117
|
if result.empty?
|
|
109
118
|
{}
|
|
110
119
|
else
|
|
@@ -141,20 +150,29 @@ module Mongoid
|
|
|
141
150
|
# are configured to be encrypted.
|
|
142
151
|
#
|
|
143
152
|
# @param [ Mongoid::Document ] model The model to generate the properties for.
|
|
144
|
-
# @param [
|
|
153
|
+
# @param [ Array<Mongoid::Document> ] path The models the walk is already
|
|
154
|
+
# inside of, outermost first.
|
|
145
155
|
#
|
|
146
156
|
# @return [ Hash ] The encryption properties.
|
|
147
|
-
def properties_for_relations(model,
|
|
157
|
+
def properties_for_relations(model, path)
|
|
148
158
|
model.relations.each_with_object({}) do |(name, relation), props|
|
|
149
|
-
|
|
159
|
+
# relation_class constantizes, and a polymorphic embedded_in has no
|
|
160
|
+
# class to resolve, so the relation type has to be checked first.
|
|
150
161
|
next unless relation.is_a?(Association::Embedded::EmbedsOne)
|
|
151
|
-
next unless relation.relation_class.encrypted?
|
|
152
162
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
163
|
+
klass = relation.try_relation_class
|
|
164
|
+
# An association target does not have to be a Mongoid document, and
|
|
165
|
+
# the class it names does not have to exist.
|
|
166
|
+
next unless klass.respond_to?(:requires_encryption_schema?)
|
|
167
|
+
# Stop at a model the walk is already inside of, or a self-embedding
|
|
168
|
+
# model never terminates. The path covers the current branch only:
|
|
169
|
+
# a model embedded by two parents, or twice by one parent, has to be
|
|
170
|
+
# emitted at every place it appears.
|
|
171
|
+
next if path.include?(klass)
|
|
172
|
+
next unless klass.requires_encryption_schema?
|
|
173
|
+
|
|
174
|
+
metadata_for(klass).merge(
|
|
175
|
+
properties_for(klass, path + [ klass ])
|
|
158
176
|
).tap do |properties|
|
|
159
177
|
props[name] = { 'bsonType' => 'object' }.merge(properties) unless properties.empty?
|
|
160
178
|
end
|