Skip to content

Commit b6e19bb

Browse files
authored
MONGOID-5293 Add Rails-style defaults & config.load_defaults (#5405)
* MONGOID-5293 add skeleton * MONGOID-5293 add load_defaults method to config * MONGOID-5293 comments * MONGOID-5293 add docs and release notes
1 parent c4961cb commit b6e19bb

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

source/reference/configuration.txt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,64 @@ for details on driver options.
458458
# methods. (default: false, driver version: 2.18.0+)
459459
#validate_update_replace: false
460460

461+
462+
.. _load-defaults:
463+
464+
Version Based Defaults
465+
======================
466+
467+
Mongoid supports setting the configuration options to the defaults for specific
468+
versions. This is useful for upgrading to a new Mongoid version. When upgrading
469+
your Mongoid version, the following should be set on ``Mongoid::Config``:
470+
471+
.. code:: ruby
472+
473+
Mongoid.configure do |config|
474+
config.load_defaults <OLD VERSION>
475+
end
476+
477+
This way, when upgrading to a new version of Mongoid, your code will run with
478+
the configuration options from the previous version of Mongoid. Then,
479+
one-by-one, you can change the feature flags for the new version, and test that
480+
your code still acts as expected. Once all of the new feature flags have been
481+
accounted for, the call to ``load_defaults`` can be changed to take in the *new*
482+
version, and all of the changed feature flags can be removed. For example, say
483+
we're upgrading from 7.5 to 8.0. Between these two versions, only two feature
484+
flags were added: ``legacy_attributes`` and ``map_big_decimal_to_decimal128``.
485+
Before upgrading to Mongoid 8, the following line can be added:
486+
487+
.. code:: ruby
488+
489+
Mongoid.configure do |config|
490+
config.load_defaults 7.5
491+
end
492+
493+
Now, after upgrading, those two feature flags will default to their 7.5
494+
functionality (``legacy_attributes: true, map_big_decimal_to_decimal128: false``).
495+
Now you can set these feature flags one-by-one and flip them to their 8.0
496+
behavior:
497+
498+
.. code:: ruby
499+
500+
Mongoid.configure do |config|
501+
config.load_defaults 7.5
502+
config.legacy_attributes = false
503+
# config.map_big_decimal_to_decimal128 = true
504+
end
505+
506+
It is advised to do these one at a time, so I have left the second flag
507+
commented out. After verifying your code works as expected with the
508+
``legacy_attributes`` flag turned off, the ``map_big_decimal_to_decimal128``
509+
setting can be uncommented. Once that functionality is verified as well, both
510+
of those lines can be removed and the ``load_defaults`` replaced with:
511+
512+
.. code:: ruby
513+
514+
Mongoid.configure do |config|
515+
config.load_defaults 8.0
516+
end
517+
518+
461519
ERb Preprocessing
462520
=================
463521

source/release-notes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Release Notes
99
.. toctree::
1010
:titlesonly:
1111

12+
release-notes/mongoid-8.1
1213
release-notes/mongoid-8.0
1314
release-notes/mongoid-7.5
1415
release-notes/mongoid-7.4

source/release-notes/mongoid-8.1.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,20 @@ If the document being saved is a new document (i.e. it has not yet been
5757
persisted to the database), then the ``:touch`` option will be ignored, and the
5858
``updated_at`` (and ``created_at``) fields will be updated with the current
5959
time.
60+
61+
62+
Added Version Based Default Configuration
63+
-----------------------------------------
64+
65+
Mongoid 8.1 has added the ability to set the default configurations for a
66+
specific version:
67+
68+
.. code:: ruby
69+
70+
Mongoid.configure do |config|
71+
config.load_defaults 8.0
72+
end
73+
74+
This is helpful for upgrading between versions. See the section on
75+
:ref:`Version Based Default Configuration <load-defaults>` for more details on
76+
how to use this feature to make upgrading between Mongoid versions easier.

0 commit comments

Comments
 (0)