refactor: Consolidate user settings table creation by integrating all preference columns and enhancing the NotifStyle enum.

This commit is contained in:
Yoga Pangestu 2026-03-26 16:01:45 +07:00
parent cd1b9b09ba
commit 22572bc6a3
6 changed files with 12 additions and 147 deletions

View File

@ -2,10 +2,14 @@
namespace App\Enums;
use App\Traits\Enum\WithComment;
use App\Traits\Enum\WithValue;
use Filament\Support\Contracts\HasLabel;
enum NotifStyle: string implements HasLabel
{
use WithComment, WithValue;
case CHEERFUL = 'cheerful';
case FORMAL = 'formal';

View File

@ -1,28 +0,0 @@
<?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('users', function (Blueprint $table) {
$table->string('notif_style')->default('cheerful')->after('email_verified_at');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
//
});
}
};

View File

@ -1,8 +1,8 @@
<?php
use App\Enums\NotifStyle;
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
@ -14,29 +14,15 @@ public function up(): void
{
Schema::create('user_settings', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->unique()->constrained()->onDelete('cascade');
$table->string('notif_style')->default('cheerful');
$table->string('primary_color')->nullable(); // For future extension
$table->foreignId('user_id')->unique()->constrained()->cascadeOnDelete();
$table->enum('notif_style', NotifStyle::values())->default(NotifStyle::CHEERFUL->value)->comment(NotifStyle::comment());
$table->string('primary_color')->nullable();
$table->string('font')->default('Inter');
$table->string('content_width')->default('full');
$table->string('border_radius')->default('lg');
$table->boolean('top_navigation')->default(false);
$table->timestamps();
});
// Migrate existing data from users table (already has notif_style from earlier migration)
$users = DB::table('users')->get();
foreach ($users as $user) {
DB::table('user_settings')->updateOrInsert(
['user_id' => $user->id],
[
'notif_style' => $user->notif_style ?? 'cheerful',
'created_at' => now(),
'updated_at' => now(),
]
);
}
// Drop the old column from users
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('notif_style');
});
}
/**
@ -44,18 +30,6 @@ public function up(): void
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->string('notif_style')->default('cheerful')->after('email_verified_at');
});
// Revert data
$settings = DB::table('user_settings')->get();
foreach ($settings as $setting) {
DB::table('users')
->where('id', $setting->user_id)
->update(['notif_style' => $setting->notif_style]);
}
Schema::dropIfExists('user_settings');
}
};

View File

@ -1,28 +0,0 @@
<?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('user_settings', function (Blueprint $table) {
$table->string('font')->default('Inter')->after('primary_color');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('user_settings', function (Blueprint $table) {
$table->dropColumn('font');
});
}
};

View File

@ -1,29 +0,0 @@
<?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('user_settings', function (Blueprint $table) {
$table->string('content_width')->default('full')->after('font');
$table->string('border_radius')->default('lg')->after('content_width');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('user_settings', function (Blueprint $table) {
$table->dropColumn(['content_width', 'border_radius']);
});
}
};

View File

@ -1,28 +0,0 @@
<?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('user_settings', function (Blueprint $table) {
$table->boolean('top_navigation')->default(false)->after('border_radius');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('user_settings', function (Blueprint $table) {
$table->dropColumn('top_navigation');
});
}
};