From ea9739e1de7576e97485051bb4d874c1d98d5ce1 Mon Sep 17 00:00:00 2001 From: Georgi Kolev Date: Sun, 4 Feb 2024 14:34:21 +0200 Subject: [PATCH] Add option for retention period --- composer.json | 3 ++- src/Command/AuditLogDeleteOldLogsCommand.php | 9 ++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 72c9e5b..bd9d397 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ "doctrine/doctrine-bundle": "^2.9", "doctrine/orm": "^2.13", "symfony/security-bundle": "^6.4", - "symfony/framework-bundle": "^6.4" + "symfony/framework-bundle": "^6.4", + "symfony/console": "^6.4" }, "require-dev": { "ext-pdo": "*", diff --git a/src/Command/AuditLogDeleteOldLogsCommand.php b/src/Command/AuditLogDeleteOldLogsCommand.php index cc68fee..048398c 100644 --- a/src/Command/AuditLogDeleteOldLogsCommand.php +++ b/src/Command/AuditLogDeleteOldLogsCommand.php @@ -8,6 +8,7 @@ use Doctrine\DBAL\ParameterType; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class AuditLogDeleteOldLogsCommand extends Command @@ -24,13 +25,15 @@ public function __construct( protected function configure(): void { $this->setName('audit-logs:delete-old-logs') - ->setDescription('Remove old records from the audit logs'); + ->setDescription('Remove old records from the audit logs') + ->addOption('retention-period', null, InputOption::VALUE_OPTIONAL, 'The retention interval, format: https://www.php.net/manual/en/dateinterval.construct.php', self::DEFAULT_RETENTION_PERIOD) + ; } #[\Override] protected function execute(InputInterface $input, OutputInterface $output): int { - $date = (new \DateTime())->sub(new \DateInterval(self::DEFAULT_RETENTION_PERIOD)); + $date = (new \DateTime())->sub(new \DateInterval($input->getOption('retention-period'))); $formattedDate = $date->format('Y-m-d H:i:s'); $output->writeln(sprintf('Delete all records before %s', $formattedDate)); @@ -85,7 +88,7 @@ private function deleteFromAuditAssociations(int $startRecordId): int $allRecords = 0; $this->connection->executeQuery('SET FOREIGN_KEY_CHECKS=0'); - $sql = 'DELETE LOW_PRIORITY FROM mscm.audit_associations WHERE id <= ? ORDER BY id LIMIT 10000'; + $sql = 'DELETE LOW_PRIORITY FROM audit_associations WHERE id <= ? ORDER BY id LIMIT 10000'; $stmt = $this->connection->prepare($sql); $stmt->bindValue(1, $startRecordId, ParameterType::INTEGER); do {