-
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 all 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
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,10 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use MongoDB\Laravel\Eloquent\Model; | ||
|
||
class Planet extends Model | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
protected $fillable = ['name', 'type']; | ||
} |
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,26 @@ | ||
<?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; | ||
|
||
protected $fillable = ['name', 'type', 'galaxy']; | ||
|
||
/** | ||
* Migrate documents with a lower schema version to the most current | ||
* schema when inserting new data or retrieving from the database. | ||
*/ | ||
public function migrateSchema(int $fromVersion): void | ||
{ | ||
if ($fromVersion < 2) { | ||
$this->galaxy = 'Milky Way'; | ||
} | ||
} | ||
} |
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,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests; | ||
|
||
use App\Models\Planet; | ||
use MongoDB\Laravel\Tests\TestCase; | ||
|
||
class SchemaVersionTest extends TestCase | ||
{ | ||
/** | ||
* @runInSeparateProcess | ||
* @preserveGlobalState disabled | ||
*/ | ||
public function testSchemaVersion(): void | ||
{ | ||
require_once __DIR__ . '/PlanetSchemaVersion2.php'; | ||
|
||
Planet::truncate(); | ||
|
||
// begin-schema-version | ||
// Simulates a document in the collection with schema version 1 | ||
Planet::insert([ | ||
[ | ||
'name' => 'WASP-39 b', | ||
'type' => 'gas', | ||
'schema_version' => 1, | ||
], | ||
]); | ||
|
||
// Saves a document with no specified schema version | ||
$saturn = Planet::create([ | ||
'name' => 'Saturn', | ||
'type' => 'gas', | ||
]); | ||
|
||
// Retrieves both models from the collection | ||
$planets = Planet::where('type', 'gas') | ||
->get(); | ||
// end-schema-version | ||
|
||
$this->assertCount(2, $planets); | ||
|
||
$p1 = Planet::where('name', 'Saturn')->first(); | ||
|
||
$this->assertEquals(2, $p1->schema_version); | ||
|
||
$p2 = Planet::where('name', 'WASP-39 b')->first(); | ||
|
||
$this->assertEquals(2, $p2->schema_version); | ||
$this->assertEquals('Milky Way', $p2->galaxy); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.