fix: no need to pre-calculate playlist song order

This commit is contained in:
Phan An 2024-07-11 19:24:40 +02:00
parent 9f414be1f3
commit a84dba5e50

View file

@ -2,7 +2,6 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
@ -12,18 +11,5 @@ return new class extends Migration
Schema::table('playlist_song', static function (Blueprint $table): void {
$table->unsignedInteger('position')->index()->default(0);
});
DB::table('playlists')->orderBy('id')->chunk(100, static function ($playlists): void {
foreach ($playlists as $playlist) {
DB::table('playlist_song')
->where('playlist_id', $playlist->id)
->update([
'position' => DB::raw("(SELECT COUNT(id)
FROM playlist_song p
WHERE p.playlist_id = '$playlist->id'
AND p.id <= playlist_song.id)"),
]);
}
});
}
};