|
| 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 | +} |
0 commit comments