-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update the database configuration for KB article categories to ensure…
… all categories contain a URL slug
- Loading branch information
1 parent
f56033a
commit 4410cfc
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
...igrations/2024_10_29_183334_data_add_remainig_slug_in_knowledge_base_categories_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} | ||
}; |