Changed channel categories from DB-backed Category records to a backed PHP enum

This commit is contained in:
2026-05-18 06:55:03 +03:30
parent 295a408965
commit 59e4ae60da
20 changed files with 221 additions and 130 deletions

View File

@@ -1,20 +0,0 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class CategoryFactory extends Factory
{
public function definition(): array
{
$name = fake()->unique()->words(2, true);
return [
'name' => Str::headline($name),
'slug' => Str::slug($name),
'description' => fake()->optional()->sentence(),
];
}
}

View File

@@ -2,7 +2,7 @@
namespace Database\Factories;
use App\Models\Category;
use App\Enums\ChannelCategory;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
@@ -15,7 +15,7 @@ class ChannelFactory extends Factory
return [
'user_id' => User::factory(),
'category_id' => Category::factory(),
'category' => fake()->randomElement(ChannelCategory::cases())->value,
'slug' => Str::slug($name),
'display_name' => Str::headline($name),
'description' => fake()->sentence(),

View File

@@ -0,0 +1,76 @@
<?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
{
public function up(): void
{
Schema::table('channels', function (Blueprint $table) {
$table->string('category')->nullable()->after('user_id')->index();
});
$allowedCategories = ['gaming', 'music', 'talk-shows', 'education', 'creative'];
DB::table('categories')
->select(['id', 'slug'])
->orderBy('id')
->get()
->each(function (object $category) use ($allowedCategories): void {
if (! in_array($category->slug, $allowedCategories, true)) {
return;
}
DB::table('channels')
->where('category_id', $category->id)
->update(['category' => $category->slug]);
});
Schema::table('channels', function (Blueprint $table) {
$table->dropConstrainedForeignId('category_id');
});
Schema::dropIfExists('categories');
}
public function down(): void
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name')->unique();
$table->string('slug')->unique();
$table->text('description')->nullable();
$table->timestamps();
});
DB::table('categories')->insert([
['name' => 'Gaming', 'slug' => 'gaming', 'created_at' => now(), 'updated_at' => now()],
['name' => 'Music', 'slug' => 'music', 'created_at' => now(), 'updated_at' => now()],
['name' => 'Talk Shows', 'slug' => 'talk-shows', 'created_at' => now(), 'updated_at' => now()],
['name' => 'Education', 'slug' => 'education', 'created_at' => now(), 'updated_at' => now()],
['name' => 'Creative', 'slug' => 'creative', 'created_at' => now(), 'updated_at' => now()],
]);
Schema::table('channels', function (Blueprint $table) {
$table->foreignId('category_id')->nullable()->after('user_id')->constrained()->nullOnDelete();
});
DB::table('categories')
->select(['id', 'slug'])
->orderBy('id')
->get()
->each(function (object $category): void {
DB::table('channels')
->where('category', $category->slug)
->update(['category_id' => $category->id]);
});
Schema::table('channels', function (Blueprint $table) {
$table->dropIndex(['category']);
$table->dropColumn('category');
});
}
};

View File

@@ -2,7 +2,7 @@
namespace Database\Seeders;
use App\Models\Category;
use App\Enums\ChannelCategory;
use App\Models\PlatformSetting;
use App\Models\User;
use App\Services\Streaming\StreamKeyManager;
@@ -15,17 +15,6 @@ class DatabaseSeeder extends Seeder
*/
public function run(): void
{
collect([
['name' => 'Gaming', 'slug' => 'gaming'],
['name' => 'Music', 'slug' => 'music'],
['name' => 'Talk Shows', 'slug' => 'talk-shows'],
['name' => 'Education', 'slug' => 'education'],
['name' => 'Creative', 'slug' => 'creative'],
])->each(fn (array $category) => Category::query()->firstOrCreate(
['slug' => $category['slug']],
['name' => $category['name']],
));
PlatformSetting::current();
$admin = User::query()->firstOrNew(['email' => 'admin@nyone.net']);
@@ -47,7 +36,7 @@ class DatabaseSeeder extends Seeder
])->save();
$channel = $official->channel()->updateOrCreate([], [
'category_id' => Category::query()->where('slug', 'creative')->value('id'),
'category' => ChannelCategory::Creative,
'slug' => 'nyone',
'display_name' => 'Nyone Official',
'description' => 'Official updates and live sessions from Nyone.',