|
4 | 4 |
|
5 | 5 | namespace MongoDB\Laravel\Eloquent;
|
6 | 6 |
|
7 |
| -trait HasSchemaVersion |
8 |
| -{ |
9 |
| - public int $currentSchemaVersion = 1; |
| 7 | +use Error; |
| 8 | +use LogicException; |
10 | 9 |
|
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 |
| - $model->upgradeSchemaVersion(); |
26 |
| - }); |
27 |
| - } |
| 10 | +use function sprintf; |
28 | 11 |
|
| 12 | +/** |
| 13 | + * Use this trait to implement schema versioning in your models. The document |
| 14 | + * is updated automatically when its schema version retrieved from the database |
| 15 | + * is lower than the current schema version of the model. |
| 16 | + * |
| 17 | + * class MyVersionedModel extends Model |
| 18 | + * { |
| 19 | + * use HasSchemaVersion; |
| 20 | + * |
| 21 | + * public const int SCHEMA_VERSION = 1; |
| 22 | + * |
| 23 | + * public function migrateSchema(int $fromVersion): void |
| 24 | + * { |
| 25 | + * // Your logic to update the document to the current schema version |
| 26 | + * } |
| 27 | + * } |
| 28 | + * |
| 29 | + * @see https://www.mongodb.com/docs/manual/tutorial/model-data-for-schema-versioning/ |
| 30 | + * |
| 31 | + * Requires PHP 8.2+ |
| 32 | + */ |
| 33 | +trait HasSchemaVersion |
| 34 | +{ |
29 | 35 | /**
|
30 |
| - * Migrate document to the current schema version. |
| 36 | + * This method should be implemented in the model to migrate a document from |
| 37 | + * an older schema version to the current schema version. |
31 | 38 | */
|
32 | 39 | public function migrateSchema(int $fromVersion): void
|
33 | 40 | {
|
34 | 41 | }
|
35 | 42 |
|
36 |
| - /** |
37 |
| - * migrate schema and set current model version |
38 |
| - * @return void |
39 |
| - */ |
40 |
| - public function upgradeSchemaVersion(): void |
| 43 | + public static function bootHasSchemaVersion(): void |
41 | 44 | {
|
42 |
| - $version = $this->getSchemaVersion(); |
| 45 | + static::saving(function ($model) { |
| 46 | + if ($model->getAttribute($model::getSchemaVersionKey()) === null) { |
| 47 | + $model->setAttribute($model::getSchemaVersionKey(), $model->getModelSchemaVersion()); |
| 48 | + } |
| 49 | + }); |
43 | 50 |
|
44 |
| - if ($version && $version < $this->currentSchemaVersion) { |
45 |
| - $this->migrateSchema($version); |
46 |
| - $this->setSchemaVersion($this->currentSchemaVersion); |
47 |
| - } |
| 51 | + static::retrieved(function (self $model) { |
| 52 | + $version = $model->getSchemaVersion(); |
| 53 | + |
| 54 | + if ($version < $model->getModelSchemaVersion()) { |
| 55 | + $model->migrateSchema($version); |
| 56 | + $model->setAttribute($model::getSchemaVersionKey(), $model->getModelSchemaVersion()); |
| 57 | + } |
| 58 | + }); |
48 | 59 | }
|
49 | 60 |
|
50 | 61 | /**
|
51 |
| - * Get Current document version |
52 |
| - * |
53 |
| - * @return int |
| 62 | + * Get Current document version, fallback to 0 if not set |
54 | 63 | */
|
55 | 64 | public function getSchemaVersion(): int
|
56 | 65 | {
|
57 | 66 | return $this->{static::getSchemaVersionKey()} ?? 0;
|
58 | 67 | }
|
59 | 68 |
|
60 |
| - /** |
61 |
| - * @param int $version |
62 |
| - * @return void |
63 |
| - */ |
64 |
| - public function setSchemaVersion(int $version): void |
| 69 | + protected static function getSchemaVersionKey(): string |
65 | 70 | {
|
66 |
| - $this->{static::getSchemaVersionKey()} = $version; |
| 71 | + return 'schema_version'; |
67 | 72 | }
|
68 | 73 |
|
69 |
| - /** |
70 |
| - * Get document version key |
71 |
| - * |
72 |
| - * @return string |
73 |
| - */ |
74 |
| - public static function getSchemaVersionKey(): string |
| 74 | + protected function getModelSchemaVersion(): int |
75 | 75 | {
|
76 |
| - return 'schema_version'; |
| 76 | + try { |
| 77 | + return $this::SCHEMA_VERSION; |
| 78 | + } catch (Error) { |
| 79 | + throw new LogicException(sprintf('Constant %s::SCHEMA_VERSION is required when using HasSchemaVersion', $this::class)); |
| 80 | + } |
77 | 81 | }
|
78 | 82 | }
|
0 commit comments