Skip to content

Commit

Permalink
refactor: index swap command (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaspar Gupta authored Feb 26, 2020
1 parent 60eef08 commit 7b839e8
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 162 deletions.
5 changes: 5 additions & 0 deletions src/Console/Mappings/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public function __construct()

$this->client = new Connection(Config::get('database.connections.elasticsearch'));
}

/**
* @return void
*/
abstract public function handle();
}
96 changes: 89 additions & 7 deletions src/Console/Mappings/IndexSwapCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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');
});
}
}
6 changes: 6 additions & 0 deletions src/Console/Mappings/Traits/GetsIndices.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down
34 changes: 0 additions & 34 deletions src/Console/Mappings/Traits/HasConnection.php

This file was deleted.

121 changes: 0 additions & 121 deletions src/Console/Mappings/Traits/UpdatesAlias.php

This file was deleted.

0 comments on commit 7b839e8

Please sign in to comment.