legacy: privatize-media überspringt unmigrierte Legacy-Bildzeilen, --prune-missing löscht sie

Der Legacy-Import legt pro Alt-Bild eine Zeile mit rohem Legacy-Dateinamen
als path an; legacy:migrate-media biegt sie auf die echte Datei um. Zeilen,
deren Datei im Alt-System verloren ging (Live-Befund: ~22k bei Presseecho),
blieben als disk=public-Leichen liegen und ließen den Privatizer mit
RuntimeException abbrechen. Jetzt: Skip + Warnhinweis statt Abbruch;
--prune-missing löscht solche Zeilen endgültig, wenn ihre Datei auf keiner
Disk existiert. Der Integritäts-Assert für migrierte Pfade bleibt.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Kevin Adametz 2026-07-20 10:50:31 +00:00
parent a1302994c5
commit 3151b1f38a
3 changed files with 115 additions and 6 deletions

View file

@ -12,7 +12,8 @@ class PrivatizePressReleaseMedia extends Command
* *
* @var string * @var string
*/ */
protected $signature = 'press-releases:privatize-media'; protected $signature = 'press-releases:privatize-media
{--prune-missing : Unmigrierte Legacy-Bildzeilen ohne Datei endgültig löschen}';
/** /**
* The console command description. * The console command description.
@ -26,7 +27,7 @@ class PrivatizePressReleaseMedia extends Command
*/ */
public function handle(PressReleaseMediaPrivatizer $privatizer): int public function handle(PressReleaseMediaPrivatizer $privatizer): int
{ {
$counts = $privatizer->privatizeAll(); $counts = $privatizer->privatizeAll((bool) $this->option('prune-missing'));
$this->info(sprintf( $this->info(sprintf(
'%d Bilder und %d Anhänge wurden auf den privaten Datenträger migriert.', '%d Bilder und %d Anhänge wurden auf den privaten Datenträger migriert.',
@ -34,6 +35,20 @@ class PrivatizePressReleaseMedia extends Command
$counts['attachments'], $counts['attachments'],
)); ));
if ($counts['legacy_pruned'] > 0) {
$this->info(sprintf(
'%d unmigrierte Legacy-Bildzeilen ohne Datei wurden gelöscht.',
$counts['legacy_pruned'],
));
}
if ($counts['legacy_skipped'] > 0) {
$this->warn(sprintf(
'%d unmigrierte Legacy-Bildzeilen übersprungen — erst `legacy:migrate-media` ausführen, danach Verbliebene ohne Datei mit `--prune-missing` löschen.',
$counts['legacy_skipped'],
));
}
return self::SUCCESS; return self::SUCCESS;
} }
} }

View file

@ -5,26 +5,43 @@ namespace App\Services\PressRelease;
use App\Models\PressReleaseAttachment; use App\Models\PressReleaseAttachment;
use App\Models\PressReleaseImage; use App\Models\PressReleaseImage;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use RuntimeException; use RuntimeException;
class PressReleaseMediaPrivatizer class PressReleaseMediaPrivatizer
{ {
/** /**
* @return array{images:int,attachments:int} * Der Legacy-Import legt Bildzeilen mit dem rohen Alt-Dateinamen als `path`
* an; erst `legacy:migrate-media` biegt sie auf `press-releases/…` um.
* Zeilen, die danach noch den Alt-Pfad tragen, haben keine Quelldatei mehr —
* sie werden hier übersprungen bzw. mit `$pruneMissing` endgültig gelöscht.
*
* @return array{images:int,attachments:int,legacy_skipped:int,legacy_pruned:int}
*/ */
public function privatizeAll(): array public function privatizeAll(bool $pruneMissing = false): array
{ {
$counts = ['images' => 0, 'attachments' => 0]; $counts = ['images' => 0, 'attachments' => 0, 'legacy_skipped' => 0, 'legacy_pruned' => 0];
PressReleaseImage::query() PressReleaseImage::query()
->withTrashed() ->withTrashed()
->where('disk', 'public') ->where('disk', 'public')
->chunkById(100, function ($images) use (&$counts): void { ->chunkById(100, function ($images) use (&$counts, $pruneMissing): void {
foreach ($images as $image) { foreach ($images as $image) {
$paths = collect([$image->path, ...array_values($image->variants ?? [])]) $paths = collect([$image->path, ...array_values($image->variants ?? [])])
->filter(fn ($path): bool => is_string($path) && filled($path)) ->filter(fn ($path): bool => is_string($path) && filled($path))
->unique(); ->unique();
if ($this->isUnmigratedLegacyImage($image)) {
if ($pruneMissing && ! $this->anyPathExists($paths->all())) {
$image->forceDelete();
$counts['legacy_pruned']++;
} else {
$counts['legacy_skipped']++;
}
continue;
}
$this->assertPathsAvailable($paths->all()); $this->assertPathsAvailable($paths->all());
foreach ($paths as $path) { foreach ($paths as $path) {
@ -51,6 +68,29 @@ class PressReleaseMediaPrivatizer
return $counts; return $counts;
} }
private function isUnmigratedLegacyImage(PressReleaseImage $image): bool
{
return $image->legacy_id !== null
&& ! Str::startsWith((string) $image->path, 'press-releases/');
}
/**
* @param list<string> $paths
*/
private function anyPathExists(array $paths): bool
{
$public = Storage::disk('public');
$private = Storage::disk('local');
foreach ($paths as $path) {
if ($public->exists($path) || $private->exists($path)) {
return true;
}
}
return false;
}
/** /**
* @param list<string> $paths * @param list<string> $paths
*/ */

View file

@ -1,6 +1,7 @@
<?php <?php
use App\Models\PressRelease; use App\Models\PressRelease;
use App\Models\PressReleaseImage;
use App\Services\PressRelease\PressReleaseMediaPrivatizer; use App\Services\PressRelease\PressReleaseMediaPrivatizer;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
@ -40,6 +41,59 @@ test('existing public press release media is moved to private storage', function
->and($attachment->fresh()->disk)->toBe('local'); ->and($attachment->fresh()->disk)->toBe('local');
}); });
test('unmigrated legacy image rows are skipped instead of aborting the run', function () {
Storage::fake('public');
Storage::fake('local');
$pressRelease = PressRelease::factory()->create();
$legacyImage = $pressRelease->images()->create([
'disk' => 'public',
'path' => 'b0cde51ff6799356bfaa8ec4c80e8921.jpg',
'legacy_portal' => 'presseecho',
'legacy_id' => 33683,
]);
$migratedImage = $pressRelease->images()->create([
'disk' => 'public',
'path' => "press-releases/{$pressRelease->id}/images/cover.jpg",
]);
Storage::disk('public')->put($migratedImage->path, 'content');
$this->artisan('press-releases:privatize-media')
->expectsOutputToContain('1 unmigrierte Legacy-Bildzeilen übersprungen')
->assertSuccessful();
expect($legacyImage->fresh())->not->toBeNull()
->and($legacyImage->fresh()->disk)->toBe('public')
->and($migratedImage->fresh()->disk)->toBe('local');
});
test('prune-missing deletes unmigrated legacy image rows only when no file exists', function () {
Storage::fake('public');
Storage::fake('local');
$pressRelease = PressRelease::factory()->create();
$lostImage = $pressRelease->images()->create([
'disk' => 'public',
'path' => 'b0cde51ff6799356bfaa8ec4c80e8921.jpg',
'legacy_portal' => 'presseecho',
'legacy_id' => 33683,
]);
$recoverableImage = $pressRelease->images()->create([
'disk' => 'public',
'path' => 'noch-vorhanden.jpg',
'legacy_portal' => 'presseecho',
'legacy_id' => 33684,
]);
Storage::disk('public')->put($recoverableImage->path, 'content');
$this->artisan('press-releases:privatize-media', ['--prune-missing' => true])
->expectsOutputToContain('1 unmigrierte Legacy-Bildzeilen ohne Datei wurden gelöscht.')
->expectsOutputToContain('1 unmigrierte Legacy-Bildzeilen übersprungen')
->assertSuccessful();
expect(PressReleaseImage::withTrashed()->find($lostImage->id))->toBeNull()
->and($recoverableImage->fresh())->not->toBeNull()
->and($recoverableImage->fresh()->disk)->toBe('public');
});
test('a missing source file never flips its database record to the private disk', function () { test('a missing source file never flips its database record to the private disk', function () {
Storage::fake('public'); Storage::fake('public');
Storage::fake('local'); Storage::fake('local');