From d2aa5d86663fe61d8712359f427272aa685250e5 Mon Sep 17 00:00:00 2001 From: "nikola.trajkovic" Date: Fri, 1 Sep 2023 15:10:41 +0200 Subject: [PATCH 1/2] feat(CDH-115): support hard delete in DeleteAllSchemasCommand --- src/Command/DeleteAllSchemasCommand.php | 17 +++++++- tests/Command/DeleteAllSchemasCommandTest.php | 39 ++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/Command/DeleteAllSchemasCommand.php b/src/Command/DeleteAllSchemasCommand.php index 895238f..f32a103 100644 --- a/src/Command/DeleteAllSchemasCommand.php +++ b/src/Command/DeleteAllSchemasCommand.php @@ -5,6 +5,7 @@ namespace Jobcloud\SchemaConsole\Command; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class DeleteAllSchemasCommand extends AbstractSchemaCommand @@ -17,7 +18,13 @@ protected function configure(): void $this ->setName('kafka-schema-registry:delete:all') ->setDescription('Delete all schemas') - ->setHelp('Delete all schemas'); + ->setHelp('Delete all schemas') + ->addOption( + 'hard', + null, + InputOption::VALUE_NONE, + 'Hard delete of a schema (removes all metadata, including schema ID)' + );; } /** @@ -29,8 +36,16 @@ public function execute(InputInterface $input, OutputInterface $output): int { $schemas = $this->schemaRegistryApi->getSubjects(); + $hardDelete = (bool) $input->getOption('hard'); + foreach ($schemas as $schemaName) { $this->schemaRegistryApi->deleteSubject($schemaName); + + if ($hardDelete) { + $this->schemaRegistryApi->deleteSubject( + sprintf('%s%s', $schemaName, '?permanent=true') + ); + } } $output->writeln('All schemas deleted.'); diff --git a/tests/Command/DeleteAllSchemasCommandTest.php b/tests/Command/DeleteAllSchemasCommandTest.php index d8ab911..127f37c 100644 --- a/tests/Command/DeleteAllSchemasCommandTest.php +++ b/tests/Command/DeleteAllSchemasCommandTest.php @@ -17,7 +17,7 @@ */ class DeleteAllSchemasCommandTest extends TestCase { - public function testCommand(): void + public function testCommandSoftDelete(): void { /** @var MockObject|KafkaSchemaRegistryApiClient $schemaRegistryApi */ $schemaRegistryApi = $this->getMockBuilder(KafkaSchemaRegistryApiClient::class) @@ -41,4 +41,41 @@ public function testCommand(): void self::assertEquals('All schemas deleted.', $commandOutput); self::assertEquals(0, $commandTester->getStatusCode()); } + + public function testCommandHardDelete(): void + { + /** @var MockObject|KafkaSchemaRegistryApiClient $schemaRegistryApi */ + $schemaRegistryApi = $this->getMockBuilder(KafkaSchemaRegistryApiClient::class) + ->disableOriginalConstructor() + ->onlyMethods(['getSubjects', 'deleteSubject']) + ->getMock(); + + $schemaRegistryApi->expects(self::once()) + ->method('getSubjects') + ->willReturn(['schema1']); + + $schemaRegistryApi->expects(self::exactly(2)) + ->method('deleteSubject') + ->with(self::callback(function ($inputArgument){ + static $input = 'schema1'; + if ($inputArgument === $input) { + $input = $input.'?permanent=true'; + return true; + } + return false; + })) + ->willReturn([]); + + $application = new Application(); + $application->add(new DeleteAllSchemasCommand($schemaRegistryApi)); + $command = $application->find('kafka-schema-registry:delete:all'); + $commandTester = new CommandTester($command); + + $commandTester->execute(['--hard' => true]); + + $commandOutput = trim($commandTester->getDisplay()); + + self::assertEquals('All schemas deleted.', $commandOutput); + self::assertEquals(0, $commandTester->getStatusCode()); + } } From 60b71939864e0f2ef7d3287564e254032889558d Mon Sep 17 00:00:00 2001 From: "nikola.trajkovic" Date: Fri, 1 Sep 2023 15:13:58 +0200 Subject: [PATCH 2/2] feat(CDH-115): cs-fixes --- src/Command/DeleteAllSchemasCommand.php | 2 +- tests/Command/DeleteAllSchemasCommandTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Command/DeleteAllSchemasCommand.php b/src/Command/DeleteAllSchemasCommand.php index f32a103..30c18ed 100644 --- a/src/Command/DeleteAllSchemasCommand.php +++ b/src/Command/DeleteAllSchemasCommand.php @@ -24,7 +24,7 @@ protected function configure(): void null, InputOption::VALUE_NONE, 'Hard delete of a schema (removes all metadata, including schema ID)' - );; + ); } /** diff --git a/tests/Command/DeleteAllSchemasCommandTest.php b/tests/Command/DeleteAllSchemasCommandTest.php index 127f37c..309f9fc 100644 --- a/tests/Command/DeleteAllSchemasCommandTest.php +++ b/tests/Command/DeleteAllSchemasCommandTest.php @@ -56,10 +56,10 @@ public function testCommandHardDelete(): void $schemaRegistryApi->expects(self::exactly(2)) ->method('deleteSubject') - ->with(self::callback(function ($inputArgument){ + ->with(self::callback(function ($inputArgument) { static $input = 'schema1'; if ($inputArgument === $input) { - $input = $input.'?permanent=true'; + $input = $input . '?permanent=true'; return true; } return false;