From b789cfc8cf048e9b306e5f4fd7bb5c3a87fc6165 Mon Sep 17 00:00:00 2001 From: guvra Date: Thu, 6 Jun 2024 19:24:25 +0200 Subject: [PATCH] Add a dry-run option --- src/Console/Command/DumpCommand.php | 7 +++++-- src/Dumper/DumperInterface.php | 2 +- src/Dumper/MysqlDumper.php | 10 ++++++---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Console/Command/DumpCommand.php b/src/Console/Command/DumpCommand.php index a70cf2be..f03233c3 100644 --- a/src/Console/Command/DumpCommand.php +++ b/src/Console/Command/DumpCommand.php @@ -42,6 +42,7 @@ public function configure(): void { $configHint = ' (can also be specified in the configuration file)'; + // phpcs:disable Generic.Files.LineLength.TooLong $this->setName('gdpr-dump') ->setDescription('Create an anonymized dump') ->addArgument( @@ -53,7 +54,9 @@ public function configure(): void ->addOption('port', null, InputOption::VALUE_REQUIRED, 'Database port' . $configHint) ->addOption('user', null, InputOption::VALUE_REQUIRED, 'Database user' . $configHint) ->addOption('password', null, InputOption::VALUE_REQUIRED, 'Database password' . $configHint) - ->addOption('database', null, InputOption::VALUE_REQUIRED, 'Database name' . $configHint); + ->addOption('database', null, InputOption::VALUE_REQUIRED, 'Database name' . $configHint) + ->addOption('dry-run', null, InputOption::VALUE_NONE, 'The command will validate the configuration file, but won\'t actually perform the dump'); + // phpcs:enable Generic.Files.LineLength.TooLong } /** @@ -83,7 +86,7 @@ public function execute(InputInterface $input, OutputInterface $output): int $this->dumpInfo->setOutput($output); } - $this->dumper->dump($config); + $this->dumper->dump($config, $input->getOption('dry-run')); } catch (Exception $e) { if ($output->isVerbose()) { throw $e; diff --git a/src/Dumper/DumperInterface.php b/src/Dumper/DumperInterface.php index b07f0bbe..ec162025 100644 --- a/src/Dumper/DumperInterface.php +++ b/src/Dumper/DumperInterface.php @@ -11,5 +11,5 @@ interface DumperInterface /** * Create a dump according to the configuration. */ - public function dump(ConfigInterface $config): void; + public function dump(ConfigInterface $config, bool $dryRun = false): void; } diff --git a/src/Dumper/MysqlDumper.php b/src/Dumper/MysqlDumper.php index 3c216ede..0f92b47c 100644 --- a/src/Dumper/MysqlDumper.php +++ b/src/Dumper/MysqlDumper.php @@ -24,7 +24,7 @@ public function __construct( /** * @inheritdoc */ - public function dump(ConfigInterface $config): void + public function dump(ConfigInterface $config, bool $dryRun = false): void { $database = $this->databaseFactory->create($config); @@ -57,9 +57,11 @@ public function dump(ConfigInterface $config): void // Close the Doctrine connection before proceeding to the dump creation (MySQLDump-PHP uses its own connection) $database->getConnection()->close(); - // Create the dump - $output = $config->getDumpOutput(); - $dumper->start($output); + if (!$dryRun) { + // Create the dump + $dumper->start($config->getDumpOutput()); + } + $this->eventDispatcher->dispatch(new DumpFinishedEvent($config)); }