Skip to content

Commit

Permalink
Add option for retention period
Browse files Browse the repository at this point in the history
  • Loading branch information
indjeto committed Feb 4, 2024
1 parent babf04e commit ea9739e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*",
Expand Down
9 changes: 6 additions & 3 deletions src/Command/AuditLogDeleteOldLogsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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('<info>Delete all records before %s</info>', $formattedDate));
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ea9739e

Please sign in to comment.