|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * Copyright (c) 2017-2018 Tobias Reich |
| 6 | + * Copyright (c) 2018-2025 LycheeOrg. |
| 7 | + */ |
| 8 | + |
| 9 | +namespace App\Console\Commands; |
| 10 | + |
| 11 | +use App\Jobs\RecomputeAlbumStatsJob; |
| 12 | +use App\Models\Album; |
| 13 | +use Illuminate\Console\Command; |
| 14 | +use Illuminate\Support\Facades\Log; |
| 15 | + |
| 16 | +class BackfillAlbumFields extends Command |
| 17 | +{ |
| 18 | + /** |
| 19 | + * The name and signature of the console command. |
| 20 | + * |
| 21 | + * @var string |
| 22 | + */ |
| 23 | + protected $signature = 'lychee:backfill-album-fields |
| 24 | + {--dry-run : Preview without making changes} |
| 25 | + {--chunk=1000 : Number of albums to process per batch}'; |
| 26 | + |
| 27 | + /** |
| 28 | + * The console command description. |
| 29 | + * |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + protected $description = 'Backfill computed fields (min/max taken_at, num_children, num_photos, cover IDs) for all albums'; |
| 33 | + |
| 34 | + /** |
| 35 | + * Execute the console command. |
| 36 | + */ |
| 37 | + public function handle(): int |
| 38 | + { |
| 39 | + $dry_run = $this->option('dry-run'); |
| 40 | + $chunk_size = (int) $this->option('chunk'); |
| 41 | + |
| 42 | + if ($chunk_size < 1) { |
| 43 | + $this->error('Chunk size must be at least 1'); |
| 44 | + |
| 45 | + return Command::FAILURE; |
| 46 | + } |
| 47 | + |
| 48 | + $this->info('Starting album fields backfill...'); |
| 49 | + if ($dry_run) { |
| 50 | + $this->warn('DRY RUN MODE - No changes will be made'); |
| 51 | + } |
| 52 | + |
| 53 | + // Get total count |
| 54 | + $total = Album::query()->count(); |
| 55 | + $this->info("Found {$total} albums to process"); |
| 56 | + |
| 57 | + if ($total === 0) { |
| 58 | + $this->info('No albums to process'); |
| 59 | + |
| 60 | + return Command::SUCCESS; |
| 61 | + } |
| 62 | + |
| 63 | + // Process albums ordered by _lft ASC (leaf-to-root order) |
| 64 | + // This ensures child albums are computed before parents |
| 65 | + $bar = $this->output->createProgressBar($total); |
| 66 | + $bar->start(); |
| 67 | + |
| 68 | + $processed = 0; |
| 69 | + |
| 70 | + Album::query() |
| 71 | + ->orderBy('_lft', 'asc') |
| 72 | + ->chunk($chunk_size, function (\Illuminate\Database\Eloquent\Collection $albums) use ($dry_run, &$processed, $bar): void { |
| 73 | + /** @var Album $album */ |
| 74 | + foreach ($albums as $album) { |
| 75 | + if (!$dry_run) { |
| 76 | + // Dispatch job to recompute stats for this album |
| 77 | + RecomputeAlbumStatsJob::dispatch($album->id); |
| 78 | + } |
| 79 | + |
| 80 | + $processed++; |
| 81 | + $bar->advance(); |
| 82 | + |
| 83 | + // Log progress at intervals |
| 84 | + if ($processed % 100 === 0) { |
| 85 | + $percentage = round(($processed / $bar->getMaxSteps()) * 100, 2); |
| 86 | + Log::info("Backfilled {$processed}/{$bar->getMaxSteps()} albums ({$percentage}%)"); |
| 87 | + } |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + $bar->finish(); |
| 92 | + $this->newLine(2); |
| 93 | + |
| 94 | + if ($dry_run) { |
| 95 | + $this->info("DRY RUN: Would have dispatched {$processed} jobs"); |
| 96 | + } else { |
| 97 | + $this->info("Dispatched {$processed} jobs to recompute album stats"); |
| 98 | + $this->info('Jobs will be processed by the queue worker'); |
| 99 | + $this->warn('Note: This operation may take some time for large galleries'); |
| 100 | + } |
| 101 | + |
| 102 | + Log::info("Backfill completed: {$processed} albums processed"); |
| 103 | + |
| 104 | + return Command::SUCCESS; |
| 105 | + } |
| 106 | +} |
0 commit comments