diff --git a/src/Console/Mappings/Command.php b/src/Console/Mappings/Command.php index e2007fe..3f85a86 100644 --- a/src/Console/Mappings/Command.php +++ b/src/Console/Mappings/Command.php @@ -27,4 +27,9 @@ public function __construct() $this->client = new Connection(Config::get('database.connections.elasticsearch')); } + + /** + * @return void + */ + abstract public function handle(); } diff --git a/src/Console/Mappings/IndexSwapCommand.php b/src/Console/Mappings/IndexSwapCommand.php index 76ea9f2..77d6a53 100644 --- a/src/Console/Mappings/IndexSwapCommand.php +++ b/src/Console/Mappings/IndexSwapCommand.php @@ -2,23 +2,23 @@ namespace DesignMyNight\Elasticsearch\Console\Mappings; -use DesignMyNight\Elasticsearch\Console\Mappings\Traits\UpdatesAlias; -use Elasticsearch\ClientBuilder; +use Illuminate\Console\ConfirmableTrait; +use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Cache; /** * Class IndexSwapCommand - * * @package DesignMyNight\Elasticsearch\Console\Mappings */ class IndexSwapCommand extends Command { - use UpdatesAlias; + use ConfirmableTrait; /** @var string $description */ protected $description = 'Swap Elasticsearch alias'; /** @var string $signature */ - protected $signature = 'index:swap {alias : Name of alias to be updated.} {index? : Name of index to be updated to.} {old-index? : Name of current index.} {--R|remove-old-index : Deletes the old index.}'; + protected $signature = 'index:swap {alias? : Name of alias to be updated.} {index? : Name of index to be updated to.}'; /** * Execute the console command. @@ -27,8 +27,90 @@ class IndexSwapCommand extends Command */ public function handle() { - ['alias' => $alias, 'index' => $index, 'old-index' => $oldIndex] = $this->arguments(); + if (!$alias = $this->argument('alias')) { + $alias = $this->choice( + 'Which alias would you like to update', + $this->aliases()->pluck('alias')->toArray() + ); + } - $this->updateAlias($index, $alias, $oldIndex, $this->option('remove-old-index')); + if (!$index = $this->argument('index')) { + $index = $this->choice( + 'Which index would you like the alias to point to', + $this->indices()->pluck('index')->toArray() + ); + } + + $this->line("Updating {$alias} to {$index}..."); + + $body = [ + 'actions' => [ + [ + 'remove' => [ + 'index' => $this->current($alias), + 'alias' => $alias, + ], + ], + [ + 'add' => [ + 'index' => $index, + 'alias' => $alias, + ], + ], + ], + ]; + + if ($this->confirmToProceed()) { + try { + $this->client->indices()->updateAliases(compact('body')); + } catch (\Exception $exception) { + $this->error("Failed to update alias: {$alias}. {$exception->getMessage()}"); + + return; + } + + $this->info("Updated {$alias} to {$index}"); + } + } + + /** + * @return Collection + */ + protected function aliases(): Collection + { + return Cache::store('array')->rememberForever('aliases', function (): Collection { + return collect($this->client->cat()->aliases())->sortBy('alias'); + }); + } + + /** + * @param string $alias + * + * @return string + */ + protected function current(string $alias): string + { + $aliases = $this->aliases(); + + if (!$alias = $aliases->firstWhere('alias', $alias)) { + $index = $this->choice( + 'Which index is the current index', + $aliases->pluck('index')->toArray() + ); + + $alias = $aliases->firstWhere('index', $index); + } + + return $alias['index']; + } + + /** + * @return Collection + */ + protected function indices(): Collection + { + return Cache::store('array')->rememberForever('indices', function (): Collection { + return collect($this->client->cat()->indices())->sortByDesc('index'); + }); } } diff --git a/src/Console/Mappings/Traits/GetsIndices.php b/src/Console/Mappings/Traits/GetsIndices.php index cf5c750..452eefe 100644 --- a/src/Console/Mappings/Traits/GetsIndices.php +++ b/src/Console/Mappings/Traits/GetsIndices.php @@ -8,6 +8,12 @@ namespace DesignMyNight\Elasticsearch\Console\Mappings\Traits; +use DesignMyNight\Elasticsearch\Console\Mappings\Command; + +/** + * Trait GetsIndices + * @package DesignMyNight\Elasticsearch\Console\Mappings\Traits + */ trait GetsIndices { /** diff --git a/src/Console/Mappings/Traits/HasConnection.php b/src/Console/Mappings/Traits/HasConnection.php deleted file mode 100644 index 1635a38..0000000 --- a/src/Console/Mappings/Traits/HasConnection.php +++ /dev/null @@ -1,34 +0,0 @@ -connection = $connection; - } - - /** - * @return Builder - */ - protected function getConnection(): Builder - { - return DB::connection()->table(config('laravel-elasticsearch.mappings_migration_table', 'mappings')); - } -} diff --git a/src/Console/Mappings/Traits/UpdatesAlias.php b/src/Console/Mappings/Traits/UpdatesAlias.php deleted file mode 100644 index f748bbf..0000000 --- a/src/Console/Mappings/Traits/UpdatesAlias.php +++ /dev/null @@ -1,121 +0,0 @@ -client->cat()->aliases()); - } catch (\Exception $exception) { - $this->error('Failed to retrieve the current active index.'); - } - - $aliases = $aliases->filter(function (array $item) use ($alias): bool { - return str_contains($item['index'], $alias); - })->sortByDesc('index'); - - if ($aliases->count() === 1) { - return $aliases->first()['index']; - } - - $index = $this->choice('Which index is the current index?', $aliases->pluck('index')->toArray(), 0); - - return $aliases->firstWhere('index', $index)['index']; - } - - /** - * Change 2018_09_04_104700_update_pages_dev to pages_dev. - * - * @param string $mapping - * - * @return string - */ - protected function getAlias(string $mapping): string - { - return preg_replace('/^\d{4}\_\d{2}\_\d{2}\_\d{6}\_(update_)?/', '', $mapping, 1); - } - - /** - * @param string $alias - * - * @return string - */ - protected function getIndex(string $alias): string - { - try { - $indices = collect($this->client->cat()->indices()); - } catch (\Exception $exception) { - $this->error('An error occurred attempting to retrieve indices.'); - } - - $relevant = $indices->filter(function (array $item) use ($alias): bool { - return str_contains($item['index'], $alias); - })->sortByDesc('index'); - - return $this->choice('Which index would you like to use?', $relevant->pluck('index')->toArray(), 0); - } - - /** - * @param string|null $index - * @param string|null $alias - * @param string|null $currentIndex - * @param bool $removeOldIndex - */ - protected function updateAlias(?string $index, string $alias = null, ?string $currentIndex = null, bool $removeOldIndex = false): void - { - $index = $index ?? $this->getIndex($alias); - - $this->line("Updating alias to mapping: {$index}"); - - $alias = $alias ?? $this->getAlias($index); - $currentIndex = $currentIndex ?? $this->getActiveIndex($alias); - - $body = [ - 'actions' => [ - [ - 'remove' => [ - 'index' => $currentIndex, - 'alias' => $alias, - ], - ], - [ - 'add' => [ - 'index' => $index, - 'alias' => $alias, - ], - ], - ], - ]; - - try { - $this->client->indices()->updateAliases(['body' => $body]); - } catch (\Exception $exception) { - $this->error("Failed to update alias: {$alias}. {$exception->getMessage()}"); - - return; - } - - $this->info("Updated alias to mapping: {$index}"); - - if ($removeOldIndex) { - $this->call('index:remove', [ - 'index' => $currentIndex, - ]); - } - } -}