Skip to content

Commit 3e1e939

Browse files
authored
MONGOID-5472 Save models using the overridden storage options used for their loading (#5750)
* documents remember the active persistence context when they are instantiated * add Mongoid.legacy_persistence_context_behavior feature flag * update the docs to include the new feature flag * update the release notes * persist the storage options, not the persistence context otherwise, we may leak client instances * update docs too * fix comparison with persistence context * remembered_storage_options can be nil * force correct storage options if reusing a persistence context for a child document * infer correct persistence context for child record * check argument types correctly * add tests to ensure an explicit context overrides a remembered one * remove artifact from previous implementation
1 parent 5ad2364 commit 3e1e939

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

source/reference/configuration.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,30 @@ for details on driver options.
320320
# to parent contexts by default.
321321
# join_contexts: false
322322

323+
# When this flag is false (the default as of Mongoid 9.0), a document that
324+
# is created or loaded will remember the storage options that were active
325+
# when it was loaded, and will use those same options by default when
326+
# saving or reloading itself.
327+
#
328+
# When this flag is true you'll get pre-9.0 behavior, where a document will
329+
# not remember the storage options from when it was loaded/created, and
330+
# subsequent updates will need to explicitly set up those options each time.
331+
#
332+
# For example:
333+
#
334+
# record = Model.with(collection: 'other_collection') { Model.first }
335+
#
336+
# This will try to load the first document from 'other_collection' and
337+
# instantiate it as a Model instance. Pre-9.0, the record object would
338+
# not remember that it came from 'other_collection', and attempts to
339+
# update it or reload it would fail unless you first remembered to
340+
# explicitly specify the collection every time.
341+
#
342+
# As of Mongoid 9.0, the record will remember that it came from
343+
# 'other_collection', and updates and reloads will automatically default
344+
# to that collection, for that record object.
345+
# legacy_persistence_context_behavior: false
346+
323347
# When this flag is false, a document will become read-only only once the
324348
# #readonly! method is called, and an error will be raised on attempting
325349
# to save or update such documents, instead of just on delete. When this

source/release-notes/mongoid-9.0.txt

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,40 @@ by running the following command:
444444

445445
... code-block:: bash
446446

447-
$ ruby -ractive_support/values/time_zone \
448-
-e 'puts ActiveSupport::TimeZone::MAPPING.keys'
447+
$ ruby -ractive_support/values/time_zone \
448+
-e 'puts ActiveSupport::TimeZone::MAPPING.keys'
449+
450+
451+
Records now remember the persistence context in which they were loaded/created
452+
------------------------------------------------------------------------------
453+
454+
Consider the following code:
455+
456+
... code-block:: ruby
457+
458+
record = Model.with(collection: 'other_collection') { Model.first }
459+
record.update(field: 'value')
460+
461+
Prior to Mongoid 9.0, this could would silently fail to execute the update,
462+
because the storage options (here, the specification of an alternate
463+
collection for the model) would not be remembered by the record. Thus, the
464+
record would be loaded from "other_collection", but when updated, would attempt
465+
to look for and update the document in the default collection for Model. To
466+
make this work, you would have had to specify the collection explicitly for
467+
every update.
468+
469+
As of Mongoid 9.0, records that are created or loaded under explicit storage
470+
options, will remember those options (including a named client,
471+
a different database, or a different collection).
472+
473+
If you need the legacy (pre-9.0) behavior, you can enable it with the following
474+
flag:
475+
476+
... code-block:: ruby
477+
478+
Mongoid.legacy_persistence_context_behavior = true
479+
480+
This flag defaults to false in Mongoid 9.
449481

450482

451483
Bug Fixes and Improvements

0 commit comments

Comments
 (0)