Skip to content

Commit 723523e

Browse files
authored
add delete command (#8)
1 parent eff974b commit 723523e

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/Commands/DeleteCommand.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Limenet\LaravelElasticaBridge\Commands;
6+
7+
use Illuminate\Console\Command;
8+
use Limenet\LaravelElasticaBridge\Client\ElasticaClient;
9+
use Limenet\LaravelElasticaBridge\Repository\IndexRepository;
10+
11+
class DeleteCommand extends Command
12+
{
13+
protected $signature = 'elastica-bridge:delete {--force}';
14+
15+
protected $description = 'Delete all Elasticsearch indices known to the package';
16+
17+
public function __construct(
18+
protected ElasticaClient $elastica,
19+
protected IndexRepository $indexRepository
20+
) {
21+
parent::__construct();
22+
}
23+
24+
public function handle(): int
25+
{
26+
if (! $this->option('force') && ! $this->confirm('Do you want to proceed? (y/N)', false)) {
27+
return self::SUCCESS;
28+
}
29+
30+
foreach ($this->inidices() as $index) {
31+
$client = $this->elastica->getIndex($index);
32+
if (! $client->exists()) {
33+
continue;
34+
}
35+
$this->info($index);
36+
37+
foreach ($client->getAliases() as $alias) {
38+
$this->info($alias);
39+
$client->removeAlias($alias);
40+
}
41+
42+
$client->delete();
43+
}
44+
45+
return self::SUCCESS;
46+
}
47+
48+
private function inidices(): array
49+
{
50+
$indices = [];
51+
52+
foreach ($this->indexRepository->all() as $indexConfig) {
53+
if ($indexConfig->hasBlueGreenIndices()) {
54+
$indices[] = $indexConfig->getBlueGreenActiveElasticaIndex()->getName();
55+
$indices[] = $indexConfig->getBlueGreenInactiveElasticaIndex()->getName();
56+
57+
continue;
58+
}
59+
60+
$indices[] = $indexConfig->getName();
61+
}
62+
63+
return $indices;
64+
}
65+
}

src/LaravelElasticaBridgeServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Limenet\LaravelElasticaBridge;
66

77
use Limenet\LaravelElasticaBridge\Client\ElasticaClient;
8+
use Limenet\LaravelElasticaBridge\Commands\DeleteCommand;
89
use Limenet\LaravelElasticaBridge\Commands\IndexCommand;
910
use Limenet\LaravelElasticaBridge\Commands\StatusCommand;
1011
use Limenet\LaravelElasticaBridge\Events\EventHandler;
@@ -25,7 +26,7 @@ public function configurePackage(Package $package): void
2526
$package
2627
->name('laravel-elastica-bridge')
2728
->hasConfigFile()
28-
->hasCommands([IndexCommand::class, StatusCommand::class]);
29+
->hasCommands([DeleteCommand::class, IndexCommand::class, StatusCommand::class]);
2930
}
3031

3132
public function packageRegistered(): void

0 commit comments

Comments
 (0)