diff --git a/src/Command/DeleteAllSchemasCommand.php b/src/Command/DeleteAllSchemasCommand.php index 895238f..30c18ed 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..309f9fc 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()); + } }