From ecfc62fc619e894ca70c511484f59b117f23e016 Mon Sep 17 00:00:00 2001 From: Danny Boy Date: Wed, 22 Mar 2023 22:25:41 +0900 Subject: [PATCH 1/3] create initial version --- README.md | 17 ++++++++ src/Mpociot/Versionable/VersionableTrait.php | 41 ++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/README.md b/README.md index 57f119d..fe2a3f5 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,23 @@ Every time you update your model, a new version containing the previous attribut All timestamps and the optional soft-delete timestamp will be ignored. + + +### Adding versions to existing data + +Versionable creates a version on update() of the *updated* model. So, if you're installing this on an already existing application, you may want to create a version of the current model: + +```php + $model->createInitialVersion(); +``` +If no version exists, this will create the initial version. + +If you want to do this for all instances of a model: + +```php + Model::initializeVersionOnAllRows(); +``` + ### Exclude attributes from versioning diff --git a/src/Mpociot/Versionable/VersionableTrait.php b/src/Mpociot/Versionable/VersionableTrait.php index f9ff17e..e4b45fe 100644 --- a/src/Mpociot/Versionable/VersionableTrait.php +++ b/src/Mpociot/Versionable/VersionableTrait.php @@ -189,6 +189,47 @@ protected function versionablePostSave() } } + + /** + * Initialize a version on every instance of a model + * @return void + */ + public static function initializeVersionOnAllRows() + { + foreach (self::all() as $obj) { + $obj->createInitialVersion(); + } + return true; + } + + /** + * Save a new version. + * @return void + */ + public function createInitialVersion() + { + if(true === $this->versions->isEmpty()) { + + $class = $this->getVersionClass(); + $version = new $class(); + $version->versionable_id = $this->getKey(); + $version->versionable_type = method_exists($this, 'getMorphClass') ? $this->getMorphClass() : get_class($this); + $version->user_id = $this->getAuthUserId(); + + $versionedHiddenFields = $this->versionedHiddenFields ?? []; + $this->makeVisible($versionedHiddenFields); + $version->model_data = serialize($this->attributesToArray()); + $this->makeHidden($versionedHiddenFields); + + if (!empty( $this->reason )) { + $version->reason = $this->reason; + } + + $version->save(); + } + } + + /** * Delete old versions of this model when they reach a specific count. * From 8410d8017765f94ccf1cc31bdc22f880c5b374ca Mon Sep 17 00:00:00 2001 From: Danny Boy Date: Thu, 23 Mar 2023 01:20:02 +0900 Subject: [PATCH 2/3] tests for initialize on current db --- src/Mpociot/Versionable/VersionableTrait.php | 3 +- tests/VersionableTest.php | 42 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/Mpociot/Versionable/VersionableTrait.php b/src/Mpociot/Versionable/VersionableTrait.php index e4b45fe..10ffd4b 100644 --- a/src/Mpociot/Versionable/VersionableTrait.php +++ b/src/Mpociot/Versionable/VersionableTrait.php @@ -199,7 +199,6 @@ public static function initializeVersionOnAllRows() foreach (self::all() as $obj) { $obj->createInitialVersion(); } - return true; } /** @@ -208,7 +207,7 @@ public static function initializeVersionOnAllRows() */ public function createInitialVersion() { - if(true === $this->versions->isEmpty()) { + if(true === $this->fresh()->versions->isEmpty()) { $class = $this->getVersionClass(); $version = new $class(); diff --git a/tests/VersionableTest.php b/tests/VersionableTest.php index e146f33..ff8004d 100644 --- a/tests/VersionableTest.php +++ b/tests/VersionableTest.php @@ -529,6 +529,48 @@ public function testWhereModelHasMorphMap() $this->assertEquals( $user->attributesToArray(), $version->getModel()->attributesToArray() ); Relation::morphMap([], false); } + + public function testAddVersionableToExistingUser() + { + $user = new \Illuminate\Foundation\Auth\User(); + $user->name = "Danny"; + $user->email = "danny.boy@bmail.php"; + $user->password = "12345"; + $user->save(); + + $this->assertNull($user->versions ); + + $user = TestVersionableUser::find($user->id); + $this->assertCount(0, $user->versions ); + $user->createInitialVersion(); + $this->assertCount(1, $user->fresh()->versions ); + + //ASSERT THAT createInitialVersion() ONLY WORKS ONCE + $user->createInitialVersion(); + $this->assertCount(1, $user->fresh()->versions ); + } + + public function testInitializeModel() + { + $user = new \Illuminate\Foundation\Auth\User(); + $user->name = "Danny"; + $user->email = "danny.boy@bmail.php"; + $user->password = "12345"; + $user->save(); + + $this->assertNull($user->versions ); + + $user = TestVersionableUser::find($user->id); + $this->assertCount(0, $user->versions ); + + TestVersionableUser::initializeVersionOnAllRows(); + $this->assertCount(1, $user->fresh()->versions ); + + //ASSERT THAT createInitialVersion() ONLY WORKS ONCE + TestVersionableUser::initializeVersionOnAllRows(); + $this->assertCount(1, $user->fresh()->versions ); + } + } From 667ef9498693097d1231a2517384b7bdd42176a9 Mon Sep 17 00:00:00 2001 From: Danny Boy Date: Thu, 23 Mar 2023 01:34:59 +0900 Subject: [PATCH 3/3] check for versioningEnabled on createInitialVersion --- src/Mpociot/Versionable/VersionableTrait.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Mpociot/Versionable/VersionableTrait.php b/src/Mpociot/Versionable/VersionableTrait.php index 10ffd4b..51e9209 100644 --- a/src/Mpociot/Versionable/VersionableTrait.php +++ b/src/Mpociot/Versionable/VersionableTrait.php @@ -207,7 +207,9 @@ public static function initializeVersionOnAllRows() */ public function createInitialVersion() { - if(true === $this->fresh()->versions->isEmpty()) { + if( true === $this->fresh()->versions->isEmpty() && + true === $this->versioningEnabled + ) { $class = $this->getVersionClass(); $version = new $class(); @@ -255,6 +257,7 @@ private function purgeOldVersions() /** * Determine if a new version should be created for this model. + * Checks if appropriate fields have been changed. * * @return bool */