168 lines
5.1 KiB
PHP
168 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Enums\IsActive;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class MigrateClassificationCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'migrate:classification';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Migrate classification and sub-classification data from legacy database with optimized processing';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
$this->info('Starting optimized classification migration...');
|
|
|
|
$this->migrateClassifications();
|
|
$this->newLine();
|
|
$this->migrateSubClassifications();
|
|
|
|
$this->newLine();
|
|
$this->info('Classification migration completed successfully!');
|
|
}
|
|
|
|
/**
|
|
* Migrate main classifications.
|
|
*/
|
|
private function migrateClassifications(): void
|
|
{
|
|
$legacyConn = DB::connection('mysql_second');
|
|
$query = $legacyConn->table('classifications');
|
|
|
|
$totalCount = $query->count();
|
|
$this->info("Found {$totalCount} legacy classification records.");
|
|
|
|
$query->orderBy('id')->chunk(50, function ($chunk) {
|
|
foreach ($chunk as $legacy) {
|
|
$this->processClassification($legacy);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Process a single legacy classification record.
|
|
*/
|
|
private function processClassification($legacy): void
|
|
{
|
|
$idText = "[#{$legacy->id}]";
|
|
$idOutput = $legacy->deleted_at ? "<fg=red>{$idText}</>" : "<info>{$idText}</info>";
|
|
|
|
$this->output->write("{$idOutput} (CLA) Processing: <comment>".Str::limit($legacy->name, 40).'</comment>... ');
|
|
|
|
try {
|
|
DB::table('classifications')->updateOrInsert(
|
|
['id' => $legacy->id],
|
|
[
|
|
'name' => $legacy->name,
|
|
'is_active' => $legacy->status == '1' ? IsActive::ACTIVE->value : IsActive::INACTIVE->value,
|
|
'created_at' => $legacy->created_at ?? now(),
|
|
'updated_at' => $legacy->updated_at ?? now(),
|
|
'deleted_at' => $legacy->deleted_at,
|
|
]
|
|
);
|
|
|
|
$this->output->writeln('<info>DONE</info>');
|
|
} catch (\Exception $e) {
|
|
$this->output->writeln('<error>FAILED: '.$e->getMessage().'</error>');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Migrate sub-classifications.
|
|
*/
|
|
private function migrateSubClassifications(): void
|
|
{
|
|
$legacyConn = DB::connection('mysql_second');
|
|
$query = $legacyConn->table('sub_classifications');
|
|
|
|
$totalCount = $query->count();
|
|
$this->info("Found {$totalCount} legacy sub-classification records.");
|
|
|
|
$query->orderBy('id')->chunk(50, function ($chunk) {
|
|
foreach ($chunk as $legacy) {
|
|
$this->processSubClassification($legacy);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Process a single legacy sub-classification record.
|
|
*/
|
|
private function processSubClassification($legacy): void
|
|
{
|
|
$idText = "[#{$legacy->id}]";
|
|
$idOutput = $legacy->deleted_at ? "<fg=red>{$idText}</>" : "<info>{$idText}</info>";
|
|
|
|
$this->output->write("{$idOutput} (SUB) Processing: <comment>".Str::limit($legacy->name, 40).'</comment>... ');
|
|
|
|
$classId = $this->resolveClassificationId($legacy->classification);
|
|
|
|
if (! $classId) {
|
|
$this->output->writeln('<error>FAILED: classification not resolved</error>');
|
|
|
|
return;
|
|
}
|
|
|
|
try {
|
|
DB::table('sub_classifications')->updateOrInsert(
|
|
['id' => $legacy->id],
|
|
[
|
|
'classification_id' => $classId,
|
|
'name' => $legacy->name,
|
|
'is_active' => $legacy->status == '1' ? IsActive::ACTIVE->value : IsActive::INACTIVE->value,
|
|
'created_at' => $legacy->created_at ?? now(),
|
|
'updated_at' => $legacy->updated_at ?? now(),
|
|
'deleted_at' => $legacy->deleted_at,
|
|
]
|
|
);
|
|
|
|
$this->output->writeln('<info>DONE</info>');
|
|
} catch (\Exception $e) {
|
|
$this->output->writeln('<error>FAILED: '.$e->getMessage().'</error>');
|
|
}
|
|
}
|
|
|
|
private function resolveClassificationId($legacyClassificationId): ?int
|
|
{
|
|
$legacyClass = DB::connection('mysql_second')
|
|
->table('classifications')
|
|
->where('id', $legacyClassificationId)
|
|
->first();
|
|
|
|
if (! $legacyClass) {
|
|
return null;
|
|
}
|
|
|
|
$existing = DB::table('classifications')
|
|
->where('id', $legacyClass->id)
|
|
->whereNull('deleted_at')
|
|
->value('id');
|
|
|
|
if ($existing) {
|
|
return $existing;
|
|
}
|
|
|
|
return DB::table('classifications')
|
|
->where('name', $legacyClass->name)
|
|
->whereNull('deleted_at')
|
|
->value('id');
|
|
}
|
|
}
|