From bbbf2709652277a2a17a6d51bd511bbfef5de238 Mon Sep 17 00:00:00 2001 From: Phan An Date: Wed, 10 Aug 2022 08:57:20 +0200 Subject: [PATCH] fix: incorrect deleteWhereValueNotIn behavior --- app/Models/SupportsDeleteWhereValueNotIn.php | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/app/Models/SupportsDeleteWhereValueNotIn.php b/app/Models/SupportsDeleteWhereValueNotIn.php index bd383758..f43089d3 100644 --- a/app/Models/SupportsDeleteWhereValueNotIn.php +++ b/app/Models/SupportsDeleteWhereValueNotIn.php @@ -19,29 +19,24 @@ trait SupportsDeleteWhereValueNotIn */ public static function deleteWhereValueNotIn(array $values, string $field = 'id'): void { - $maxChunkSize = config('database.default') === 'sqlite-persistent' ? 999 : 65535; + $maxChunkSize = DB::getDriverName() === 'sqlite' ? 999 : 65535; - // If the number of entries is lower than, or equals to maxChunkSize, just go ahead. if (count($values) <= $maxChunkSize) { static::query()->whereNotIn($field, $values)->delete(); return; } - // Otherwise, we get the actual IDs that should be deleted… - $allIDs = static::query()->select($field)->get()->pluck($field)->all(); - $whereInIDs = array_diff($allIDs, $values); + $allIds = static::query()->select($field)->get()->pluck($field)->all(); + $deletableIds = array_diff($allIds, $values); - // …and see if we can delete them instead. - if (count($whereInIDs) < $maxChunkSize) { - static::query()->whereIn($field, $whereInIDs)->delete(); + if (count($deletableIds) < $maxChunkSize) { + static::query()->whereIn($field, $deletableIds)->delete(); return; } - // If that's not possible (i.e. this array has more than maxChunkSize elements, too) - // then we'll delete chunk by chunk. - static::deleteByChunk($values, $field, $maxChunkSize); + static::deleteByChunk($deletableIds, $field, $maxChunkSize); } public static function deleteByChunk(array $values, string $field = 'id', int $chunkSize = 65535): void