Skip to content

Commit f561f1c

Browse files
Merge pull request ProAI#8 from garret-gunter/fix/no-extra-versions
Only add version when saving versioned fields
2 parents bfc1670 + 54008a2 commit f561f1c

File tree

7 files changed

+140
-14
lines changed

7 files changed

+140
-14
lines changed

src/BuilderTrait.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ public function update(array $values) {
119119
// get records
120120
$affectedRecords = $this->getAffectedRecords();
121121

122+
// only update main table if not updating versioned fields.
123+
if (empty($versionValues)) {
124+
return $this->toBase()->update($values);
125+
}
126+
122127
// update main table records
123128
if (! $this->query->increment($this->model->getLatestVersionColumn(), 1, $values)) {
124129
return false;

tests/Models/Comment.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace ProAI\Versioning\Tests\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use ProAI\Versioning\Versionable;
7+
8+
/**
9+
* Class Comment
10+
*
11+
* A model without versioned timestamps.
12+
*
13+
* @package ProAI\Versioning\Tests\Models
14+
*
15+
* @property int $id
16+
* @property string $latest_version
17+
* @property string $content
18+
* @property string $title
19+
* @property \Carbon\Carbon|null $created_at
20+
* @property \Carbon\Carbon|null $updated_at
21+
*
22+
* @mixin \Illuminate\Database\Eloquent\Builder
23+
*/
24+
class Comment extends Model {
25+
use Versionable;
26+
27+
/**
28+
* The attributes that are mass assignable.
29+
*
30+
* @var array
31+
*/
32+
protected $fillable = [
33+
'content', 'latest_version', 'title',
34+
];
35+
36+
/**
37+
* Indicates if the model should be timestamped.
38+
*
39+
* @var bool
40+
*/
41+
public $timestamps = true;
42+
43+
public $versioned = ['content'];
44+
}

tests/Models/Post.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace ProAI\Versioning\Tests\Models;
44

5-
use Illuminate\Foundation\Auth\User as Authenticatable;
5+
use Illuminate\Database\Eloquent\Model;
66
use ProAI\Versioning\Versionable;
77
use ProAI\Versioning\SoftDeletes;
88

@@ -19,7 +19,7 @@
1919
*
2020
* @mixin \Illuminate\Database\Eloquent\Builder
2121
*/
22-
class Post extends Authenticatable {
22+
class Post extends Model {
2323
use Versionable;
2424
use SoftDeletes;
2525

tests/Unit/BuilderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ public function itWillThrowExceptionWhenKeysAreTooLong(): void {
206206

207207
$builder = $model->newQuery();
208208
$builder->update([
209-
'a.long.key' => 'Citadel',
210-
]);
209+
'a.long.key' => 'Citadel',
210+
]);
211211
}
212212

213213
/**

tests/Unit/VersionableTest.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ProAI\Versioning\Tests\Unit;
44

55
use Carbon\Carbon;
6+
use ProAI\Versioning\Tests\Models\Comment;
67
use ProAI\Versioning\Tests\Models\User;
78
use ProAI\Versioning\Tests\TestCase;
89

@@ -159,30 +160,48 @@ public function itWillUpdateTheLatestVersionWhenSaving(): void {
159160
* @test
160161
*/
161162
public function itWillOnlyVersionVersionedAttributes(): void {
162-
/** @var User $model */
163-
$model = factory(User::class)->create([]);
164-
$email = $model->email;
163+
/** @var Comment $model */
164+
$model = factory(Comment::class)->create(
165+
[
166+
'title' => 'Some kind of lorem impsum should go here',
167+
]
168+
);
169+
$originalContent = $model->content;
170+
171+
$newContent = 'I approve of this comment.';
172+
$model->content = $newContent;
173+
$model->save();
165174

166-
$model->email = '[email protected]';
167-
$model->username = 'RickSanchez';
175+
$newTitle = 'Not lorem ipsum';
176+
$model->title = $newTitle;
168177
$model->save();
169178

170179
$this->assertDatabaseHas($model->getTable(), [
171-
'username' => 'RickSanchez',
180+
'title' => $newTitle,
172181
]);
173182

174183
$this->assertDatabaseHas($model->getVersionTable(), [
175184
'ref_id' => $model->id,
176185
'version' => 1,
177-
'email' => $email,
178-
'city' => $model->city
186+
'content' => $originalContent,
179187
]);
180188

181189
$this->assertDatabaseHas($model->getVersionTable(), [
182190
'ref_id' => $model->id,
183191
'version' => 2,
184-
'email' => $model->email,
185-
'city' => $model->city
192+
'content' => $newContent,
193+
]);
194+
195+
// Latest version should be 2.
196+
$this->assertDatabaseHas($model->getTable(), [
197+
'title' => $newTitle,
198+
'latest_version' => 2,
199+
]);
200+
201+
// A 3rd version should not exist.
202+
$this->assertDatabaseMissing($model->getVersionTable(), [
203+
['ref_id', $model->id,],
204+
['version', '>=', 3,],
186205
]);
187206
}
188207
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use Faker\Generator as Faker;
4+
use ProAI\Versioning\Tests\Models\Comment;
5+
6+
/*
7+
|--------------------------------------------------------------------------
8+
| Comment Factories
9+
|--------------------------------------------------------------------------
10+
|
11+
*/
12+
/** @var \Illuminate\Database\Eloquent\Factory $factory */
13+
$factory->define(Comment::class, static function (Faker $faker) {
14+
return [
15+
'title' => $faker->sentence,
16+
'content' => $faker->text,
17+
];
18+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateCommentsTable extends Migration {
8+
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up(): void {
15+
Schema::create('comments', static function (Blueprint $table) {
16+
$table->increments('id');
17+
$table->integer('latest_version');
18+
$table->text('title');
19+
$table->timestamps();
20+
});
21+
22+
Schema::create('comments_version', static function (Blueprint $table) {
23+
$table->integer('ref_id')->unsigned();
24+
$table->integer('version')->unsigned();
25+
$table->text('content');
26+
27+
$table->primary(['ref_id', 'version']);
28+
});
29+
}
30+
31+
/**
32+
* Reverse the migrations.
33+
*
34+
* @return void
35+
*/
36+
public function down(): void {
37+
Schema::dropIfExists('comments');
38+
Schema::dropIfExists('comments_version');
39+
}
40+
}

0 commit comments

Comments
 (0)