File tree Expand file tree Collapse file tree 4 files changed +80
-12
lines changed Expand file tree Collapse file tree 4 files changed +80
-12
lines changed Original file line number Diff line number Diff line change 6
6
7
7
trait HasDocumentVersion
8
8
{
9
+ public $ documentVersion = 1 ;
10
+
9
11
/**
10
12
* Auto call on model instance as booting
11
13
*
@@ -16,11 +18,31 @@ public static function bootHasDocumentVersion(): void
16
18
static ::saving (function ($ model ) {
17
19
$ versionKey = static ::getDocumentVersionKey ();
18
20
if (!empty ($ versionKey )) {
19
- $ model ->{$ versionKey } = defined (static ::class.'::DOCUMENT_VERSION ' )
20
- ? (int ) static ::DOCUMENT_VERSION
21
- : 1 ;
21
+ $ model ->{$ versionKey } = $ model ->documentVersion ?? 1 ;
22
22
}
23
23
});
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 ;
24
46
}
25
47
26
48
/**
@@ -32,6 +54,6 @@ protected static function getDocumentVersionKey(): string
32
54
{
33
55
return defined (static ::class.'::DOCUMENT_VERSION_KEY ' )
34
56
? (string ) static ::DOCUMENT_VERSION_KEY
35
- : '__v ' ;
57
+ : 'schema_version ' ;
36
58
}
37
59
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 19
19
use MongoDB \Laravel \Connection ;
20
20
use MongoDB \Laravel \Eloquent \Model ;
21
21
use MongoDB \Laravel \Tests \Models \Book ;
22
- use MongoDB \Laravel \Tests \Models \DocumentVersion ;
23
22
use MongoDB \Laravel \Tests \Models \Guarded ;
24
23
use MongoDB \Laravel \Tests \Models \IdIsBinaryUuid ;
25
24
use MongoDB \Laravel \Tests \Models \IdIsInt ;
@@ -54,7 +53,6 @@ public function tearDown(): void
54
53
Book::truncate ();
55
54
Item::truncate ();
56
55
Guarded::truncate ();
57
- DocumentVersion::truncate ();
58
56
}
59
57
60
58
public function testNewModel (): void
@@ -1190,12 +1188,6 @@ public function testCreateWithNullId()
1190
1188
$ this ->assertSame (1 , User::count ());
1191
1189
}
1192
1190
1193
- public function testDocumentVersion ()
1194
- {
1195
- $ document = DocumentVersion::create (['name ' => 'versionTest ' ]);
1196
- $ this ->assertEquals (1 , $ document ->__v );
1197
- }
1198
-
1199
1191
/** @param class-string<Model> $modelClass */
1200
1192
private static function registerModelEvents (string $ modelClass , array &$ events ): void
1201
1193
{
Original file line number Diff line number Diff line change @@ -12,7 +12,17 @@ class DocumentVersion extends Eloquent
12
12
{
13
13
use HasDocumentVersion;
14
14
15
+ public $ documentVersion = 1 ;
16
+
15
17
protected $ connection = 'mongodb ' ;
16
18
protected $ collection = 'documentVersion ' ;
17
19
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
+ }
18
28
}
You can’t perform that action at this time.
0 commit comments