info('Starting optimized announcement migration...'); $legacyConn = DB::connection('mysql_second'); $query = $legacyConn->table('news')->where('category', '1'); $totalCount = $query->count(); if ($totalCount === 0) { $this->warn('No announcements found in legacy database.'); return; } $this->info("Found {$totalCount} legacy announcement records."); $query->orderBy('id')->chunk(50, function ($chunk) { foreach ($chunk as $legacy) { $this->processAnnouncement($legacy); } }); $this->newLine(); $this->info('Announcement migration completed successfully!'); } /** * Process a single legacy announcement record. */ private function processAnnouncement($legacy): void { $idText = "[#{$legacy->id}]"; $idOutput = $legacy->deleted_at ? "{$idText}" : "{$idText}"; $this->output->write("{$idOutput} Processing: ".Str::limit($legacy->title, 40).'... '); try { DB::table('announcements')->updateOrInsert( ['id' => $legacy->id], [ 'title' => $legacy->title, 'content' => $legacy->content, 'type' => AnnouncementType::PUBLIC->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().''); } } }