some fixes
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('websites', function (Blueprint $table) {
|
||||
$table->string('slug')->nullable()->after('domain');
|
||||
$table->unique('slug');
|
||||
$table->index('slug');
|
||||
});
|
||||
|
||||
// Generate slugs for existing websites
|
||||
$websites = DB::table('websites')->get();
|
||||
foreach ($websites as $website) {
|
||||
$baseSlug = Str::slug($website->name);
|
||||
$slug = $baseSlug;
|
||||
$counter = 1;
|
||||
|
||||
// Ensure uniqueness
|
||||
while (DB::table('websites')->where('slug', $slug)->where('id', '!=', $website->id)->exists()) {
|
||||
$slug = $baseSlug . '-' . $counter;
|
||||
$counter++;
|
||||
}
|
||||
|
||||
DB::table('websites')->where('id', $website->id)->update(['slug' => $slug]);
|
||||
}
|
||||
|
||||
// Make slug non-nullable after populating
|
||||
Schema::table('websites', function (Blueprint $table) {
|
||||
$table->string('slug')->nullable(false)->change();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('websites', function (Blueprint $table) {
|
||||
$table->dropUnique(['slug']);
|
||||
$table->dropIndex(['slug']);
|
||||
$table->dropColumn('slug');
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user