feat: many category and sub can be in playlist
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
<?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_category_playlist', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('playlist_id')->constrained('music_playlists')->cascadeOnDelete();
|
||||
$table->foreignId('category_id')->constrained('music_categories')->cascadeOnDelete();
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['playlist_id', 'category_id']);
|
||||
});
|
||||
|
||||
Schema::create('music_subcategory_playlist', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('playlist_id')->constrained('music_playlists')->cascadeOnDelete();
|
||||
$table->foreignId('subcategory_id')->constrained('music_subcategories')->cascadeOnDelete();
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['playlist_id', 'subcategory_id']);
|
||||
});
|
||||
|
||||
// Backfill the new pivots from the existing single columns.
|
||||
if (Schema::hasColumn('music_playlists', 'category_id')) {
|
||||
DB::table('music_playlists')
|
||||
->whereNotNull('category_id')
|
||||
->orderBy('id')
|
||||
->select('id', 'category_id')
|
||||
->chunk(200, function ($rows) {
|
||||
$now = now();
|
||||
$insert = $rows->map(fn ($row) => [
|
||||
'playlist_id' => $row->id,
|
||||
'category_id' => $row->category_id,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
])->all();
|
||||
|
||||
DB::table('music_category_playlist')->insertOrIgnore($insert);
|
||||
});
|
||||
}
|
||||
|
||||
if (Schema::hasColumn('music_playlists', 'subcategory_id')) {
|
||||
DB::table('music_playlists')
|
||||
->whereNotNull('subcategory_id')
|
||||
->orderBy('id')
|
||||
->select('id', 'subcategory_id')
|
||||
->chunk(200, function ($rows) {
|
||||
$now = now();
|
||||
$insert = $rows->map(fn ($row) => [
|
||||
'playlist_id' => $row->id,
|
||||
'subcategory_id' => $row->subcategory_id,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
])->all();
|
||||
|
||||
DB::table('music_subcategory_playlist')->insertOrIgnore($insert);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('music_subcategory_playlist');
|
||||
Schema::dropIfExists('music_category_playlist');
|
||||
}
|
||||
};
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
<?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_playlists', function (Blueprint $table) {
|
||||
$table->dropForeign(['subcategory_id']);
|
||||
$table->dropColumn('subcategory_id');
|
||||
|
||||
$table->dropForeign(['category_id']);
|
||||
$table->dropColumn('category_id');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('music_playlists', function (Blueprint $table) {
|
||||
$table->foreignId('category_id')->nullable()->after('id')
|
||||
->constrained('music_categories')->nullOnDelete();
|
||||
$table->foreignId('subcategory_id')->nullable()->after('category_id')
|
||||
->constrained('music_subcategories')->nullOnDelete();
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user