83 lines
2.3 KiB
PHP
83 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Enums\IsActive;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class MigrateThemeCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'migrate:theme';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Migrate theme data from legacy proof_airing_themes table with optimized processing';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
$this->info('Starting optimized theme migration...');
|
|
|
|
$legacyConn = DB::connection('mysql_second');
|
|
$query = $legacyConn->table('proof_airing_themes');
|
|
|
|
$totalCount = $query->count();
|
|
if ($totalCount === 0) {
|
|
$this->warn('No themes found in legacy database.');
|
|
|
|
return;
|
|
}
|
|
|
|
$this->info("Found {$totalCount} legacy theme records.");
|
|
|
|
$query->orderBy('id')->chunk(50, function ($chunk) {
|
|
foreach ($chunk as $legacy) {
|
|
$this->processTheme($legacy);
|
|
}
|
|
});
|
|
|
|
$this->newLine();
|
|
$this->info('Theme migration completed successfully!');
|
|
}
|
|
|
|
/**
|
|
* Process a single legacy theme record.
|
|
*/
|
|
private function processTheme($legacy): void
|
|
{
|
|
$idText = "[#{$legacy->id}]";
|
|
$idOutput = $legacy->deleted_at ? "<fg=red>{$idText}</>" : "<info>{$idText}</info>";
|
|
|
|
$this->output->write("{$idOutput} Processing: <comment>".Str::limit($legacy->name, 40).'</comment>... ');
|
|
|
|
try {
|
|
DB::table('themes')->updateOrInsert(
|
|
['id' => $legacy->id],
|
|
[
|
|
'name' => $legacy->name,
|
|
'is_active' => IsActive::ACTIVE->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>');
|
|
}
|
|
}
|
|
}
|