From fb2e722d0a10fa92fc85d11d2489e590db4123b4 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Tue, 21 Apr 2026 08:34:38 +0700 Subject: [PATCH] refactor: implement classification ID resolution in sub-classification migration process --- .../Commands/MigrateClassificationCommand.php | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/app/Console/Commands/MigrateClassificationCommand.php b/app/Console/Commands/MigrateClassificationCommand.php index 58a587e..9a2a79e 100644 --- a/app/Console/Commands/MigrateClassificationCommand.php +++ b/app/Console/Commands/MigrateClassificationCommand.php @@ -112,11 +112,19 @@ private function processSubClassification($legacy): void $this->output->write("{$idOutput} (SUB) Processing: ".Str::limit($legacy->name, 40).'... '); + $classId = $this->resolveClassificationId($legacy->classification); + + if (! $classId) { + $this->output->writeln('FAILED: classification not resolved'); + + 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('FAILED: '.$e->getMessage().''); } } + + 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'); + } }