info('Starting content recap migration...'); $legacyConn = DB::connection('mysql_second'); // 1. Migrate Classifications first $this->info('Migrating classifications for content recap...'); $legacyRecapClasses = $legacyConn->table('content_recap_classifications')->get(); $classMapping = []; foreach ($legacyRecapClasses as $legacyClass) { // Check by name to avoid duplicates in classifications table $existing = DB::table('classifications')->where('name', $legacyClass->name)->first(); if ($existing) { $classMapping[$legacyClass->id] = $existing->id; } else { $newId = DB::table('classifications')->insertGetId([ 'name' => $legacyClass->name, 'is_active' => IsActive::ACTIVE->value, 'sort_order' => 0, 'created_at' => $legacyClass->created_at ?? now(), 'updated_at' => $legacyClass->updated_at ?? now(), 'deleted_at' => $legacyClass->deleted_at, ]); $classMapping[$legacyClass->id] = $newId; } } // 2. Migrate Content Recaps $this->info('Migrating content recaps...'); $legacyRecaps = $legacyConn->table('content_recap')->get(); if ($legacyRecaps->isEmpty()) { $this->warn('No content recaps found in legacy database.'); return; } $this->withProgressBar($legacyRecaps, function ($legacy) use ($classMapping) { $classificationId = $classMapping[$legacy->classification_id] ?? null; // Fallback for classification if not found in master table mapping if (! $classificationId && $legacy->classification) { $classificationId = DB::table('classifications') ->where('name', $legacy->classification) ->value('id'); } // If still no classification, skip or use a default one? // The table requires classification_id so we must have one. if (! $classificationId) { return; // Skip if no classification } DB::table('content_recaps')->updateOrInsert( ['id' => $legacy->id], [ 'classification_id' => $classificationId, 'title' => $legacy->title, 'link' => Str::limit($legacy->link, 50, ''), 'posting_date' => $legacy->posting_date === '0000-00-00' ? now()->toDateString() : $legacy->posting_date, 'type' => $this->mapContentType($legacy->content_type), 'channel' => Str::limit($legacy->channel, 20, ''), 'social_media' => Str::limit($legacy->social_media ?? '-', 50, ''), 'created_at' => $legacy->created_at ?? now(), 'updated_at' => $legacy->updated_at ?? now(), 'deleted_at' => $legacy->deleted_at, ] ); }); $this->newLine(); $this->info('Content recap migration completed successfully!'); } private function mapContentType(?string $legacyType): string { return match (strtolower($legacyType ?? '')) { 'foto' => ContentType::FOTO->value, 'grafis' => ContentType::GRAPHIC->value, 'audio video' => ContentType::VIDEO->value, default => ContentType::FOTO->value, }; } }