Skip to content

Commit 762f7a9

Browse files
Add retrieved model migration and version helpers
1 parent a51ab6c commit 762f7a9

File tree

4 files changed

+80
-12
lines changed

4 files changed

+80
-12
lines changed

src/Eloquent/HasDocumentVersion.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
trait HasDocumentVersion
88
{
9+
public $documentVersion = 1;
10+
911
/**
1012
* Auto call on model instance as booting
1113
*
@@ -16,11 +18,31 @@ public static function bootHasDocumentVersion(): void
1618
static::saving(function ($model) {
1719
$versionKey = static::getDocumentVersionKey();
1820
if (!empty($versionKey)) {
19-
$model->{$versionKey} = defined(static::class.'::DOCUMENT_VERSION')
20-
? (int) static::DOCUMENT_VERSION
21-
: 1;
21+
$model->{$versionKey} = $model->documentVersion ?? 1;
2222
}
2323
});
24+
25+
static::retrieved(function ($model) {
26+
$model->migrateDocumentVersion($model->getDocumentVersion());
27+
});
28+
}
29+
30+
/**
31+
* migrate model document version schema
32+
*
33+
* @param int $fromVersion
34+
* @return void
35+
*/
36+
public function migrateDocumentVersion(int $fromVersion): void {}
37+
38+
/**
39+
* Get Current document version
40+
*
41+
* @return int
42+
*/
43+
public function getDocumentVersion(): int
44+
{
45+
return (int) $this->{static::getDocumentVersionKey()} ?? 0;
2446
}
2547

2648
/**
@@ -32,6 +54,6 @@ protected static function getDocumentVersionKey(): string
3254
{
3355
return defined(static::class.'::DOCUMENT_VERSION_KEY')
3456
? (string) static::DOCUMENT_VERSION_KEY
35-
: '__v';
57+
: 'schema_version';
3658
}
3759
}

tests/DocumentVersionTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Laravel\Tests;
6+
7+
use MongoDB\Laravel\Tests\Models\DocumentVersion;
8+
use MongoDB\Laravel\Tests\Models\User;
9+
10+
class DocumentVersionTest extends TestCase
11+
{
12+
public function tearDown(): void
13+
{
14+
DocumentVersion::truncate();
15+
}
16+
17+
public function testCreateWithNullId()
18+
{
19+
$document = new DocumentVersion(['name' => 'Luc']);
20+
$this->assertEmpty($document->getDocumentVersion());
21+
$document->save();
22+
23+
$this->assertEquals(1, $document->getDocumentVersion());
24+
$this->assertNull($document->age);
25+
26+
$document = DocumentVersion::query()->where('name', 'Luc')->first();
27+
$this->assertEquals(35, $document->age);
28+
29+
dump($document->toArray());
30+
31+
// Test without migration
32+
$newDocument = new DocumentVersion(['name' => 'Vador']);
33+
$newDocument->documentVersion = 2;
34+
$newDocument->save();
35+
36+
$this->assertEquals(2, $newDocument->getDocumentVersion());
37+
$this->assertNull($newDocument->age);
38+
39+
$document = DocumentVersion::query()->where('name', 'Vador')->first();
40+
$this->assertNull($newDocument->age);
41+
42+
dump($newDocument->toArray());
43+
}
44+
}

tests/ModelTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use MongoDB\Laravel\Connection;
2020
use MongoDB\Laravel\Eloquent\Model;
2121
use MongoDB\Laravel\Tests\Models\Book;
22-
use MongoDB\Laravel\Tests\Models\DocumentVersion;
2322
use MongoDB\Laravel\Tests\Models\Guarded;
2423
use MongoDB\Laravel\Tests\Models\IdIsBinaryUuid;
2524
use MongoDB\Laravel\Tests\Models\IdIsInt;
@@ -54,7 +53,6 @@ public function tearDown(): void
5453
Book::truncate();
5554
Item::truncate();
5655
Guarded::truncate();
57-
DocumentVersion::truncate();
5856
}
5957

6058
public function testNewModel(): void
@@ -1190,12 +1188,6 @@ public function testCreateWithNullId()
11901188
$this->assertSame(1, User::count());
11911189
}
11921190

1193-
public function testDocumentVersion()
1194-
{
1195-
$document = DocumentVersion::create(['name' => 'versionTest']);
1196-
$this->assertEquals(1, $document->__v);
1197-
}
1198-
11991191
/** @param class-string<Model> $modelClass */
12001192
private static function registerModelEvents(string $modelClass, array &$events): void
12011193
{

tests/Models/DocumentVersion.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,17 @@ class DocumentVersion extends Eloquent
1212
{
1313
use HasDocumentVersion;
1414

15+
public $documentVersion = 1;
16+
1517
protected $connection = 'mongodb';
1618
protected $collection = 'documentVersion';
1719
protected static $unguarded = true;
20+
public function migrateDocumentVersion(int $fromVersion): void
21+
{
22+
if ($fromVersion) {
23+
if ($fromVersion < 2) {
24+
$this->age = 35;
25+
}
26+
}
27+
}
1828
}

0 commit comments

Comments
 (0)