diff --git a/app-modules/knowledge-base/database/migrations/2024_10_29_183334_data_add_remainig_slug_in_knowledge_base_categories_table.php b/app-modules/knowledge-base/database/migrations/2024_10_29_183334_data_add_remainig_slug_in_knowledge_base_categories_table.php new file mode 100644 index 000000000..d3be0282c --- /dev/null +++ b/app-modules/knowledge-base/database/migrations/2024_10_29_183334_data_add_remainig_slug_in_knowledge_base_categories_table.php @@ -0,0 +1,47 @@ +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"); + } + } + } +};