info('Starting media monitoring migration...'); $legacyConn = DB::connection('mysql_second'); // Get legacy monitorings $legacyMonitorings = $legacyConn->table('media_monitorings')->get(); if ($legacyMonitorings->isEmpty()) { $this->warn('No media monitorings found in legacy database.'); return; } $this->withProgressBar($legacyMonitorings, function ($legacy) { // Main record DB::table('media_monitorings')->updateOrInsert( ['id' => $legacy->id], [ 'code' => Str::limit($legacy->code, 10, ''), 'media_name' => Str::limit($legacy->media_name, 50, ''), 'title' => $legacy->title, 'channel' => Str::limit($legacy->channel, 20, ''), 'writter' => Str::limit($legacy->writter, 50, ''), 'link' => $legacy->link, 'news_page' => Str::limit($legacy->news_page, 20, ''), 'quote' => $legacy->quote, 'content' => $legacy->content, 'influencer' => Str::limit($legacy->influencer, 50, ''), 'keyword' => Str::limit($legacy->keyword, 100, ''), 'release_date' => $legacy->release_date, 'created_at' => $legacy->created_at ?? now(), 'updated_at' => $legacy->updated_at ?? now(), 'deleted_at' => $legacy->deleted_at, ] ); // Handle Theme (Insert into themes table if not exists) $themeName = trim($legacy->theme); if (! empty($themeName)) { $themeName = Str::limit($themeName, 50, ''); // Ensure theme exists in master table DB::table('themes')->updateOrInsert( ['name' => $themeName], [ 'is_active' => IsActive::ACTIVE->value, 'sort_order' => 0, 'created_at' => now(), 'updated_at' => now(), ] ); $themeId = DB::table('themes')->where('name', $themeName)->value('id'); // Link to pivot table if ($themeId) { DB::table('media_monitoring_theme')->updateOrInsert( [ 'media_monitoring_id' => $legacy->id, 'theme_id' => $themeId, ] ); } } }); $this->newLine(); $this->info('Media monitoring migration completed successfully!'); } }