mirror of
https://git.meshkee.com/cssd/cssd-back.git
synced 2026-10-10 19:31:07 +03:30
feat: add command for update model namespace in morph fields
This commit is contained in:
+56
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Symfony\Component\Console\Command\Command as CommandAlias;
|
||||
|
||||
class UpdateModelNamespaces extends Command
|
||||
{
|
||||
protected $signature = 'update:model-namespaces';
|
||||
protected $description = 'Update model namespaces in database columns from App to App\Models';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$oldNamespace = 'App\\\\';
|
||||
$newNamespace = 'App\\\\Models\\\\';
|
||||
|
||||
$tablesAndColumns = [
|
||||
'uploadables' => ['uploadable_type'],
|
||||
'model_has_permissions' => ['model_type'],
|
||||
'model_has_roles' => ['model_type'],
|
||||
'media' => ['model_type'],
|
||||
'transfers' => ['transferable_type'],
|
||||
];
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
foreach ($tablesAndColumns as $table => $columns) {
|
||||
foreach ($columns as $column) {
|
||||
DB::table($table)
|
||||
->where($column, 'LIKE', "{$oldNamespace}%")
|
||||
->where($column, 'NOT LIKE', "{$newNamespace}%") // جلوگیری از اعمال تکراری
|
||||
->update([
|
||||
$column => DB::raw("REPLACE(`{$column}`, '{$oldNamespace}', '{$newNamespace}')")
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
$this->info("Model namespaces updated successfully.");
|
||||
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
$this->error("Error updating model namespaces: " . $e->getMessage());
|
||||
}
|
||||
|
||||
return CommandAlias::SUCCESS;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user