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)); +});