From 319692b20c3115ea7f91fdf8eae178cea91fd34f Mon Sep 17 00:00:00 2001 From: Luke Kuzmish Date: Sat, 15 Jul 2023 11:34:16 -0400 Subject: [PATCH] always set the updated_at column when marking conversions generated --- src/MediaCollections/Models/Media.php | 13 +++++++++++-- .../Commands/RegenerateCommandTest.php | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/MediaCollections/Models/Media.php b/src/MediaCollections/Models/Media.php index 112698752..d9f9a7304 100644 --- a/src/MediaCollections/Models/Media.php +++ b/src/MediaCollections/Models/Media.php @@ -267,7 +267,7 @@ public function markAsConversionGenerated(string $conversionName): self $this->generated_conversions = $generatedConversions; - $this->save(); + $this->saveOrTouch(); return $this; } @@ -280,7 +280,7 @@ public function markAsConversionNotGenerated(string $conversionName): self $this->generated_conversions = $generatedConversions; - $this->save(); + $this->saveOrTouch(); return $this; } @@ -452,4 +452,13 @@ public function toMailAttachment(): Attachment { return $this->mailAttachment(); } + + protected function saveOrTouch(): bool + { + if (! $this->exists || $this->isDirty()) { + return $this->save(); + } + + return $this->touch(); + } } diff --git a/tests/Conversions/Commands/RegenerateCommandTest.php b/tests/Conversions/Commands/RegenerateCommandTest.php index 4a482d098..1e34ac416 100644 --- a/tests/Conversions/Commands/RegenerateCommandTest.php +++ b/tests/Conversions/Commands/RegenerateCommandTest.php @@ -349,3 +349,18 @@ expect($derivedImage2)->toBeFile(); expect($derivedImage3)->toBeFile(); }); + +it('can set updated_at column when regenerating', function () { + $this->travelTo('2020-01-01 00:00:00'); + $media = $this->testModelWithConversion + ->addMedia($this->getTestFilesDirectory('test.jpg')) + ->toMediaCollection('images'); + + $this->travelBack(); + + $this->artisan('media-library:regenerate'); + + $media->refresh(); + + expect($media->updated_at)->toBeGreaterThanOrEqual(now()->subSeconds(5)); +});