mirror of
https://github.com/koel/koel
synced 2024-11-10 06:34:14 +00:00
fix: incorrect deleteWhereValueNotIn behavior
This commit is contained in:
parent
9d9dc0b397
commit
bbbf270965
1 changed files with 6 additions and 11 deletions
|
@ -19,29 +19,24 @@ trait SupportsDeleteWhereValueNotIn
|
||||||
*/
|
*/
|
||||||
public static function deleteWhereValueNotIn(array $values, string $field = 'id'): void
|
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) {
|
if (count($values) <= $maxChunkSize) {
|
||||||
static::query()->whereNotIn($field, $values)->delete();
|
static::query()->whereNotIn($field, $values)->delete();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, we get the actual IDs that should be deleted…
|
$allIds = static::query()->select($field)->get()->pluck($field)->all();
|
||||||
$allIDs = static::query()->select($field)->get()->pluck($field)->all();
|
$deletableIds = array_diff($allIds, $values);
|
||||||
$whereInIDs = array_diff($allIDs, $values);
|
|
||||||
|
|
||||||
// …and see if we can delete them instead.
|
if (count($deletableIds) < $maxChunkSize) {
|
||||||
if (count($whereInIDs) < $maxChunkSize) {
|
static::query()->whereIn($field, $deletableIds)->delete();
|
||||||
static::query()->whereIn($field, $whereInIDs)->delete();
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If that's not possible (i.e. this array has more than maxChunkSize elements, too)
|
static::deleteByChunk($deletableIds, $field, $maxChunkSize);
|
||||||
// then we'll delete chunk by chunk.
|
|
||||||
static::deleteByChunk($values, $field, $maxChunkSize);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function deleteByChunk(array $values, string $field = 'id', int $chunkSize = 65535): void
|
public static function deleteByChunk(array $values, string $field = 'id', int $chunkSize = 65535): void
|
||||||
|
|
Loading…
Reference in a new issue