info('Starting optimized location migration...'); $this->migrateLocations(); $this->newLine(); $this->migrateSubLocations(); $this->newLine(); $this->info('Location migration completed successfully!'); } /** * Migrate main locations. */ private function migrateLocations(): void { $legacyConn = DB::connection('mysql_second'); $query = $legacyConn->table('locuses'); $totalCount = $query->count(); $this->info("Found {$totalCount} legacy location records."); $query->orderBy('id')->chunk(50, function ($chunk) { foreach ($chunk as $legacy) { $this->processLocation($legacy); } }); } /** * Process a single legacy location record. */ private function processLocation($legacy): void { $idText = "[#{$legacy->id}]"; $idOutput = $legacy->deleted_at ? "{$idText}" : "{$idText}"; $this->output->write("{$idOutput} (LOC) Processing: ".Str::limit($legacy->name, 40).'... '); try { DB::table('locations')->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('DONE'); } catch (\Exception $e) { $this->output->writeln('FAILED: '.$e->getMessage().''); } } /** * Migrate sub-locations. */ private function migrateSubLocations(): void { $legacyConn = DB::connection('mysql_second'); $query = $legacyConn->table('sub_locuses'); $totalCount = $query->count(); $this->info("Found {$totalCount} legacy sub-location records."); $query->orderBy('id')->chunk(50, function ($chunk) { foreach ($chunk as $legacy) { $this->processSubLocation($legacy); } }); } /** * Process a single legacy sub-location record. */ private function processSubLocation($legacy): void { $idText = "[#{$legacy->id}]"; $idOutput = $legacy->deleted_at ? "{$idText}" : "{$idText}"; $this->output->write("{$idOutput} (SUB) Processing: ".Str::limit($legacy->name, 40).'... '); try { DB::table('sub_locations')->updateOrInsert( ['id' => $legacy->id], [ 'location_id' => $legacy->locus, // Foreign key to locations '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('DONE'); } catch (\Exception $e) { $this->output->writeln('FAILED: '.$e->getMessage().''); } } }