-
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
DOCSP-41306: schema version trait #3051
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.
lgtm w/ 1 suggestion!
docs/eloquent-models/model-class.txt
Outdated
version to the current version (``2``). | ||
|
||
The following code creates and saves instances of the ``Planet`` model that are | ||
not at the current schema version, then retrieves the planets from the |
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.
not at the current schema version, then retrieves the planets from the | |
not at the current schema version, then retrieves the Planet documents[? objects?] from the |
docs/eloquent-models/model-class.txt
Outdated
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. |
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.
2a8fb47
to
5543d5c
Compare
docs/eloquent-models/model-class.txt
Outdated
$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 comment
The 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
// begin query where | |
$result = DB::connection('mongodb') | |
->collection('movies') | |
->where('imdb.rating', 9.3) | |
->get(); | |
// end query where |
$planets = Planet::where('type', 'gas') | ||
->get(); | ||
|
||
echo 'After migration:\n' . $planets; |
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.
echo
should be avoided in tests. Otherwise you need to assert the output:
laravel-mongodb/docs/includes/usage-examples/CountTest.php
Lines 36 to 41 in a62d4b9
echo 'Number of documents: ' . $count; | |
// end-count | |
$this->assertEquals(2, $count); | |
$this->expectOutputString('Number of documents: 2'); | |
} |
16aff95
to
0f645c3
Compare
0f645c3
to
e114452
Compare
|
||
class Planet extends Model | ||
{ | ||
protected $fillable = ['name', 'type', 'galaxy']; |
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.
@rustagir this is required to use Planet::create()
.
docs/eloquent-models/model-class.txt
Outdated
When you save or retrieve a model that does not have a schema version | ||
specified, Laravel assumes that it follows the latest schema version. |
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.
We automatically save the current "SCHEMA_VERSION". When a model is retrieved, if it doesn't contain the schema_version property, we assume it was inserted before the model was versioned, so we assume its version 0
and run the full migration.
When you save or retrieve a model that does not have a schema version | |
specified, Laravel assumes that it follows the latest schema version. | |
When you save a model that does not have a schema version specified, | |
Laravel assumes that it follows the latest schema version. |
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.
Will update the copy
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.
LGTM. With a last note: Laravel (the framework) is not involved in the schema version behavior. It is either this library or the trait to be precise.
docs/eloquent-models/model-class.txt
Outdated
When you save a model that does not have a schema version | ||
specified, Laravel assumes that it follows the latest schema version. | ||
When you retrieve a model that does not contain the ``schema_version`` | ||
field, Laravel assumes that its schema version is ``0`` and performs the | ||
migration. |
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.
When you save a model that does not have a schema version | |
specified, Laravel assumes that it follows the latest schema version. | |
When you retrieve a model that does not contain the ``schema_version`` | |
field, Laravel assumes that its schema version is ``0`` and performs the | |
migration. | |
When you save a model that does not have a schema version | |
specified, the trait assumes that it follows the latest schema version. | |
When you retrieve a model that does not contain the ``schema_version`` | |
field, the trait assumes that its schema version is ``0`` and performs the | |
migration. |
https://jira.mongodb.org/browse/DOCSP-41306
STAGING
Adds documentation for schema versioning trait/migration feature
Checklist