mongoid 9.0.12 → 9.0.14

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 (58) hide show
  1. checksums.yaml +4 -4
  2. data/lib/config/locales/en.yml +44 -11
  3. data/lib/mongoid/association/embedded/batchable.rb +8 -0
  4. data/lib/mongoid/association/referenced/belongs_to.rb +16 -8
  5. data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +12 -0
  6. data/lib/mongoid/association/referenced/has_many/proxy.rb +4 -2
  7. data/lib/mongoid/attributes/readonly.rb +19 -1
  8. data/lib/mongoid/changeable.rb +21 -1
  9. data/lib/mongoid/clients/factory.rb +2 -0
  10. data/lib/mongoid/config/encryption.rb +152 -19
  11. data/lib/mongoid/contextual/none.rb +51 -0
  12. data/lib/mongoid/criteria/scopable.rb +7 -1
  13. data/lib/mongoid/criteria.rb +45 -1
  14. data/lib/mongoid/document.rb +10 -0
  15. data/lib/mongoid/encryptable.rb +43 -16
  16. data/lib/mongoid/errors/invalid_encrypted_association.rb +28 -0
  17. data/lib/mongoid/errors/invalid_encryption_schema.rb +27 -0
  18. data/lib/mongoid/errors/no_encryption_schema.rb +5 -3
  19. data/lib/mongoid/errors.rb +2 -0
  20. data/lib/mongoid/fields/encrypted.rb +14 -3
  21. data/lib/mongoid/fields/foreign_key.rb +9 -0
  22. data/lib/mongoid/fields/standard.rb +10 -0
  23. data/lib/mongoid/fields/validators/macro.rb +29 -0
  24. data/lib/mongoid/fields.rb +42 -2
  25. data/lib/mongoid/persistence_context.rb +46 -4
  26. data/lib/mongoid/reloadable.rb +1 -0
  27. data/lib/mongoid/search_indexable.rb +5 -1
  28. data/lib/mongoid/tasks/database.rb +10 -7
  29. data/lib/mongoid/threaded.rb +1 -1
  30. data/lib/mongoid/version.rb +1 -1
  31. data/lib/mongoid/warnings.rb +4 -0
  32. data/spec/integration/atomic/modifiers_spec.rb +57 -0
  33. data/spec/integration/encryption_spec.rb +418 -0
  34. data/spec/mongoid/association/referenced/encrypted_foreign_key_spec.rb +144 -0
  35. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +48 -0
  36. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +83 -0
  37. data/spec/mongoid/attributes/readonly_dotted_spec.rb +332 -0
  38. data/spec/mongoid/attributes/readonly_spec.rb +389 -0
  39. data/spec/mongoid/changeable_spec.rb +18 -0
  40. data/spec/mongoid/clients/factory_spec.rb +36 -0
  41. data/spec/mongoid/config/encryption_spec.rb +450 -0
  42. data/spec/mongoid/contextual/none_spec.rb +22 -5
  43. data/spec/mongoid/criteria_spec.rb +17 -0
  44. data/spec/mongoid/encryptable_spec.rb +54 -0
  45. data/spec/mongoid/fields_spec.rb +29 -0
  46. data/spec/mongoid/findable_spec.rb +14 -0
  47. data/spec/mongoid/matcher/regexp_budget_spec.rb +6 -0
  48. data/spec/mongoid/scopable_spec.rb +103 -2
  49. data/spec/mongoid/search_indexable_spec.rb +34 -1
  50. data/spec/mongoid/tasks/database_spec.rb +13 -0
  51. data/spec/spec_helper.rb +8 -0
  52. data/spec/support/crypt/models.rb +138 -0
  53. data/spec/support/models/delete_all_account.rb +10 -0
  54. data/spec/support/models/delete_all_item.rb +9 -0
  55. data/spec/support/models/delete_all_scoped_item.rb +11 -0
  56. data/spec/support/models/delete_all_tag.rb +7 -0
  57. data/spec/support/models/modifier_models.rb +32 -0
  58. metadata +1995 -1958
@@ -166,6 +166,12 @@ module Mongoid
166
166
  def internal_state=(state)
167
167
  self._id = state[:id]
168
168
  @changed_attributes = state[:changed_attributes]
169
+ # The document was constructed as new in #becomes, collecting
170
+ # pre-create change markers. Drop them when the copied state is that
171
+ # of a persisted document: its pending changes were all tracked after
172
+ # the document was persisted, and stale markers would let in-place
173
+ # changes to readonly attributes bypass the dirty filter.
174
+ @pre_create_changed_attributes = nil unless state[:new_record]
169
175
  @errors = ActiveModel::Errors.new(self).tap { |e| e.copy!(state[:errors]) }
170
176
  @new_record = state[:new_record]
171
177
  @destroyed = state[:destroyed]
@@ -337,6 +343,10 @@ module Mongoid
337
343
  relation = __send__(name)
338
344
  Array.wrap(relation).each do |r|
339
345
  r.instance_variable_set(:@new_record, new_record)
346
+ # The embedded documents were also constructed as new; drop
347
+ # their pre-create change markers when they are marked as
348
+ # persisted, as #internal_state= does for the parent document.
349
+ r.instance_variable_set(:@pre_create_changed_attributes, nil) unless new_record
340
350
  end
341
351
  end
342
352
  end
@@ -34,7 +34,7 @@ module Mongoid
34
34
  #
35
35
  # @return [ true | false ] Whether the model is encrypted.
36
36
  def encrypted?
37
- !encrypt_metadata.empty? || fields.any? { |_, field| field.is_a?(Mongoid::Fields::Encrypted) }
37
+ !encrypt_metadata.empty? || fields.any? { |_, field| field.encrypted? }
38
38
  end
39
39
 
40
40
  # Whether an encryption schema has to be generated for this model.
@@ -44,21 +44,29 @@ module Mongoid
44
44
  # group has no encrypted field of its own, but its collection still needs
45
45
  # a schema, otherwise the embedded fields are written in plaintext.
46
46
  #
47
- # The answer is memoized, since this runs on the persistence path.
48
- # Declaring encryption on a model after it has already been persisted is
49
- # not supported.
47
+ # This runs on the persistence path, so the answer is memoized. A true
48
+ # answer is always memoized. A false answer is memoized only when every
49
+ # embeds_one target in the walk resolved to a class; if one could not be
50
+ # resolved yet, the answer is recomputed next time, so a class loaded
51
+ # later is still detected. Declaring encryption on a model after it has
52
+ # already been persisted is not supported.
50
53
  #
51
54
  # @return [ true | false ] Whether the model needs an encryption schema.
52
55
  #
53
56
  # @api private
54
57
  def requires_encryption_schema?
55
58
  return @requires_encryption_schema if defined?(@requires_encryption_schema)
59
+ return @requires_encryption_schema = true if encrypted?
56
60
 
57
- @requires_encryption_schema = encrypted? || embeds_encrypted?([ self ])
61
+ found, complete = embedded_encryption_scan([ self ])
62
+ @requires_encryption_schema = found if found || complete
63
+ found
58
64
  end
59
65
 
60
- # Whether any model reachable through this model's embeds_one relations
61
- # declares encryption.
66
+ # Walks the models reachable through this model's embeds_one relations,
67
+ # looking for one that declares encryption. Subclasses of an
68
+ # association target are included: any of them can be assigned to the
69
+ # association.
62
70
  #
63
71
  # embeds_many is not considered: libmongocrypt cannot express per-field
64
72
  # encryption under array items, so those fields are never mapped.
@@ -66,21 +74,40 @@ module Mongoid
66
74
  # @param [ Array<Class> ] path The models the walk is already inside of.
67
75
  # A model embedding itself terminates here.
68
76
  #
69
- # @return [ true | false ] Whether an embedded model is encrypted.
77
+ # @return [ Array<true | false> ] A pair: whether an embedded model is
78
+ # encrypted, and whether every embeds_one target the walk needed
79
+ # resolved to a class. The second value is only meaningful when the
80
+ # first is false.
70
81
  #
71
82
  # @api private
72
- def embeds_encrypted?(path)
73
- relations.each_value.any? do |relation|
74
- next false unless relation.is_a?(Association::Embedded::EmbedsOne)
83
+ def embedded_encryption_scan(path)
84
+ complete = true
85
+
86
+ relations.each_value do |relation|
87
+ next unless relation.is_a?(Association::Embedded::EmbedsOne)
75
88
 
76
89
  klass = relation.try_relation_class
77
- # An association target does not have to be a Mongoid document, and
78
- # the class it names does not have to exist.
79
- next false unless klass.respond_to?(:encrypted?)
80
- next false if path.include?(klass)
90
+ # The class an association names does not have to exist yet.
91
+ if klass.nil?
92
+ complete = false
93
+ next
94
+ end
95
+
96
+ # An association target does not have to be a Mongoid document.
97
+ next unless klass.respond_to?(:encrypted?)
98
+
99
+ ([ klass ] + klass.descendants).uniq.each do |candidate|
100
+ next if path.include?(candidate)
101
+ return [ true, true ] if candidate.encrypted?
81
102
 
82
- klass.encrypted? || klass.embeds_encrypted?(path + [ klass ])
103
+ found, nested_complete = candidate.embedded_encryption_scan(path + [ candidate ])
104
+ return [ true, true ] if found
105
+
106
+ complete &&= nested_complete
107
+ end
83
108
  end
109
+
110
+ [ false, complete ]
84
111
  end
85
112
 
86
113
  # Override the key_id for the model.
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mongoid
4
+ module Errors
5
+ # This error is raised when an encrypted field is used as the foreign key
6
+ # of a has_and_belongs_to_many association. Encryption protects an array
7
+ # only as a whole, while the association queries and atomically updates
8
+ # individual elements of it, so the association cannot manage the key.
9
+ class InvalidEncryptedAssociation < MongoidError
10
+ # Create the new error.
11
+ #
12
+ # @example Create the error.
13
+ # InvalidEncryptedAssociation.new(Person, :preferences, 'preference_ids')
14
+ #
15
+ # @param [ Class ] klass The model declaring the association.
16
+ # @param [ Symbol | String ] association The association name.
17
+ # @param [ String ] field The encrypted foreign key field.
18
+ def initialize(klass, association, field)
19
+ super(
20
+ compose_message(
21
+ 'invalid_encrypted_association',
22
+ { klass: klass.name, association: association, field: field }
23
+ )
24
+ )
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mongoid
4
+ module Errors
5
+ # This error is raised when two models writing to the same namespace
6
+ # describe a field, or the collection metadata, differently. The schema map
7
+ # has one entry per namespace, so the two descriptions cannot both be
8
+ # honored.
9
+ class InvalidEncryptionSchema < MongoidError
10
+ # Create the new error.
11
+ #
12
+ # @example Create the error.
13
+ # InvalidEncryptionSchema.new('music.bands', 'properties.code')
14
+ #
15
+ # @param [ String | nil ] namespace The namespace being described.
16
+ # @param [ String ] path The conflicting path in the schema.
17
+ def initialize(namespace, path)
18
+ super(
19
+ compose_message(
20
+ 'invalid_encryption_schema',
21
+ { namespace: namespace || 'the collection', path: path }
22
+ )
23
+ )
24
+ end
25
+ end
26
+ end
27
+ end
@@ -15,11 +15,13 @@ module Mongoid
15
15
  # @param [ Class ] klass The model class.
16
16
  # @param [ String ] namespace The namespace the model resolved to.
17
17
  # @param [ String | Symbol ] client The name of the client in use.
18
- def initialize(klass, namespace, client)
18
+ # @param [ String | nil ] field The encrypted field missing from the
19
+ # schema entry, if the namespace itself is covered.
20
+ def initialize(klass, namespace, client, field = nil)
19
21
  super(
20
22
  compose_message(
21
- 'no_encryption_schema',
22
- { klass: klass, namespace: namespace, client: client }
23
+ field ? 'no_encryption_schema_field' : 'no_encryption_schema',
24
+ { klass: klass, namespace: namespace, client: client, field: field }
23
25
  )
24
26
  )
25
27
  end
@@ -20,6 +20,8 @@ require "mongoid/errors/invalid_collection"
20
20
  require "mongoid/errors/invalid_config_file"
21
21
  require "mongoid/errors/invalid_config_option"
22
22
  require "mongoid/errors/invalid_dependent_strategy"
23
+ require "mongoid/errors/invalid_encrypted_association"
24
+ require "mongoid/errors/invalid_encryption_schema"
23
25
  require "mongoid/errors/invalid_field"
24
26
  require "mongoid/errors/invalid_field_option"
25
27
  require "mongoid/errors/invalid_field_type"
@@ -3,8 +3,8 @@
3
3
 
4
4
  module Mongoid
5
5
  module Fields
6
- # Represents a field that should be encrypted.
7
- class Encrypted < Standard
6
+ # Behavior shared by the field types whose value is encrypted.
7
+ module EncryptionOptions
8
8
  def initialize(name, options = {})
9
9
  @encryption_options = if options[:encrypt].is_a?(Hash)
10
10
  options[:encrypt]
@@ -14,6 +14,11 @@ module Mongoid
14
14
  super
15
15
  end
16
16
 
17
+ # @return [ true ] Always true: this field is encrypted.
18
+ def encrypted?
19
+ true
20
+ end
21
+
17
22
  # @return [ true | false | nil ] Whether the field should be encrypted using a
18
23
  # deterministic encryption algorithm; if not specified, nil is returned.
19
24
  def deterministic?
@@ -27,7 +32,8 @@ module Mongoid
27
32
  end
28
33
 
29
34
  # @return [ String | nil ] The name of the field that contains the
30
- # key alt name to use for encryption; if not specified, nil is returned.
35
+ # key alt name to use for encryption; if not specified, nil is
36
+ # returned.
31
37
  def key_name_field
32
38
  @encryption_options[:key_name_field]
33
39
  end
@@ -44,5 +50,10 @@ module Mongoid
44
50
  @encryption_options[:key_id] = key_id
45
51
  end
46
52
  end
53
+
54
+ # Represents a field that should be encrypted.
55
+ class Encrypted < Standard
56
+ include EncryptionOptions
57
+ end
47
58
  end
48
59
  end
@@ -188,5 +188,14 @@ module Mongoid
188
188
  # @return [ Object ] The serialized default.
189
189
  def serialize_default(object); object; end
190
190
  end
191
+
192
+ # Represents a foreign key field whose value is encrypted.
193
+ #
194
+ # A belongs_to association stores its key in a single value, so it can be
195
+ # encrypted like any other field. The encryption options of the field
196
+ # declared before the association are preserved here.
197
+ class EncryptedForeignKey < ForeignKey
198
+ include EncryptionOptions
199
+ end
191
200
  end
192
201
  end
@@ -58,6 +58,16 @@ module Mongoid
58
58
  false
59
59
  end
60
60
 
61
+ # Is this field encrypted?
62
+ #
63
+ # @example Is the field encrypted?
64
+ # field.encrypted?
65
+ #
66
+ # @return [ true | false ] If the field is encrypted.
67
+ def encrypted?
68
+ false
69
+ end
70
+
61
71
  # Create the new field with a name and optional additional options.
62
72
  #
63
73
  # @example Create the new field.
@@ -36,6 +36,7 @@ module Mongoid
36
36
  validate_field_name(klass, name)
37
37
  validate_name_uniqueness(klass, name, options)
38
38
  validate_options(klass, name, options)
39
+ validate_encrypted_association(klass, name, options)
39
40
  end
40
41
 
41
42
  # Validate the association definition.
@@ -120,6 +121,34 @@ module Mongoid
120
121
  end
121
122
  end
122
123
  end
124
+
125
+ # Determine if an encrypted field is the foreign key of a
126
+ # has_and_belongs_to_many association. Defining the field after the
127
+ # association has to be rejected, and so does defining it on a parent
128
+ # of the class that declares the association: the field is propagated
129
+ # to the descendants and would replace their association key.
130
+ #
131
+ # @api private
132
+ #
133
+ # @param [ Class ] klass The model class.
134
+ # @param [ Symbol | String ] name The field name.
135
+ # @param [ Hash ] options The provided options.
136
+ #
137
+ # @raise [ Errors::InvalidEncryptedAssociation ] If the field is the
138
+ # foreign key of a has_and_belongs_to_many association.
139
+ def validate_encrypted_association(klass, name, options)
140
+ return unless options[:encrypt]
141
+
142
+ ([ klass ] + klass.descendants).each do |candidate|
143
+ field = candidate.fields[name.to_s]
144
+ next unless field&.foreign_key?
145
+
146
+ association = field.association
147
+ next unless association.is_a?(Association::Referenced::HasAndBelongsToMany)
148
+
149
+ raise Errors::InvalidEncryptedAssociation.new(candidate, association.name, name)
150
+ end
151
+ end
123
152
  end
124
153
  end
125
154
  end
@@ -485,7 +485,7 @@ module Mongoid
485
485
  Validators::Macro.validate(self, name, options)
486
486
  added = add_field(named, options)
487
487
  descendants.each do |subclass|
488
- subclass.add_field(named, options)
488
+ subclass.add_field(named, descendant_field_options(subclass, named, options))
489
489
  end
490
490
  added
491
491
  end
@@ -580,6 +580,41 @@ module Mongoid
580
580
  field
581
581
  end
582
582
 
583
+ # Build the options to apply to a descendant when a belongs_to
584
+ # association declared on an ancestor generates its foreign key. A
585
+ # descendant may declare the field as encrypted before the association
586
+ # is declared on the parent. The encryption options and the declared
587
+ # type belong to the descendant and must survive the propagation, as
588
+ # must the foreign key behavior.
589
+ #
590
+ # When the descendant's encryption options differ from the ancestor's,
591
+ # the difference is preserved: the schema generation reports it instead
592
+ # of one declaration silently overriding the other.
593
+ #
594
+ # A field declared without an association is not covered: the ancestor's
595
+ # declaration keeps replacing the descendant's field as before.
596
+ #
597
+ # @api private
598
+ #
599
+ # @param [ Class ] subclass The descendant the field is propagated to.
600
+ # @param [ String ] name The field name.
601
+ # @param [ Hash ] options The options of the ancestor's declaration.
602
+ #
603
+ # @return [ Hash ] The options to apply to the descendant.
604
+ def descendant_field_options(subclass, name, options)
605
+ return options unless options[:identity]
606
+ return options unless options[:association].is_a?(Association::Referenced::BelongsTo)
607
+
608
+ existing = subclass.fields[name]
609
+ return options unless existing&.encrypted?
610
+
611
+ preserved = options.merge(type: existing.type, encrypt: existing.options[:encrypt])
612
+ if existing.foreign_key? && existing.association
613
+ preserved[:association] = existing.association
614
+ end
615
+ preserved
616
+ end
617
+
583
618
  # Run through all custom options stored in Mongoid::Fields.options and
584
619
  # execute the handler if the option is provided.
585
620
  #
@@ -803,7 +838,12 @@ module Mongoid
803
838
  opts = options.merge(klass: self)
804
839
  opts[:type] = retrieve_and_validate_type(name, options[:type])
805
840
  return Fields::Localized.new(name, opts) if options[:localize]
806
- return Fields::ForeignKey.new(name, opts) if options[:identity]
841
+
842
+ if options[:identity]
843
+ return Fields::EncryptedForeignKey.new(name, opts) if options[:encrypt]
844
+
845
+ return Fields::ForeignKey.new(name, opts)
846
+ end
807
847
  return Fields::Encrypted.new(name, opts) if options[:encrypt]
808
848
  Fields::Standard.new(name, opts)
809
849
  end
@@ -209,12 +209,15 @@ module Mongoid
209
209
  # that schema, and when it is absent it asks the server for a schema
210
210
  # instead; a collection without a validator then yields no encryption at
211
211
  # all, and no error. Checking the namespace here is what turns that silent
212
- # downgrade into a failure.
212
+ # downgrade into a failure. When the namespace is present, its entry is
213
+ # checked against the fields the model declares as encrypted too: a
214
+ # hand-written schema can cover the namespace and still leave out a field.
213
215
  #
214
216
  # @param [ Mongo::Client ] client The client this context resolved to.
215
217
  #
216
218
  # @raise [ Errors::NoEncryptionSchema ] if the model needs an encryption
217
- # schema and the client's schema does not cover the target namespace.
219
+ # schema and the client's schema does not cover the target namespace, or
220
+ # the namespace entry does not cover every encrypted field of the model.
218
221
  def verify_encryption_schema!(client)
219
222
  klass = @object.is_a?(Class) ? @object : @object.class
220
223
  # A model whose encrypted fields all live on embedded models declares no
@@ -229,9 +232,48 @@ module Mongoid
229
232
 
230
233
  schema_map = client.options.dig(:auto_encryption_options, :schema_map)
231
234
  namespace = "#{client.database.name}.#{collection_name}"
232
- return if schema_map&.key?(namespace)
235
+ entry = schema_map && schema_map[namespace]
236
+ raise Errors::NoEncryptionSchema.new(klass, namespace, client_name) unless entry
233
237
 
234
- raise Errors::NoEncryptionSchema.new(klass, namespace, client_name)
238
+ # The schema map may have been written by hand, with symbol keys rather
239
+ # than strings; both are tolerated at every level.
240
+ Mongoid.config.encrypted_paths_for(klass).each do |path|
241
+ next if encryption_schema_has_path?(entry, path)
242
+
243
+ raise Errors::NoEncryptionSchema.new(
244
+ klass, namespace, client_name, path.join('.')
245
+ )
246
+ end
247
+ end
248
+
249
+ # Whether a schema entry encrypts the property at the given path.
250
+ #
251
+ # @param [ Hash ] entry The namespace schema entry.
252
+ # @param [ Array<String> ] path The property path, outermost first.
253
+ #
254
+ # @return [ true | false ] Whether the leaf is marked as encrypted.
255
+ def encryption_schema_has_path?(entry, path)
256
+ node = entry
257
+ path.each do |name|
258
+ properties = schema_value(node, 'properties')
259
+ return false unless properties.is_a?(Hash)
260
+
261
+ node = schema_value(properties, name)
262
+ return false unless node.is_a?(Hash)
263
+ end
264
+ !schema_value(node, 'encrypt').nil?
265
+ end
266
+
267
+ # Read a schema key that may be written as a string or a symbol.
268
+ #
269
+ # @param [ Hash ] hash The schema object.
270
+ # @param [ String ] key The key to read.
271
+ #
272
+ # @return [ Object ] The value, or nil when the key is absent.
273
+ def schema_value(hash, key)
274
+ return hash[key] if hash.key?(key)
275
+
276
+ hash[key.to_sym]
235
277
  end
236
278
 
237
279
  def client_options
@@ -42,6 +42,7 @@ module Mongoid
42
42
  @attributes = attributes
43
43
  @attributes_before_type_cast = @attributes.dup
44
44
  @changed_attributes = {}
45
+ @pre_create_changed_attributes = nil
45
46
  @previous_changes = {}
46
47
  @previous_attributes = {}
47
48
  @previously_new_record = false
@@ -167,7 +167,11 @@ module Mongoid
167
167
  #
168
168
  # @return [ Array<Hash> ] the raw index documents
169
169
  def get_indexes(names)
170
- collection.search_indexes.select { |i| names.include?(i['name']) }
170
+ # Reading index metadata sends no document data, so it is exempt from
171
+ # the encryption schema check, like the other index operations here.
172
+ Threaded.with_collection_management do
173
+ collection.search_indexes.select { |i| names.include?(i['name']) }
174
+ end
171
175
  end
172
176
  end
173
177
  end
@@ -65,15 +65,18 @@ module Mongoid
65
65
  # @param [ true | false ] wait whether to wait for the indexes to be
66
66
  # built.
67
67
  def create_search_indexes(models = ::Mongoid.models, wait: true)
68
- searchable = models.select { |m| m.search_index_specs.any? }
68
+ Threaded.with_collection_management do
69
+ searchable = models.select { |m| m.search_index_specs.any? }
69
70
 
70
- # queue up the search index creation requests
71
- index_names_by_model = searchable.each_with_object({}) do |model, obj|
72
- logger.info("MONGOID: Creating search indexes on #{model}...")
73
- obj[model] = model.create_search_indexes
74
- end
71
+ # queue up the search index creation requests
72
+ index_names_by_model = searchable.each_with_object({}) do |model, obj|
73
+ logger.info("MONGOID: Creating search indexes on #{model}...")
74
+ obj[model] = model.create_search_indexes
75
+ end
75
76
 
76
- wait_for_search_indexes(index_names_by_model) if wait
77
+ wait_for_search_indexes(index_names_by_model) if wait
78
+ index_names_by_model
79
+ end
77
80
  end
78
81
 
79
82
  # Return the list of indexes by model that exist in the database but aren't
@@ -40,7 +40,7 @@ module Mongoid
40
40
  EXECUTE_CALLBACKS = '[mongoid]:execute-callbacks'
41
41
 
42
42
  # The key for the time left in the current in-memory regexp budget.
43
- REGEXP_BUDGET_KEY = 'regexp-budget'
43
+ REGEXP_BUDGET_KEY = '[mongoid]:regexp-budget'
44
44
 
45
45
  extend self
46
46
 
@@ -5,5 +5,5 @@ module Mongoid
5
5
  #
6
6
  # Note that this file is automatically updated via `rake candidate:create`.
7
7
  # Manual changes to this file will be overwritten by that rake task.
8
- VERSION = '9.0.12'
8
+ VERSION = '9.0.14'
9
9
  end
@@ -37,5 +37,9 @@ module Mongoid
37
37
  warning :autosave_saves_unchanged_documents, "Autosave associations are currently configured to save documents even if they haven't changed. " \
38
38
  'This legacy behavior is deprecated. Set Mongoid.autosave_saves_unchanged_documents to false to ' \
39
39
  'skip saving unchanged documents in autosave associations.'
40
+ warning :bypass_auto_encryption,
41
+ 'auto_encryption_options includes bypass_auto_encryption: true. The driver ' \
42
+ 'will not encrypt anything for this client: fields declared with encrypt: true ' \
43
+ 'are written to the database unencrypted.'
40
44
  end
41
45
  end
@@ -114,4 +114,61 @@ describe Mongoid::Atomic::Modifiers do
114
114
  end
115
115
  end
116
116
 
117
+ context 'when assigning embeds_many attributes on a new embedded document' do
118
+ let(:cabinet) do
119
+ ModifierModels::Cabinet.create!
120
+ end
121
+
122
+ before do
123
+ cabinet.reload
124
+ end
125
+
126
+ context 'when the nested documents are assigned on the built document' do
127
+ before do
128
+ cabinet.build_drawer(matcher: 'all')
129
+ cabinet.drawer.assign_attributes(items: [ { name: 'a' } ])
130
+ end
131
+
132
+ it 'saves without conflicting $set paths' do
133
+ expect { cabinet.save! }.not_to raise_error
134
+
135
+ cabinet.reload
136
+ expect(cabinet.drawer.matcher).to eq('all')
137
+ expect(cabinet.drawer.items.map(&:name)).to eq([ 'a' ])
138
+ end
139
+ end
140
+
141
+ context 'when the nested documents are assigned via the parent' do
142
+ before do
143
+ cabinet.assign_attributes(drawer: { matcher: 'all', items: [ { name: 'a' } ] })
144
+ end
145
+
146
+ it 'saves without conflicting $set paths' do
147
+ expect { cabinet.save! }.not_to raise_error
148
+
149
+ cabinet.reload
150
+ expect(cabinet.drawer.matcher).to eq('all')
151
+ expect(cabinet.drawer.items.map(&:name)).to eq([ 'a' ])
152
+ end
153
+ end
154
+
155
+ context 'when an assigned child has its own embedded collection' do
156
+ let(:item) { ModifierModels::Item.new(name: 'a') }
157
+
158
+ before do
159
+ item.assign_attributes(widgets: [ { label: 'w' } ])
160
+ cabinet.build_drawer(matcher: 'all')
161
+ cabinet.drawer.assign_attributes(items: [ item ])
162
+ end
163
+
164
+ it 'does not write the child collection to the root document' do
165
+ expect { cabinet.save! }.not_to raise_error
166
+
167
+ stored = ModifierModels::Cabinet.collection.find(_id: cabinet.id).first
168
+ expect(stored.keys).to contain_exactly('_id', 'drawer')
169
+ expect(stored['drawer']['items'].first['widgets'].first['label']).to eq('w')
170
+ end
171
+ end
172
+ end
173
+
117
174
  end