feat: add many playlist for music
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
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
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('music_playlist', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('music_id')->constrained('music')->cascadeOnDelete();
|
||||
$table->foreignId('playlist_id')->constrained('music_playlists')->cascadeOnDelete();
|
||||
$table->integer('order')->default(0);
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['music_id', 'playlist_id']);
|
||||
});
|
||||
|
||||
// Backfill the pivot from the existing single playlist_id column.
|
||||
if (Schema::hasColumn('music', 'playlist_id')) {
|
||||
DB::table('music')
|
||||
->whereNotNull('playlist_id')
|
||||
->orderBy('id')
|
||||
->select('id', 'playlist_id', 'order')
|
||||
->chunk(200, function ($rows) {
|
||||
$now = now();
|
||||
$insert = $rows->map(fn ($row) => [
|
||||
'music_id' => $row->id,
|
||||
'playlist_id' => $row->playlist_id,
|
||||
'order' => $row->order ?? 0,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
])->all();
|
||||
|
||||
DB::table('music_playlist')->insertOrIgnore($insert);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('music_playlist');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('music', function (Blueprint $table) {
|
||||
$table->dropForeign(['playlist_id']);
|
||||
$table->dropColumn('playlist_id');
|
||||
$table->dropColumn('order');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('music', function (Blueprint $table) {
|
||||
$table->foreignId('playlist_id')->nullable()->after('type')
|
||||
->constrained('music_playlists')->nullOnDelete();
|
||||
$table->integer('order')->default(0)->after('duration');
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user