fix: db
This commit is contained in:
@@ -40,55 +40,70 @@ class PruneNowalletPhotosCommand extends Command
|
||||
$q = Photo::query()->whereNotIn('device_id', $keepIds);
|
||||
$this->applyBefore($q, $before);
|
||||
|
||||
$prunePhotoCount = (clone $q)->count();
|
||||
$deviceIds = (clone $q)->distinct()->pluck('device_id');
|
||||
$keepPhotoCount = Photo::query()->whereIn('device_id', $keepIds)->count();
|
||||
|
||||
$this->info(sprintf(
|
||||
'%s keep_devices=%d keep_photos=%d prune_devices=%d prune_photos=%d%s',
|
||||
'%s keep_devices=%d streaming batches of 500 (no pre-count)%s',
|
||||
$execute ? 'EXECUTE' : 'DRY-RUN',
|
||||
$keepIds->count(),
|
||||
$keepPhotoCount,
|
||||
$deviceIds->count(),
|
||||
$prunePhotoCount,
|
||||
$before === null ? '' : ' '.$this->beforeLabel($before),
|
||||
));
|
||||
|
||||
if ($prunePhotoCount === 0) {
|
||||
$disk = Storage::disk('local');
|
||||
$seen = 0;
|
||||
$deletedRows = 0;
|
||||
$deletedFiles = 0;
|
||||
$touched = [];
|
||||
|
||||
$lastId = 0;
|
||||
while (true) {
|
||||
$photos = (clone $q)
|
||||
->select(['photos.id', 'photos.device_id', 'photos.path'])
|
||||
->where('photos.id', '>', $lastId)
|
||||
->orderBy('photos.id')
|
||||
->limit(500)
|
||||
->get();
|
||||
if ($photos->isEmpty()) {
|
||||
break;
|
||||
}
|
||||
$ids = [];
|
||||
foreach ($photos as $photo) {
|
||||
$ids[] = $photo->id;
|
||||
$touched[$photo->device_id] = true;
|
||||
$seen++;
|
||||
if (! $execute) {
|
||||
continue;
|
||||
}
|
||||
$path = trim((string) ($photo->path ?? ''));
|
||||
if ($path !== '' && $disk->delete($path)) {
|
||||
$deletedFiles++;
|
||||
}
|
||||
}
|
||||
if ($execute && $ids !== []) {
|
||||
DB::table('photo_reads')->whereIn('photo_id', $ids)->delete();
|
||||
Photo::query()->whereIn('id', $ids)->delete();
|
||||
$deletedRows += count($ids);
|
||||
}
|
||||
$lastId = (int) $photos->last()->id;
|
||||
$this->info($execute
|
||||
? sprintf(' deleted_rows=%d deleted_files=%d', $deletedRows, $deletedFiles)
|
||||
: sprintf(' matched=%d', $seen));
|
||||
}
|
||||
|
||||
if ($seen === 0) {
|
||||
$this->info('nothing to prune');
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
if (! $execute) {
|
||||
$this->warn('dry-run only; pass --execute to delete');
|
||||
$this->info(sprintf(
|
||||
'dry-run matched_photos=%d prune_devices=%d; pass --execute to delete',
|
||||
$seen,
|
||||
count($touched),
|
||||
));
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
$disk = Storage::disk('local');
|
||||
$deletedRows = 0;
|
||||
$deletedFiles = 0;
|
||||
$touched = [];
|
||||
|
||||
(clone $q)->select(['photos.id', 'photos.device_id', 'photos.path'])
|
||||
->orderBy('photos.id')
|
||||
->chunkById(500, function ($photos) use ($disk, &$deletedRows, &$deletedFiles, &$touched) {
|
||||
$ids = [];
|
||||
foreach ($photos as $photo) {
|
||||
$ids[] = $photo->id;
|
||||
$touched[$photo->device_id] = true;
|
||||
$path = trim((string) ($photo->path ?? ''));
|
||||
if ($path !== '' && $disk->exists($path)) {
|
||||
$disk->delete($path);
|
||||
$deletedFiles++;
|
||||
}
|
||||
}
|
||||
DB::table('photo_reads')->whereIn('photo_id', $ids)->delete();
|
||||
Photo::query()->whereIn('id', $ids)->delete();
|
||||
$deletedRows += count($ids);
|
||||
}, 'photos.id', 'id');
|
||||
|
||||
$clearedDirs = 0;
|
||||
foreach (array_keys($touched) as $deviceId) {
|
||||
$device = Device::query()->find($deviceId);
|
||||
|
||||
Reference in New Issue
Block a user