mongoid 9.1.1 → 9.1.3

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.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/lib/config/locales/en.yml +44 -11
  3. data/lib/mongoid/association/accessors.rb +21 -4
  4. data/lib/mongoid/association/eager_load/discriminated_inclusion.rb +10 -10
  5. data/lib/mongoid/association/eager_load/embedded_distributor.rb +6 -6
  6. data/lib/mongoid/association/eager_load/lookup_pipeline.rb +1 -1
  7. data/lib/mongoid/association/eager_loadable.rb +8 -1
  8. data/lib/mongoid/association/embedded/batchable.rb +17 -1
  9. data/lib/mongoid/association/nested/many.rb +1 -1
  10. data/lib/mongoid/association/referenced/belongs_to/buildable.rb +19 -15
  11. data/lib/mongoid/association/referenced/belongs_to.rb +11 -3
  12. data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +25 -5
  13. data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +12 -0
  14. data/lib/mongoid/association/referenced/has_many/buildable.rb +17 -7
  15. data/lib/mongoid/association/referenced/has_many/proxy.rb +4 -2
  16. data/lib/mongoid/association/referenced/has_one/buildable.rb +20 -6
  17. data/lib/mongoid/association/relatable.rb +18 -0
  18. data/lib/mongoid/association.rb +23 -0
  19. data/lib/mongoid/atomic/modifiers.rb +100 -16
  20. data/lib/mongoid/attributes/readonly.rb +19 -1
  21. data/lib/mongoid/changeable.rb +19 -1
  22. data/lib/mongoid/clients/factory.rb +2 -0
  23. data/lib/mongoid/config/encryption.rb +152 -19
  24. data/lib/mongoid/contextual/none.rb +51 -0
  25. data/lib/mongoid/criteria/scopable.rb +5 -0
  26. data/lib/mongoid/criteria.rb +45 -1
  27. data/lib/mongoid/document.rb +13 -0
  28. data/lib/mongoid/encryptable.rb +43 -16
  29. data/lib/mongoid/errors/invalid_encrypted_association.rb +28 -0
  30. data/lib/mongoid/errors/invalid_encryption_schema.rb +27 -0
  31. data/lib/mongoid/errors/no_encryption_schema.rb +5 -3
  32. data/lib/mongoid/errors.rb +2 -0
  33. data/lib/mongoid/factory.rb +1 -1
  34. data/lib/mongoid/fields/encrypted.rb +14 -3
  35. data/lib/mongoid/fields/foreign_key.rb +9 -0
  36. data/lib/mongoid/fields/standard.rb +10 -0
  37. data/lib/mongoid/fields/validators/macro.rb +29 -0
  38. data/lib/mongoid/fields.rb +49 -2
  39. data/lib/mongoid/persistable/creatable.rb +120 -14
  40. data/lib/mongoid/persistable.rb +15 -15
  41. data/lib/mongoid/persistence_context.rb +46 -4
  42. data/lib/mongoid/reloadable.rb +1 -0
  43. data/lib/mongoid/search_indexable.rb +5 -1
  44. data/lib/mongoid/tasks/database.rb +10 -7
  45. data/lib/mongoid/touchable.rb +28 -1
  46. data/lib/mongoid/version.rb +1 -1
  47. data/lib/mongoid/warnings.rb +4 -0
  48. data/spec/integration/associations/embedded_delayed_atomic_sets_spec.rb +216 -0
  49. data/spec/integration/associations/has_and_belongs_to_many_spec.rb +14 -0
  50. data/spec/integration/atomic/modifiers_spec.rb +283 -0
  51. data/spec/integration/encryption_spec.rb +418 -0
  52. data/spec/mongoid/association/accessors_spec.rb +35 -0
  53. data/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb +35 -6
  54. data/spec/mongoid/association/referenced/encrypted_foreign_key_spec.rb +142 -0
  55. data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +36 -5
  56. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +48 -0
  57. data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +24 -6
  58. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +81 -0
  59. data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +18 -3
  60. data/spec/mongoid/atomic/modifiers_spec.rb +347 -0
  61. data/spec/mongoid/attributes/nested_spec.rb +29 -0
  62. data/spec/mongoid/attributes/readonly_dotted_spec.rb +334 -0
  63. data/spec/mongoid/attributes/readonly_spec.rb +389 -0
  64. data/spec/mongoid/changeable_spec.rb +18 -0
  65. data/spec/mongoid/clients/factory_spec.rb +36 -0
  66. data/spec/mongoid/config/encryption_spec.rb +450 -0
  67. data/spec/mongoid/contextual/none_spec.rb +18 -0
  68. data/spec/mongoid/criteria/includable_spec.rb +346 -0
  69. data/spec/mongoid/criteria_spec.rb +17 -0
  70. data/spec/mongoid/encryptable_spec.rb +54 -0
  71. data/spec/mongoid/factory_spec.rb +8 -0
  72. data/spec/mongoid/fields_spec.rb +37 -0
  73. data/spec/mongoid/findable_spec.rb +14 -0
  74. data/spec/mongoid/scopable_spec.rb +102 -0
  75. data/spec/mongoid/search_indexable_spec.rb +26 -0
  76. data/spec/mongoid/tasks/database_spec.rb +13 -0
  77. data/spec/mongoid/touchable_spec.rb +217 -0
  78. data/spec/mongoid/touchable_spec_models.rb +37 -0
  79. data/spec/support/crypt/models.rb +138 -0
  80. data/spec/support/models/delete_all_account.rb +10 -0
  81. data/spec/support/models/delete_all_item.rb +9 -0
  82. data/spec/support/models/delete_all_scoped_item.rb +11 -0
  83. data/spec/support/models/delete_all_tag.rb +7 -0
  84. data/spec/support/models/modifier_models.rb +65 -0
  85. metadata +2045 -2008
@@ -13,13 +13,9 @@ module Mongoid
13
13
  # @param [ Hash ] modifications The add to set modifiers.
14
14
  def add_to_set(modifications)
15
15
  modifications.each_pair do |field, value|
16
- if add_to_sets.has_key?(field)
17
- value.each do |val|
18
- add_to_sets[field]['$each'].push(val)
19
- end
20
- else
21
- add_to_sets[field] = { '$each' => value }
22
- end
16
+ mods = add_to_set_conflict?(field) ? conflicting_add_to_sets : add_to_sets
17
+ add_each_operation(mods, field, value)
18
+ add_to_set_fields[field.split('.', 2)[0]] = field
23
19
  end
24
20
  end
25
21
 
@@ -31,7 +27,8 @@ module Mongoid
31
27
  # @param [ Hash ] modifications The pull all modifiers.
32
28
  def pull_all(modifications)
33
29
  modifications.each_pair do |field, value|
34
- add_operation(pull_alls, field, value)
30
+ mods = pull_all_conflict?(field) ? conflicting_pull_alls : pull_alls
31
+ add_operation(mods, field, value)
35
32
  pull_fields[field.split('.', 2)[0]] = field
36
33
  end
37
34
  end
@@ -41,6 +38,14 @@ module Mongoid
41
38
  # @example Add pull all operations.
42
39
  # modifiers.pull({ "addresses" => { "_id" => { "$in" => [ 1, 2, 3 ]}}})
43
40
  #
41
+ # A $pull is never split out into a separate write. It shifts the
42
+ # positions of the remaining array elements, and the paths of the other
43
+ # operations on that array are built for the positions after the $pull.
44
+ # In a separate write the $pull could run after them, so they would hit
45
+ # a different element. Kept in the main update, an operation that
46
+ # conflicts with the $pull is rejected by MongoDB (code 40) without
47
+ # changing stored data.
48
+ #
44
49
  # @param [ Hash ] modifications The pull all modifiers.
45
50
  def pull(modifications)
46
51
  modifications.each_pair do |field, value|
@@ -99,6 +104,7 @@ module Mongoid
99
104
  else
100
105
  unsets.update(field => true)
101
106
  end
107
+ unset_fields[field.split('.', 2)[0]] = field
102
108
  end
103
109
  end
104
110
 
@@ -166,17 +172,28 @@ module Mongoid
166
172
  # @return [ true | false ] If this field is a conflict.
167
173
  def set_conflict?(field)
168
174
  name = field.split('.', 2)[0]
169
- pull_fields.has_key?(name) || push_fields.has_key?(name)
175
+ pull_fields.has_key?(name) || push_fields.has_key?(name) ||
176
+ add_to_set_fields.has_key?(name)
170
177
  end
171
178
 
172
179
  # Is the operation going to be a conflict for an $unset?
173
180
  #
181
+ # A $push or $addToSet does not shift existing array element positions,
182
+ # so a conflicting $unset can be safely applied in a separate write
183
+ # afterwards. A $pull does shift positions, so the $unset is not split
184
+ # out in that case; the operations stay in the same update and MongoDB
185
+ # rejects the update (code 40) without changing stored data, rather
186
+ # than risking the $unset hitting a different element.
187
+ #
174
188
  # @param [ String ] field The field.
175
189
  #
176
190
  # @return [ true | false ] If this field is a conflict.
177
191
  def unset_conflict?(field)
178
192
  key = field.split('.', 2)[0]
179
- set_fields.has_key?(key)
193
+ return true if set_fields.has_key?(key)
194
+ return false if pull_fields.has_key?(key)
195
+
196
+ push_fields.has_key?(key) || add_to_set_fields.has_key?(key)
180
197
  end
181
198
 
182
199
  # Is the operation going to be a conflict for a $push?
@@ -190,9 +207,36 @@ module Mongoid
190
207
  def push_conflict?(field)
191
208
  name = field.split('.', 2)[0]
192
209
  set_fields.has_key?(name) || pull_fields.has_key?(name) ||
210
+ add_to_set_fields.has_key?(name) || unset_fields.has_key?(name) ||
193
211
  (push_fields.keys.count { |item| item.split('.', 2).first == name } > 1)
194
212
  end
195
213
 
214
+ # Is the operation going to be a conflict for an $addToSet?
215
+ #
216
+ # As with $push, an $addToSet is only split out when the other
217
+ # operation does not shift array element positions. A conflicting
218
+ # $pull is left as-is so MongoDB rejects the update rather than the
219
+ # $addToSet possibly hitting a different element in a separate write.
220
+ #
221
+ # @param [ String ] field The field.
222
+ #
223
+ # @return [ true | false ] If this field is a conflict.
224
+ def add_to_set_conflict?(field)
225
+ name = field.split('.', 2)[0]
226
+ set_fields.has_key?(name) || push_fields.has_key?(name)
227
+ end
228
+
229
+ # Is the operation going to be a conflict for a $pullAll?
230
+ #
231
+ # @param [ String ] field The field.
232
+ #
233
+ # @return [ true | false ] If this field is a conflict.
234
+ def pull_all_conflict?(field)
235
+ name = field.split('.', 2)[0]
236
+ set_fields.has_key?(name) || push_fields.has_key?(name) ||
237
+ add_to_set_fields.has_key?(name) || pull_root?(name)
238
+ end
239
+
196
240
  # Returns true if the $unset is made redundant by a $set that covers
197
241
  # the entire root-level field. When $set "children" is issued, the
198
242
  # current (live) state of the array already includes all pending changes
@@ -202,16 +246,26 @@ module Mongoid
202
246
  # @param [ String ] field the field
203
247
  def unset_superseded_by_set?(field)
204
248
  key = field.split('.', 2).first
205
- sets(initialize: false).has_key?(key)
249
+ sets(initialize: false)&.has_key?(key)
250
+ end
251
+
252
+ # Get the conflicting $addToSet operations.
253
+ #
254
+ # @example Get the conflicting add to sets.
255
+ # modifiers.conflicting_add_to_sets
256
+ #
257
+ # @return [ Hash ] The conflicting add to set operations.
258
+ def conflicting_add_to_sets
259
+ conflicts['$addToSet'] ||= {}
206
260
  end
207
261
 
208
- # Get the conflicting pull modifications.
262
+ # Get the conflicting pull all modifications.
209
263
  #
210
- # @example Get the conflicting pulls.
211
- # modifiers.conflicting_pulls
264
+ # @example Get the conflicting pull alls.
265
+ # modifiers.conflicting_pull_alls
212
266
  #
213
- # @return [ Hash ] The conflicting pull operations.
214
- def conflicting_pulls
267
+ # @return [ Hash ] The conflicting pull all operations.
268
+ def conflicting_pull_alls
215
269
  conflicts['$pullAll'] ||= {}
216
270
  end
217
271
 
@@ -282,6 +336,26 @@ module Mongoid
282
336
  @set_fields ||= {}
283
337
  end
284
338
 
339
+ # Get the names of the fields that need to be added to sets.
340
+ #
341
+ # @example Get the add to set fields.
342
+ # modifiers.add_to_set_fields
343
+ #
344
+ # @return [ Array<String> ] The add to set fields.
345
+ def add_to_set_fields
346
+ @add_to_set_fields ||= {}
347
+ end
348
+
349
+ # Get the names of the fields that need to be unset.
350
+ #
351
+ # @example Get the unset fields.
352
+ # modifiers.unset_fields
353
+ #
354
+ # @return [ Array<String> ] The unset fields.
355
+ def unset_fields
356
+ @unset_fields ||= {}
357
+ end
358
+
285
359
  # Get the $pullAll operations or initialize a new one.
286
360
  #
287
361
  # @example Get the $pullAll operations.
@@ -333,6 +407,16 @@ module Mongoid
333
407
  def unsets
334
408
  self['$unset'] ||= {}
335
409
  end
410
+
411
+ # Is there a $pull on the given root-level field? Does not initialize
412
+ # the $pull operations.
413
+ #
414
+ # @param [ String ] name The root-level field name.
415
+ #
416
+ # @return [ true | false ] If a $pull touches this field.
417
+ def pull_root?(name)
418
+ (self['$pull'] || {}).keys.any? { |key| key.split('.', 2)[0] == name }
419
+ end
336
420
  end
337
421
  end
338
422
  end
@@ -21,7 +21,25 @@ module Mongoid
21
21
  # @return [ true | false ] If the document is new, or if the field is not
22
22
  # readonly.
23
23
  def attribute_writable?(name)
24
- new_record? || (!self.class.readonly_attributes.include?(name) && _loaded?(name))
24
+ return true if new_record?
25
+
26
+ readonly = self.class.readonly_attributes
27
+ return _loaded?(name) if readonly.empty?
28
+
29
+ path = name.to_s
30
+ return false if readonly.include?(path)
31
+
32
+ readonly.each do |field|
33
+ # A readonly attribute that is a declared field with a dotted name
34
+ # is that literal field, not a path prefix.
35
+ next if field.include?('.') && self.class.fields.key?(field)
36
+
37
+ # A path that is a declared field with a dotted name addresses that
38
+ # literal field, not a descendant of the readonly attribute.
39
+ return false if path.start_with?("#{field}.") && !self.class.fields.key?(path)
40
+ end
41
+
42
+ _loaded?(name)
25
43
  end
26
44
 
27
45
  private
@@ -80,6 +80,7 @@ module Mongoid
80
80
  @children_may_have_changed = false
81
81
  reset_atomic_updates!
82
82
  changed_attributes.clear
83
+ @pre_create_changed_attributes = nil
83
84
  end
84
85
 
85
86
  # Things that need to execute after a document has been persisted.
@@ -266,6 +267,13 @@ module Mongoid
266
267
  def attribute_changed?(attr, from: ATTRIBUTE_UNCHANGED, to: ATTRIBUTE_UNCHANGED)
267
268
  attr = database_field_name(attr)
268
269
  return false unless changed_attributes.key?(attr)
270
+ # An in-place change to a readonly attribute of a persisted document
271
+ # is not part of the dirty tracking API and will not be persisted. A
272
+ # change tracked while the document was new initializes the document,
273
+ # and new documents may set readonly attributes, so it is still
274
+ # reported as a saved change.
275
+ return false if !new_record? && self.class.readonly_attributes.include?(attr) &&
276
+ !@pre_create_changed_attributes&.include?(attr)
269
277
  return false if changed_attributes[attr] == attributes[attr]
270
278
  return false if from != changed_attributes[attr]
271
279
  return false if to != attributes[attr]
@@ -328,6 +336,13 @@ module Mongoid
328
336
  def attribute_will_change!(attr)
329
337
  return if changed_attributes.key?(attr)
330
338
 
339
+ # The snapshot is taken for readonly attributes as well. Their changes
340
+ # are hidden from the dirty tracking API (see #attribute_changed?), but
341
+ # the snapshot is needed to reset an in-place change. Changes tracked
342
+ # while the document is new initialize it, and new documents may set
343
+ # readonly attributes, so they are remembered here and still reported
344
+ # when the document is created.
345
+ (@pre_create_changed_attributes ||= ::Set.new).add(attr) if new_record?
331
346
  changed_attributes[attr] = read_raw_attribute(attr).__deep_copy__
332
347
  end
333
348
 
@@ -341,7 +356,10 @@ module Mongoid
341
356
  # @return [ Object ] The old value.
342
357
  def reset_attribute!(attr)
343
358
  attr = database_field_name(attr)
344
- attributes[attr] = changed_attributes.delete(attr) if attribute_changed?(attr)
359
+ return unless changed_attributes.key?(attr)
360
+ return if changed_attributes[attr] == attributes[attr]
361
+
362
+ attributes[attr] = changed_attributes.delete(attr)
345
363
  end
346
364
 
347
365
  def reset_attribute_to_default!(attr)
@@ -90,6 +90,8 @@ module Mongoid
90
90
  return nil unless opts[:auto_encryption_options]
91
91
 
92
92
  opts[:auto_encryption_options].dup.tap do |auto_encryption_options|
93
+ Mongoid::Warnings.warn_bypass_auto_encryption if auto_encryption_options[:bypass_auto_encryption]
94
+
93
95
  if auto_encryption_options.key?(:schema_map)
94
96
  default_logger.warn(
95
97
  'The :schema_map is configured in the :auto_encryption_options for the client;' +
@@ -20,7 +20,11 @@ module Mongoid
20
20
  #
21
21
  # @return [ Hash ] The encryption schema map.
22
22
  def encryption_schema_map(default_database, models = ::Mongoid.models)
23
- models.each_with_object({}) do |model, map|
23
+ # Subclasses share their parent's collection but are not registered in
24
+ # Mongoid.models, so they have to be added to the walk explicitly.
25
+ classes = models.flat_map { |model| [ model ] + model.descendants }.uniq
26
+
27
+ classes.each_with_object({}) do |model, map|
24
28
  next if model.embedded?
25
29
  next unless model.requires_encryption_schema?
26
30
 
@@ -34,14 +38,33 @@ module Mongoid
34
38
  next if database.respond_to?(:call)
35
39
 
36
40
  key = "#{database}.#{model.collection_name}"
37
- props = metadata_for(model).merge(properties_for(model, [ model ]))
41
+ props = metadata_for(model).merge(properties_for(model, [ model ], key))
42
+ next if props.empty?
43
+
38
44
  # The root of a collection schema describes the document, so it is
39
45
  # always an object. Saying so matters when a nested schema carries
40
46
  # encryptMetadata: mongocryptd rejects the schema otherwise.
41
- map[key] = { 'bsonType' => 'object' }.merge(props) unless props.empty?
47
+ entry = { 'bsonType' => 'object' }.merge(props)
48
+ map[key] = map.key?(key) ? merge_schema_entries(map[key], entry, key) : entry
42
49
  end
43
50
  end
44
51
 
52
+ # Collect the paths of the fields encrypted by the schema generated for
53
+ # a single model.
54
+ #
55
+ # @param [ Mongoid::Document ] model The model.
56
+ #
57
+ # @return [ Array<Array<String>> ] The property paths, outermost first.
58
+ #
59
+ # @api private
60
+ def encrypted_paths_for(model)
61
+ return model.instance_variable_get(:@encrypted_paths_for) if
62
+ model.instance_variable_defined?(:@encrypted_paths_for)
63
+
64
+ properties = properties_for(model, [ model ])['properties'] || {}
65
+ model.instance_variable_set(:@encrypted_paths_for, collect_encrypted_paths(properties, []))
66
+ end
67
+
45
68
  private
46
69
 
47
70
  # The algorithm to use for the deterministic encryption.
@@ -109,10 +132,12 @@ module Mongoid
109
132
  # @param [ Mongoid::Document ] model The model to generate the properties for.
110
133
  # @param [ Array<Mongoid::Document> ] path The models the walk is already
111
134
  # inside of, outermost first.
135
+ # @param [ String | nil ] namespace The namespace the schema is built
136
+ # for, used for error reporting.
112
137
  #
113
138
  # @return [ Hash ] The encryption properties.
114
- def properties_for(model, path)
115
- result = properties_for_fields(model).merge(properties_for_relations(model, path))
139
+ def properties_for(model, path, namespace = nil)
140
+ result = properties_for_fields(model).merge(properties_for_relations(model, path, namespace))
116
141
  if result.empty?
117
142
  {}
118
143
  else
@@ -127,7 +152,7 @@ module Mongoid
127
152
  # @return [ Hash ] The encryption properties.
128
153
  def properties_for_fields(model)
129
154
  model.fields.each_with_object({}) do |(name, field), props|
130
- next unless field.is_a?(Mongoid::Fields::Encrypted)
155
+ next unless field.encrypted?
131
156
 
132
157
  props[name] = {
133
158
  'encrypt' => {
@@ -151,10 +176,12 @@ module Mongoid
151
176
  # @param [ Mongoid::Document ] model The model to generate the properties for.
152
177
  # @param [ Array<Mongoid::Document> ] path The models the walk is already
153
178
  # inside of, outermost first.
179
+ # @param [ String | nil ] namespace The namespace the schema is built
180
+ # for, used for error reporting.
154
181
  #
155
182
  # @return [ Hash ] The encryption properties.
156
- def properties_for_relations(model, path)
157
- model.relations.each_with_object({}) do |(name, relation), props|
183
+ def properties_for_relations(model, path, namespace = nil)
184
+ model.relations.each_with_object({}) do |(_name, relation), props|
158
185
  # relation_class constantizes, and a polymorphic embedded_in has no
159
186
  # class to resolve, so the relation type has to be checked first.
160
187
  next unless relation.is_a?(Association::Embedded::EmbedsOne)
@@ -163,21 +190,127 @@ module Mongoid
163
190
  # An association target does not have to be a Mongoid document, and
164
191
  # the class it names does not have to exist.
165
192
  next unless klass.respond_to?(:requires_encryption_schema?)
166
- # Stop at a model the walk is already inside of, or a self-embedding
167
- # model never terminates. The path covers the current branch only:
168
- # a model embedded by two parents, or twice by one parent, has to be
169
- # emitted at every place it appears.
170
- next if path.include?(klass)
171
- next unless klass.requires_encryption_schema?
172
-
173
- metadata_for(klass).merge(
174
- properties_for(klass, path + [ klass ])
175
- ).tap do |properties|
176
- props[name] = { 'bsonType' => 'object' }.merge(properties) unless properties.empty?
193
+
194
+ # Any subclass of the declared class can be assigned to the
195
+ # association, so its encrypted fields have to be mapped here too.
196
+ properties = ([ klass ] + klass.descendants).uniq.inject({}) do |merged, candidate|
197
+ # Stop at a model the walk is already inside of, or a self-embedding
198
+ # model never terminates. The path covers the current branch only:
199
+ # a model embedded by two parents, or twice by one parent, has to be
200
+ # emitted at every place it appears.
201
+ next merged if path.include?(candidate)
202
+ next merged unless candidate.respond_to?(:requires_encryption_schema?)
203
+ next merged unless candidate.requires_encryption_schema?
204
+
205
+ merge_schema_entries(
206
+ merged,
207
+ metadata_for(candidate).merge(
208
+ properties_for(candidate, path + [ candidate ], namespace)
209
+ ),
210
+ namespace
211
+ )
177
212
  end
213
+ # The document is persisted under the store_as key, not under the
214
+ # name of the association, so that is the key the schema needs.
215
+ props[relation.store_as] = { 'bsonType' => 'object' }.merge(properties) unless properties.empty?
178
216
  end
179
217
  end
180
218
 
219
+ # Walk the schema properties and collect the path of every encrypted
220
+ # leaf.
221
+ #
222
+ # @param [ Hash ] properties The properties to walk.
223
+ # @param [ Array<String> ] path The properties the walk is already
224
+ # inside of.
225
+ #
226
+ # @return [ Array<Array<String>> ] The property paths.
227
+ def collect_encrypted_paths(properties, path)
228
+ properties.each_with_object([]) do |(name, schema), paths|
229
+ if schema.key?('encrypt')
230
+ paths << (path + [ name ])
231
+ elsif schema['properties']
232
+ paths.concat(collect_encrypted_paths(schema['properties'], path + [ name ]))
233
+ end
234
+ end
235
+ end
236
+
237
+ # Merge two schema objects covering the same model or namespace.
238
+ #
239
+ # Every model writing to one namespace contributes an entry to the
240
+ # schema map. Those entries have to agree on everything they have in
241
+ # common, or the schema would silently encrypt a field differently for
242
+ # one model than for another.
243
+ #
244
+ # @param [ Hash ] left The first schema.
245
+ # @param [ Hash ] right The second schema.
246
+ # @param [ String | nil ] namespace The namespace the schema belongs to.
247
+ #
248
+ # @return [ Hash ] The merged schema.
249
+ def merge_schema_entries(left, right, namespace = nil)
250
+ left.merge(right) do |key, left_value, right_value|
251
+ case key
252
+ when 'bsonType'
253
+ left_value
254
+ when 'encryptMetadata'
255
+ merge_encrypt_metadata(left_value, right_value, namespace)
256
+ when 'properties'
257
+ merge_properties(left_value, right_value, namespace)
258
+ else
259
+ raise_invalid_encryption_schema(namespace, key)
260
+ end
261
+ end
262
+ end
263
+
264
+ # Merge two encryptMetadata objects.
265
+ #
266
+ # @param [ Hash ] left The first encryptMetadata object.
267
+ # @param [ Hash ] right The second encryptMetadata object.
268
+ # @param [ String | nil ] namespace The namespace the schema belongs to.
269
+ #
270
+ # @return [ Hash ] The merged encryptMetadata object.
271
+ def merge_encrypt_metadata(left, right, namespace)
272
+ left.merge(right) do |key, left_value, right_value|
273
+ unless left_value == right_value
274
+ raise_invalid_encryption_schema(namespace, "encryptMetadata.#{key}")
275
+ end
276
+
277
+ left_value
278
+ end
279
+ end
280
+
281
+ # Merge two properties objects.
282
+ #
283
+ # @param [ Hash ] left The first properties object.
284
+ # @param [ Hash ] right The second properties object.
285
+ # @param [ String | nil ] namespace The namespace the schema belongs to.
286
+ #
287
+ # @return [ Hash ] The merged properties object.
288
+ def merge_properties(left, right, namespace)
289
+ left.merge(right) do |name, left_value, right_value|
290
+ if left_value.key?('encrypt') && right_value.key?('encrypt')
291
+ unless left_value['encrypt'] == right_value['encrypt']
292
+ raise_invalid_encryption_schema(namespace, "properties.#{name}")
293
+ end
294
+
295
+ left_value
296
+ elsif left_value.key?('encrypt') || right_value.key?('encrypt')
297
+ raise_invalid_encryption_schema(namespace, "properties.#{name}")
298
+ else
299
+ merge_schema_entries(left_value, right_value, namespace)
300
+ end
301
+ end
302
+ end
303
+
304
+ # Raise an error for a schema conflicting with another one.
305
+ #
306
+ # @param [ String | nil ] namespace The namespace the schema belongs to.
307
+ # @param [ String ] path The conflicting path in the schema.
308
+ #
309
+ # @raise [ Errors::InvalidEncryptionSchema ] Always raised.
310
+ def raise_invalid_encryption_schema(namespace, path)
311
+ raise Errors::InvalidEncryptionSchema.new(namespace, path)
312
+ end
313
+
181
314
  # Get the BSON type identifier for the provided field according to the
182
315
  # https://www.mongodb.com/docs/manual/reference/bson-types/#std-label-bson-types
183
316
  #
@@ -26,6 +26,31 @@ module Mongoid
26
26
  other.is_a?(None)
27
27
  end
28
28
 
29
+ # Delete all documents in the database that match the selector. There
30
+ # are no documents in the null context, so nothing is deleted.
31
+ #
32
+ # @example Delete all the documents.
33
+ # context.delete
34
+ #
35
+ # @return [ Integer ] Always zero.
36
+ def delete
37
+ 0
38
+ end
39
+ alias delete_all delete
40
+
41
+ # Destroy all documents in the database that match the selector. There
42
+ # are no documents in the null context, so nothing is destroyed and no
43
+ # callbacks are run.
44
+ #
45
+ # @example Destroy all the documents.
46
+ # context.destroy
47
+ #
48
+ # @return [ Integer ] Always zero.
49
+ def destroy
50
+ 0
51
+ end
52
+ alias destroy_all destroy
53
+
29
54
  # Get the distinct field values in null context.
30
55
  #
31
56
  # @example Get the distinct values in null context.
@@ -111,6 +136,32 @@ module Mongoid
111
136
  {}
112
137
  end
113
138
 
139
+ # Update the first matching document atomically. There are no documents
140
+ # in the null context, so nothing is updated.
141
+ #
142
+ # @example Update the matching document.
143
+ # context.update(name: "Smiths")
144
+ #
145
+ # @param [ Hash ] attributes The new attributes for the document.
146
+ #
147
+ # @return [ nil | false ] False if no attributes were provided.
148
+ def update(attributes = nil, _opts = {})
149
+ attributes ? nil : false
150
+ end
151
+
152
+ # Update all the matching documents atomically. There are no documents
153
+ # in the null context, so nothing is updated.
154
+ #
155
+ # @example Update all the matching documents.
156
+ # context.update_all(name: "Smiths")
157
+ #
158
+ # @param [ Hash ] attributes The new attributes for each document.
159
+ #
160
+ # @return [ nil | false ] False if no attributes were provided.
161
+ def update_all(attributes = nil, _opts = {})
162
+ attributes ? nil : false
163
+ end
164
+
114
165
  # Create the new null context.
115
166
  #
116
167
  # @example Create the new context.
@@ -13,9 +13,13 @@ module Mongoid
13
13
  #
14
14
  # @return [ Criteria ] The criteria.
15
15
  def apply_default_scope
16
+ explicit_none = none_state == true
16
17
  klass.without_default_scope do
17
18
  merge!(klass.default_scoping.call)
18
19
  end
20
+ # A none coming from the default scope is not an explicit none, so it
21
+ # may be removed by unscoping.
22
+ @none = :default if !explicit_none && @none
19
23
  self.scoping_options = true, false
20
24
  end
21
25
 
@@ -100,6 +104,7 @@ module Mongoid
100
104
  crit.scoping_options = false, true
101
105
  crit.selector.clear
102
106
  crit.options.clear
107
+ crit.send(:clear_default_scope_none)
103
108
  end
104
109
  crit
105
110
  end
@@ -371,6 +371,20 @@ module Mongoid
371
371
  self.inclusions = (inclusions + other.inclusions).uniq
372
372
  self._raw_results = _raw_results || other._raw_results
373
373
  @use_lookup ||= other.use_lookup?
374
+ if other.empty_and_chainable?
375
+ # An explicit none wins over a none that only came from a default
376
+ # scope. Association proxies forward is_a? to their target, so the
377
+ # runtime class is checked instead; anything that is not a criteria is
378
+ # treated as an explicit none.
379
+ explicit = if Criteria === other # rubocop:disable Style/CaseEquality
380
+ other.send(:none_state) == true
381
+ else
382
+ true
383
+ end
384
+ @none = true if @none == true || explicit
385
+ @none ||= :default
386
+ @context = nil
387
+ end
374
388
  self
375
389
  end
376
390
 
@@ -382,7 +396,9 @@ module Mongoid
382
396
  #
383
397
  # @return [ Criteria ] The none criteria.
384
398
  def none
385
- @none = true and self
399
+ @none = true
400
+ @context = nil
401
+ self
386
402
  end
387
403
 
388
404
  # Is the criteria an empty but chainable criteria?
@@ -563,6 +579,34 @@ module Mongoid
563
579
 
564
580
  private
565
581
 
582
+ # The kind of none on this criteria, if any. An explicit none is +true+,
583
+ # a none coming only from the default scope is +:default+, and a criteria
584
+ # that is not none returns +nil+.
585
+ #
586
+ # @api private
587
+ #
588
+ # @example Get the none state.
589
+ # criteria.send(:none_state)
590
+ #
591
+ # @return [ true | :default | nil ] The none state.
592
+ def none_state
593
+ @none
594
+ end
595
+
596
+ # Removes the none flag if it was set only by the default scope. An
597
+ # explicitly requested none is never removed.
598
+ #
599
+ # @api private
600
+ #
601
+ # @example Clear a default scope none.
602
+ # criteria.send(:clear_default_scope_none)
603
+ #
604
+ # @return [ Criteria ] The criteria.
605
+ def clear_default_scope_none
606
+ @none = nil if @none == :default
607
+ self
608
+ end
609
+
566
610
  # Are documents in the query missing, and are we configured to raise an
567
611
  # error?
568
612
  #