76 lines
2.4 KiB
PHP
76 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Command;
|
|
|
|
use App\Entity\LogEntry;
|
|
use App\Repository\LogEntryRepository;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Component\Console\Attribute\AsCommand;
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
|
|
|
#[AsCommand(
|
|
name: 'app:log-entry:backfill-error-codes',
|
|
description: 'Backfills error_code column from extra data in existing log entries'
|
|
)]
|
|
class LogEntryBackfillErrorCodesCommand extends Command
|
|
{
|
|
private const BATCH_SIZE = 100;
|
|
|
|
public function __construct(
|
|
private readonly LogEntryRepository $logEntryRepository,
|
|
private readonly EntityManagerInterface $entityManager,
|
|
) {
|
|
parent::__construct();
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
$io = new SymfonyStyle($input, $output);
|
|
|
|
$io->info('Starting backfill of error codes...');
|
|
|
|
$totalProcessed = 0;
|
|
$totalUpdated = 0;
|
|
|
|
while (true) {
|
|
$entries = $this->logEntryRepository->createQueryBuilder('l')
|
|
->where('l.errorCode IS NULL')
|
|
->andWhere('l.extra LIKE :pattern')
|
|
->setParameter('pattern', '%error_code%')
|
|
->orderBy('l.id', 'ASC')
|
|
->setMaxResults(self::BATCH_SIZE)
|
|
->getQuery()
|
|
->getResult();
|
|
|
|
if ([] === $entries) {
|
|
break;
|
|
}
|
|
|
|
foreach ($entries as $entry) {
|
|
/** @var LogEntry $entry */
|
|
$extra = $entry->getExtra();
|
|
if (isset($extra['error_code']) && null === $entry->getErrorCode()) {
|
|
$entry->setErrorCode($extra['error_code']);
|
|
++$totalUpdated;
|
|
}
|
|
++$totalProcessed;
|
|
}
|
|
|
|
$this->entityManager->flush();
|
|
$this->entityManager->clear();
|
|
|
|
$io->writeln(sprintf('Processed %d entries, %d updated so far', $totalProcessed, $totalUpdated));
|
|
}
|
|
|
|
$message = sprintf('Backfill complete. Processed %d entries, updated %d with error codes.', $totalProcessed, $totalUpdated);
|
|
$io->success($message);
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|