Skip to content

Commit

Permalink
Update the database configuration for KB article categories to ensure…
Browse files Browse the repository at this point in the history
… all categories contain a URL slug
  • Loading branch information
ketan-canyon committed Oct 29, 2024
1 parent f56033a commit 4410cfc
Showing 1 changed file with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Database\QueryException;
use Illuminate\Database\Migrations\Migration;
use AidingApp\KnowledgeBase\Models\KnowledgeBaseCategory;

return new class () extends Migration {
public function up(): void
{
$categories = KnowledgeBaseCategory::whereNull('slug')->get();

foreach ($categories as $category) {
$baseSlug = Str::slug($category->name);
$slug = $baseSlug;
$attempts = 0;

while ($attempts < 15) {
try {
DB::beginTransaction();

$category->slug = $slug;
$category->save();

DB::commit();

break;
} catch (QueryException $e) {
DB::rollBack();

if ($e->errorInfo[1] === 7) {
$attempts++;
$slug = $baseSlug . '-' . $attempts;
} else {
throw $e;
}
}
}

if ($attempts == 15) {
Log::info("Failed to generate a unique slug after 15 attempts for category ID {$category->name}\n");
}
}
}
};

0 comments on commit 4410cfc

Please sign in to comment.