mirror of
https://github.com/koel/koel
synced 2024-11-10 06:34:14 +00:00
feat: guess table key name instead of hard-coding "id"
This commit is contained in:
parent
48f6bcc105
commit
4182411881
2 changed files with 9 additions and 5 deletions
|
@ -17,9 +17,11 @@ trait SupportsDeleteWhereValueNotIn
|
|||
/**
|
||||
* Deletes all records whose certain value is not in an array.
|
||||
*/
|
||||
public static function deleteWhereValueNotIn(array $values, string $field = 'id'): void
|
||||
public static function deleteWhereValueNotIn(array $values, ?string $field = null): void
|
||||
{
|
||||
$maxChunkSize = DB::getDriverName() === 'sqlite' ? 999 : 65535;
|
||||
$field ??= (new static())->getKeyName();
|
||||
|
||||
$maxChunkSize = DB::getDriverName() === 'sqlite' ? 999 : 65_535;
|
||||
|
||||
if (count($values) <= $maxChunkSize) {
|
||||
static::query()->whereNotIn($field, $values)->delete();
|
||||
|
@ -36,11 +38,13 @@ trait SupportsDeleteWhereValueNotIn
|
|||
return;
|
||||
}
|
||||
|
||||
static::deleteByChunk($deletableIds, $field, $maxChunkSize);
|
||||
static::deleteByChunk($deletableIds, $maxChunkSize, $field);
|
||||
}
|
||||
|
||||
public static function deleteByChunk(array $values, string $field = 'id', int $chunkSize = 65535): void
|
||||
public static function deleteByChunk(array $values, int $chunkSize = 65_535, ?string $field = null): void
|
||||
{
|
||||
$field ??= (new static())->getKeyName();
|
||||
|
||||
DB::transaction(static function () use ($values, $field, $chunkSize): void {
|
||||
foreach (array_chunk($values, $chunkSize) as $chunk) {
|
||||
static::query()->whereIn($field, $chunk)->delete();
|
||||
|
|
|
@ -323,7 +323,7 @@ class SongTest extends TestCase
|
|||
self::assertNotSame(0, Song::query()->count());
|
||||
$ids = Song::query()->select('id')->get()->pluck('id')->all();
|
||||
|
||||
Song::deleteByChunk($ids, 'id', 1);
|
||||
Song::deleteByChunk($ids, 1);
|
||||
|
||||
self::assertSame(0, Song::query()->count());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue