|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of Packagist. |
| 5 | + * |
| 6 | + * (c) Jordi Boggiano <[email protected]> |
| 7 | + * Nils Adermann <[email protected]> |
| 8 | + * |
| 9 | + * For the full copyright and license information, please view the LICENSE |
| 10 | + * file that was distributed with this source code. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace App\Command; |
| 14 | + |
| 15 | +use App\Util\DoctrineTrait; |
| 16 | +use Doctrine\Persistence\ManagerRegistry; |
| 17 | +use Symfony\Component\Console\Command\Command; |
| 18 | +use Symfony\Component\Console\Input\InputInterface; |
| 19 | +use Symfony\Component\Console\Output\OutputInterface; |
| 20 | + |
| 21 | +class MigrateAuditRecordTypesCommand extends Command |
| 22 | +{ |
| 23 | + use DoctrineTrait; |
| 24 | + |
| 25 | + private const array TYPE_MAPPING = [ |
| 26 | + 'canonical_url_change' => 'canonical_url_changed', |
| 27 | + 'version_reference_change' => 'version_reference_changed', |
| 28 | + ]; |
| 29 | + |
| 30 | + public function __construct( |
| 31 | + private readonly ManagerRegistry $doctrine, |
| 32 | + ) { |
| 33 | + parent::__construct(); |
| 34 | + } |
| 35 | + |
| 36 | + protected function configure(): void |
| 37 | + { |
| 38 | + $this |
| 39 | + ->setName('packagist:migrate-audit-record-types') |
| 40 | + ->setDescription('Migrate audit record type values to new naming convention') |
| 41 | + ->setDefinition([ |
| 42 | + ]) |
| 43 | + ; |
| 44 | + } |
| 45 | + |
| 46 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 47 | + { |
| 48 | + $connection = $this->getEM()->getConnection(); |
| 49 | + |
| 50 | + $totalUpdated = 0; |
| 51 | + |
| 52 | + foreach (self::TYPE_MAPPING as $oldType => $newType) { |
| 53 | + $output->writeln("Migrating '$oldType' to '$newType'..."); |
| 54 | + |
| 55 | + try { |
| 56 | + $updatedRows = $connection->executeStatement( |
| 57 | + 'UPDATE audit_log SET type = ? WHERE type = ?', |
| 58 | + [$newType, $oldType] |
| 59 | + ); |
| 60 | + |
| 61 | + $totalUpdated += $updatedRows; |
| 62 | + |
| 63 | + if ($updatedRows === 0) { |
| 64 | + $output->writeln(" No records found for type '$oldType'"); |
| 65 | + } else { |
| 66 | + $output->writeln(" Updated $updatedRows records"); |
| 67 | + } |
| 68 | + } catch (\Exception $e) { |
| 69 | + $output->writeln("<error>Failed to update records for type '$oldType': {$e->getMessage()}</error>"); |
| 70 | + return Command::FAILURE; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + $output->writeln("<info>Migration completed successfully. Total records updated: {$totalUpdated}</info>"); |
| 75 | + |
| 76 | + return Command::SUCCESS; |
| 77 | + } |
| 78 | +} |
0 commit comments