Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/Command/DeleteAllSchemasCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)'
);
}

/**
Expand All @@ -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.');
Expand Down
39 changes: 38 additions & 1 deletion tests/Command/DeleteAllSchemasCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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());
}
}