-
Notifications
You must be signed in to change notification settings - Fork 1.5k
DOCSP-41306: schema version trait #3051
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8c9d05f
DOCSP-41306: schema version trait
rustagir f3e67c7
MW PR fixes 1
rustagir bc1349a
add test wip
rustagir 5543d5c
add tests
rustagir 16aff95
apply phpcbf formatting
rustagir e114452
JM tech review 1
rustagir e55af09
error
rustagir 0ae0aee
comment style
rustagir 632297e
Fix test, expose 2 models, lock laravel version to avoid breaking change
GromNaN 0d21446
JM tech review 2
rustagir 5c98416
fixes
rustagir 9e5983a
revert database v
rustagir 7712c28
JM tech review 3
rustagir 342e292
JM tech review 4
rustagir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -33,6 +33,8 @@ to {+odm-short+} models: | |||||||||||||
- :ref:`laravel-model-customize` explains several model class customizations. | ||||||||||||||
- :ref:`laravel-model-pruning` shows how to periodically remove models that | ||||||||||||||
you no longer need. | ||||||||||||||
- :ref:`laravel-schema-versioning` shows how to implement model schema | ||||||||||||||
versioning. | ||||||||||||||
|
||||||||||||||
.. _laravel-model-define: | ||||||||||||||
|
||||||||||||||
|
@@ -67,7 +69,6 @@ This model is stored in the ``planets`` MongoDB collection. | |||||||||||||
To learn how to specify the database name that your Laravel application uses, | ||||||||||||||
:ref:`laravel-quick-start-connect-to-mongodb`. | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
.. _laravel-authenticatable-model: | ||||||||||||||
|
||||||||||||||
Extend the Authenticatable Model | ||||||||||||||
|
@@ -333,3 +334,93 @@ models that the prune action deletes: | |||||||||||||
:emphasize-lines: 5,10,12 | ||||||||||||||
:dedent: | ||||||||||||||
|
||||||||||||||
.. _laravel-schema-versioning: | ||||||||||||||
|
||||||||||||||
Create a Versioned Model Schema | ||||||||||||||
------------------------------- | ||||||||||||||
|
||||||||||||||
You can implement a schema versioning pattern into your application by | ||||||||||||||
using the ``HasSchemaVersion`` trait on an Eloquent model. You might | ||||||||||||||
choose to implement a schema version to organize or standardize a | ||||||||||||||
collection that contains data with different schemas. | ||||||||||||||
|
||||||||||||||
.. tip:: | ||||||||||||||
|
||||||||||||||
To learn more about schema versioning, see the :manual:`Model Data for | ||||||||||||||
Schema Versioning </tutorial/model-data-for-schema-versioning/>` | ||||||||||||||
tutorial in the {+server-docs-name+}. | ||||||||||||||
|
||||||||||||||
To use this feature with models that use MongoDB as a database, add the | ||||||||||||||
``MongoDB\Laravel\Eloquent\HasSchemaVersion`` import to your model. | ||||||||||||||
Then, set the ``SCHEMA_VERSION`` constant to set the current schema | ||||||||||||||
version for your collection. | ||||||||||||||
|
||||||||||||||
When creating your model, you can define the ``migrateSchema()`` method | ||||||||||||||
to specify a migration to the current schema version upon saving a | ||||||||||||||
model. | ||||||||||||||
|
||||||||||||||
Schema Versioning Example | ||||||||||||||
~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||
|
||||||||||||||
The example class is defined with the following behavior: | ||||||||||||||
|
||||||||||||||
- Implements the ``HasSchemaVersion`` trait and sets the current | ||||||||||||||
``SCHEMA_VERSION`` to ``2`` | ||||||||||||||
|
||||||||||||||
- Defines the ``migrateSchema()`` method to migrate models in which the | ||||||||||||||
schema version is less than ``2`` to have a ``galaxy`` field that has a value | ||||||||||||||
of ``'Milky Way'`` | ||||||||||||||
|
||||||||||||||
.. literalinclude:: /includes/eloquent-models/PlanetSchemaVersion.php | ||||||||||||||
:language: php | ||||||||||||||
:emphasize-lines: 10,12,14 | ||||||||||||||
:dedent: | ||||||||||||||
|
||||||||||||||
When you save a model in which the ``schema_version`` field is | ||||||||||||||
absent or the value is less than ``2``, Laravel adds the ``galaxy`` | ||||||||||||||
field and updates the schema version to the current version (``2``). | ||||||||||||||
|
||||||||||||||
The following code inserts instances of the ``Planet`` model that are | ||||||||||||||
not at the current schema version, then retrieves the models from the | ||||||||||||||
collection to demonstrate the migration changes: | ||||||||||||||
|
||||||||||||||
.. io-code-block:: | ||||||||||||||
:copyable: true | ||||||||||||||
|
||||||||||||||
.. input:: | ||||||||||||||
:language: php | ||||||||||||||
|
||||||||||||||
$saturn = Planet::create([ | ||||||||||||||
'name' => 'Saturn', | ||||||||||||||
'type' => 'gas', | ||||||||||||||
]); | ||||||||||||||
|
||||||||||||||
$wasp = Planet::create([ | ||||||||||||||
'name' => 'WASP-39 b', | ||||||||||||||
'type' => 'gas', | ||||||||||||||
'schema_version' => 1, | ||||||||||||||
]); | ||||||||||||||
|
||||||||||||||
$planets = Planet::where('type', 'gas') | ||||||||||||||
->get(); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code snippets can be extracted from the test file like this: laravel-mongodb/docs/includes/query-builder/QueryBuilderTest.php Lines 51 to 56 in a62d4b9
|
||||||||||||||
|
||||||||||||||
.. output:: | ||||||||||||||
:language: none | ||||||||||||||
:visible: false | ||||||||||||||
|
||||||||||||||
[ | ||||||||||||||
{ | ||||||||||||||
"_id": ..., | ||||||||||||||
"name": "Saturn", | ||||||||||||||
"type": "gas", | ||||||||||||||
"galaxy": "Milky Way", | ||||||||||||||
rustagir marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
"schema_version": 2, | ||||||||||||||
}, | ||||||||||||||
{ | ||||||||||||||
"_id": ..., | ||||||||||||||
"name": "WASP-39 b", | ||||||||||||||
"type": "gas", | ||||||||||||||
"galaxy": "Milky Way", | ||||||||||||||
"schema_version": 2, | ||||||||||||||
} | ||||||||||||||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use MongoDB\Laravel\Eloquent\HasSchemaVersion; | ||
use MongoDB\Laravel\Eloquent\Model; | ||
|
||
class Planet extends Model | ||
{ | ||
use HasSchemaVersion; | ||
|
||
public const SCHEMA_VERSION = 2; | ||
|
||
public function migrateSchema(int $fromVersion): void | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if ($fromVersion < 2) { | ||
$this->galaxy = 'Milky Way'; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be 1 for the first version of the data doesn't contain any versioned schema.