-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
PHPORM-206 Add model schema version feature #3021
Conversation
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.
Thanks for proposing this feature.
The ability to store documents with different schemas in the same collection is undoubtedly one of the great strengths of the MongoDB model. We have a tutorial on this subject.
I think there's a piece missing: the code to automatically update documents when they're read from the DB. This would probably be done in a listener of the retrieved
event.
In the trait:
static::retrieved(function (self $model) {
$model->upgradeDocumentVersion($model, $model->{static::getDocumentVersionKey()} ?? 0);
});
In the code would look like this (example for the docs):
public function upgradeDocumentVersion(Model $document, int $fromVersion): void
{
switch (true) {
case $fromVersion < 2:
// Field renamed
$document->newField = $document->oldField;
// No break to apply all changes
case $fromVersion < 3:
// Field type updated
$document->price = (float) $document->price;
// ...
}
}
src/Eloquent/HasSchemaVersion.php
Outdated
* migrate schema and set current model version | ||
* @return void | ||
*/ | ||
public function upgradeSchemaVersion(): void |
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.
Why do you need this public method? All this code can be safely in the "retrieved" closure.
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.
I think it's a good idea to let the user choose which method to use, between migrating + upgrading the version or migrating and upgrading the version later.
c56af3b
to
09c95b4
Compare
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.
@florianJacques I updated the implementation so that
- the
SCHEMA_VERSION
constant is required - the tests ensure the documents are updated when retrieved
- adding documentation in the trait description
Co-authored-by: Jérôme Tamarelle <jerome@tamarelle.net>
Co-authored-by: Jérôme Tamarelle <jerome@tamarelle.net>
09c95b4
to
a8ca8aa
Compare
src/Eloquent/HasSchemaVersion.php
Outdated
|
||
trait HasSchemaVersion | ||
{ | ||
public $currentSchemaVersion = 1; |
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.
I don't think this is the best solution, do you have any ideas?
src/Eloquent/HasSchemaVersion.php
Outdated
}); | ||
|
||
static::retrieved(function ($model) { | ||
$model->upgradeSchemaVersion(); |
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.
I think we need to find a way to give control over the call to this function
src/Eloquent/HasSchemaVersion.php
Outdated
* migrate schema and set current model version | ||
* @return void | ||
*/ | ||
public function upgradeSchemaVersion(): void |
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.
I think it's a good idea to let the user choose which method to use, between migrating + upgrading the version or migrating and upgrading the version later.
Thanks for your work ! |
Thank you @florianJacques |
Added a feature for managing mongoDB document versioning.
The default key is "schema_version"
If the version is not specified, t is automatically set to 1
Usage
For overwrite version key:
For update document version: