Skip to content

Commit

Permalink
Merge pull request #3 from avocet-shores/feature/fix-fastforward-to-s…
Browse files Browse the repository at this point in the history
…napshot-bug

Fix snapshot/fast-forward bug
  • Loading branch information
jared-cannon authored Jan 16, 2025
2 parents 619a5dc + a4edd23 commit 8efc693
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
4 changes: 1 addition & 3 deletions src/LaravelRewindServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public function configurePackage(Package $package): void

public function registeringPackage(): void
{
$this->app->singleton('laravel-rewind-manager', function () {
return $this->app->make(RewindManager::class);
});
$this->app->bind('laravel-rewind-manager', RewindManager::class);
}
}
1 change: 0 additions & 1 deletion src/Services/RewindManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ protected function applyDiffs($model, int $currentVersion, int $targetVersion):
for ($ver = $currentVersion + 1; $ver <= $targetVersion; $ver++) {
$versionRec = $model->versions
->where('version', $ver)
->where('is_snapshot', false)
->first();

// If there's no partial diff for $ver (e.g. if it was a snapshot or doesn't exist), skip
Expand Down
43 changes: 43 additions & 0 deletions tests/Feature/RewindManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,49 @@
Rewind::goTo($post, 2);
})->throws(VersionDoesNotExistException::class);

it('is able to fast-forward to a snapshot', function () {
app()->config->set('rewind.snapshot_interval', 3);

// Arrange
$post = Post::create([
'user_id' => $this->user->id,
'title' => 'Original Title',
'body' => 'Original Body',
]);

$post->refresh();

// Assert the model has current_version set to 1
$this->assertSame(1, $post->current_version);

$post->update([
'title' => 'Updated Title',
'body' => 'Updated Body',
]);

$this->assertSame(2, $post->current_version);

// Snapshot
$post->update([
'title' => 'Updated Title Again',
'body' => 'Updated Body Again',
]);

$this->assertSame(3, $post->current_version);

Rewind::rewind($post);

$this->assertSame(2, $post->current_version);

// Act: Fast-forward to the snapshot
Rewind::fastForward($post);

// Assert: The model should be at the snapshot version
$this->assertSame(3, $post->current_version);
$this->assertSame('Updated Title Again', $post->title);
$this->assertSame('Updated Body Again', $post->body);
});

it('throws an exception when we try to rewind before version 1', function () {
// Arrange
$post = Post::create([
Expand Down

0 comments on commit 8efc693

Please sign in to comment.