refactor: implement classification ID resolution in sub-classification migration process

This commit is contained in:
Yoga Pangestu 2026-04-21 08:34:38 +07:00
parent 6252831ac2
commit fb2e722d0a

View File

@ -112,11 +112,19 @@ private function processSubClassification($legacy): void
$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' => $legacy->classification, // Foreign key to classifications
'classification_id' => $classId,
'name' => $legacy->name,
'is_active' => $legacy->status == '1' ? IsActive::ACTIVE->value : IsActive::INACTIVE->value,
'created_at' => $legacy->created_at ?? now(),
@ -130,4 +138,30 @@ private function processSubClassification($legacy): void
$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');
}
}