Skip to content

Commit e760abc

Browse files
committed
Add delete command
1 parent 3fa1d40 commit e760abc

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MeiliSearch\Bundle\Command;
6+
7+
use MeiliSearch\Exceptions\ApiException;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Input\InputOption;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
12+
/**
13+
* Class MeiliSearchImportCommand.
14+
*/
15+
final class MeiliSearchDeleteCommand extends IndexCommand
16+
{
17+
protected static $defaultName = 'meili:delete';
18+
19+
protected function configure(): void
20+
{
21+
$this
22+
->setDescription('Delete the indices')
23+
->addOption('indices', 'i', InputOption::VALUE_OPTIONAL, 'Comma-separated list of index names');
24+
}
25+
26+
protected function execute(InputInterface $input, OutputInterface $output): int
27+
{
28+
$indexToDelete = collect($this->getEntitiesFromArgs($input, $output))->unique('name');
29+
30+
/** @var array<string, mixed> $index */
31+
foreach ($indexToDelete as $index) {
32+
$indexName = $index['name'];
33+
try {
34+
$this->searchService->deleteByIndexName($indexName);
35+
} catch (ApiException $e) {
36+
$output->writeln('Cannot delete '.$indexName.': '.$e->getMessage());
37+
continue;
38+
}
39+
$output->writeln('Deleted <info>'.$indexName.'</info>');
40+
}
41+
42+
if (0 === count($indexToDelete)) {
43+
$output->writeln('Cannot delete index. Not found.');
44+
}
45+
46+
$output->writeln('<info>Done!</info>');
47+
48+
return 0;
49+
}
50+
}

tests/Integration/CommandsTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testSearchClearUnknownIndex(): void
5151
$this->assertStringContainsString('Cannot clear index. Not found.', $output);
5252
}
5353

54-
public function testSearchImportAndClearWithoutIndices(): void
54+
public function testSearchImportAndClearAndDeleteWithoutIndices(): void
5555
{
5656
for ($i = 0; $i <= 5; ++$i) {
5757
$this->createPost();
@@ -109,6 +109,22 @@ public function testSearchImportAndClearWithoutIndices(): void
109109
Cleared sf_phpunit__pages index of MeiliSearch\Bundle\Test\Entity\Page
110110
Done!
111111

112+
EOD, $clearOutput);
113+
114+
$clearCommand = $this->application->find('meili:delete');
115+
$clearCommandTester = new CommandTester($clearCommand);
116+
$clearCommandTester->execute([]);
117+
118+
$clearOutput = $clearCommandTester->getDisplay();
119+
120+
$this->assertSame(<<<'EOD'
121+
Deleted sf_phpunit__posts
122+
Deleted sf_phpunit__comments
123+
Deleted sf_phpunit__aggregated
124+
Deleted sf_phpunit__tags
125+
Deleted sf_phpunit__pages
126+
Done!
127+
112128
EOD, $clearOutput);
113129
}
114130

0 commit comments

Comments
 (0)