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
65 changes: 65 additions & 0 deletions src/Commands/DeleteCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Limenet\LaravelElasticaBridge\Commands;

use Illuminate\Console\Command;
use Limenet\LaravelElasticaBridge\Client\ElasticaClient;
use Limenet\LaravelElasticaBridge\Repository\IndexRepository;

class DeleteCommand extends Command
{
protected $signature = 'elastica-bridge:delete {--force}';

protected $description = 'Delete all Elasticsearch indices known to the package';

public function __construct(
protected ElasticaClient $elastica,
protected IndexRepository $indexRepository
) {
parent::__construct();
}

public function handle(): int
{
if (! $this->option('force') && ! $this->confirm('Do you want to proceed? (y/N)', false)) {
return self::SUCCESS;
}

foreach ($this->inidices() as $index) {
$client = $this->elastica->getIndex($index);
if (! $client->exists()) {
continue;
}
$this->info($index);

foreach ($client->getAliases() as $alias) {
$this->info($alias);
$client->removeAlias($alias);
}

$client->delete();
}

return self::SUCCESS;
}

private function inidices(): array
{
$indices = [];

foreach ($this->indexRepository->all() as $indexConfig) {
if ($indexConfig->hasBlueGreenIndices()) {
$indices[] = $indexConfig->getBlueGreenActiveElasticaIndex()->getName();
$indices[] = $indexConfig->getBlueGreenInactiveElasticaIndex()->getName();

continue;
}

$indices[] = $indexConfig->getName();
}

return $indices;
}
}
3 changes: 2 additions & 1 deletion src/LaravelElasticaBridgeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Limenet\LaravelElasticaBridge;

use Limenet\LaravelElasticaBridge\Client\ElasticaClient;
use Limenet\LaravelElasticaBridge\Commands\DeleteCommand;
use Limenet\LaravelElasticaBridge\Commands\IndexCommand;
use Limenet\LaravelElasticaBridge\Commands\StatusCommand;
use Limenet\LaravelElasticaBridge\Events\EventHandler;
Expand All @@ -25,7 +26,7 @@ public function configurePackage(Package $package): void
$package
->name('laravel-elastica-bridge')
->hasConfigFile()
->hasCommands([IndexCommand::class, StatusCommand::class]);
->hasCommands([DeleteCommand::class, IndexCommand::class, StatusCommand::class]);
}

public function packageRegistered(): void
Expand Down