84 lines
2.4 KiB
PHP
84 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Enums\AnnouncementType;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class MigrateAnnouncementCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'migrate:announcement';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Migrate announcement data from legacy news table where category = 1 with optimized processing';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
$this->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 ? "<fg=red>{$idText}</>" : "<info>{$idText}</info>";
|
|
|
|
$this->output->write("{$idOutput} Processing: <comment>".Str::limit($legacy->title, 40).'</comment>... ');
|
|
|
|
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('<info>DONE</info>');
|
|
} catch (\Exception $e) {
|
|
$this->output->writeln('<error>FAILED: '.$e->getMessage().'</error>');
|
|
}
|
|
}
|
|
}
|