Skip to content

Commit f65ada2

Browse files
Rename trait and refacto
1 parent bc550c1 commit f65ada2

File tree

4 files changed

+81
-87
lines changed

4 files changed

+81
-87
lines changed

src/Eloquent/HasDocumentVersion.php

Lines changed: 0 additions & 76 deletions
This file was deleted.

src/Eloquent/HasSchemaVersion.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Laravel\Eloquent;
6+
7+
trait HasSchemaVersion
8+
{
9+
public $currentSchemaVersion = 1;
10+
11+
/**
12+
* Auto call on model instance as booting
13+
*
14+
* @return void
15+
*/
16+
public static function bootHasSchemaVersion(): void
17+
{
18+
static::saving(function ($model) {
19+
if (!$model->getSchemaVersion()) {
20+
$model->setSchemaVersion($model->currentSchemaVersion);
21+
}
22+
});
23+
24+
static::retrieved(function ($model) {
25+
if ($model->getSchemaVersion() < $model->currentSchemaVersion) {
26+
$model->migrateSchemaVersion($model->getSchemaVersion());
27+
}
28+
});
29+
}
30+
31+
/**
32+
* Migrate document to the current schema version.
33+
*/
34+
public function migrateSchema(int $fromVersion): void
35+
{
36+
}
37+
38+
/**
39+
* Get Current document version
40+
*
41+
* @return int
42+
*/
43+
public function getSchemaVersion(): int
44+
{
45+
return $this->{static::getSchemaVersionKey()} ?? 0;
46+
}
47+
48+
/**
49+
* @param int $version
50+
* @return void
51+
*/
52+
public function setSchemaVersion(int $version): void
53+
{
54+
$this->{static::getSchemaVersionKey()} = $version;
55+
}
56+
57+
/**
58+
* Get document version key
59+
*
60+
* @return string
61+
*/
62+
public static function getSchemaVersionKey(): string
63+
{
64+
return 'schema_version';
65+
}
66+
}

tests/DocumentVersionTest.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,29 @@ public function tearDown(): void
1414
DocumentVersion::truncate();
1515
}
1616

17-
public function testCreateWithNullId()
17+
public function testWithBasicDocument()
1818
{
1919
$document = new DocumentVersion(['name' => 'Luc']);
20-
$this->assertEmpty($document->getDocumentVersion());
20+
$this->assertEmpty($document->getSchemaVersion());
2121
$document->save();
2222

23-
$this->assertEquals(1, $document->getDocumentVersion());
23+
$this->assertEquals(1, $document->getSchemaVersion());
2424
$this->assertNull($document->age);
2525

26-
$document = DocumentVersion::query()->where('name', 'Luc')->first();
26+
$document->currentSchemaVersion = 2;
27+
$document->migrateSchemaVersion($document->getSchemaVersion());
28+
2729
$this->assertEquals(35, $document->age);
28-
$this->assertEquals(2, $document->getDocumentVersion());
30+
$this->assertEquals(2, $document->getSchemaVersion());
2931

3032
// Test without migration
3133
$newDocument = new DocumentVersion(['name' => 'Vador']);
32-
$newDocument->setDocumentVersion(2);
34+
$newDocument->setSchemaVersion(2);
3335
$newDocument->save();
36+
$newDocument->currentSchemaVersion = 2;
37+
$newDocument->migrateSchema($newDocument->getSchemaVersion());
3438

35-
$this->assertEquals(2, $newDocument->getDocumentVersion());
39+
$this->assertEquals(2, $newDocument->getSchemaVersion());
3640
$this->assertNull($newDocument->age);
3741

3842
$newDocument = DocumentVersion::query()->where('name', 'Vador')->first();

tests/Models/DocumentVersion.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@
44

55
namespace MongoDB\Laravel\Tests\Models;
66

7-
use MongoDB\Laravel\Eloquent\HasDocumentVersion;
7+
use MongoDB\Laravel\Eloquent\HasSchemaVersion;
88
use MongoDB\Laravel\Eloquent\Model as Eloquent;
99

1010
class DocumentVersion extends Eloquent
1111
{
12-
use HasDocumentVersion;
12+
use HasSchemaVersion;
1313

1414
protected $connection = 'mongodb';
1515
protected $collection = 'documentVersion';
1616
protected static $unguarded = true;
1717

18-
public function migrateDocumentVersion($fromVersion): void
18+
public function migrateSchemaVersion($fromVersion): void
1919
{
2020
if ($fromVersion) {
2121
if ($fromVersion < 2) {
2222
$this->age = 35;
2323

24-
$this->setDocumentVersion(2);
24+
$this->setSchemaVersion(2);
2525
}
2626
}
2727
}

0 commit comments

Comments
 (0)