Ability to delete an index if expecting an alias

This commit is contained in:
Patrick McAndrew 2014-08-28 17:59:58 +01:00
parent a7a23b92cb
commit 2958833012
4 changed files with 47 additions and 7 deletions

View file

@ -33,6 +33,7 @@ class ResetCommand extends ContainerAwareCommand
->setName('fos:elastica:reset') ->setName('fos:elastica:reset')
->addOption('index', null, InputOption::VALUE_OPTIONAL, 'The index to reset') ->addOption('index', null, InputOption::VALUE_OPTIONAL, 'The index to reset')
->addOption('type', null, InputOption::VALUE_OPTIONAL, 'The type to reset') ->addOption('type', null, InputOption::VALUE_OPTIONAL, 'The type to reset')
->addOption('force', null, InputOption::VALUE_NONE, 'Force index deletion if same name as alias')
->setDescription('Reset search indexes') ->setDescription('Reset search indexes')
; ;
} }
@ -53,6 +54,7 @@ class ResetCommand extends ContainerAwareCommand
{ {
$index = $input->getOption('index'); $index = $input->getOption('index');
$type = $input->getOption('type'); $type = $input->getOption('type');
$force = (true == $input->getOption('force'));
if (null === $index && null !== $type) { if (null === $index && null !== $type) {
throw new \InvalidArgumentException('Cannot specify type option without an index.'); throw new \InvalidArgumentException('Cannot specify type option without an index.');
@ -69,7 +71,7 @@ class ResetCommand extends ContainerAwareCommand
foreach ($indexes as $index) { foreach ($indexes as $index) {
$output->writeln(sprintf('<info>Resetting</info> <comment>%s</comment>', $index)); $output->writeln(sprintf('<info>Resetting</info> <comment>%s</comment>', $index));
$this->resetter->resetIndex($index); $this->resetter->resetIndex($index, false, $force);
} }
} }
} }

View file

@ -0,0 +1,12 @@
<?php
namespace FOS\ElasticaBundle\Exception;
class AliasIsIndexException extends \Exception
{
public function __construct($indexName)
{
parent::__construct(sprintf('Expected alias %s instead of index', $indexName));
}
}

View file

@ -15,6 +15,7 @@ use Elastica\Exception\ExceptionInterface;
use FOS\ElasticaBundle\Configuration\IndexConfig; use FOS\ElasticaBundle\Configuration\IndexConfig;
use FOS\ElasticaBundle\Elastica\Client; use FOS\ElasticaBundle\Elastica\Client;
use FOS\ElasticaBundle\Elastica\Index; use FOS\ElasticaBundle\Elastica\Index;
use FOS\ElasticaBundle\Exception\AliasIsIndexException;
class AliasProcessor class AliasProcessor
{ {
@ -35,9 +36,10 @@ class AliasProcessor
* *
* @param IndexConfig $indexConfig * @param IndexConfig $indexConfig
* @param Index $index * @param Index $index
* @param boolean $force If index exists with same name as alias, remove it
* @throws \RuntimeException * @throws \RuntimeException
*/ */
public function switchIndexAlias(IndexConfig $indexConfig, Index $index) public function switchIndexAlias(IndexConfig $indexConfig, Index $index, $force = false)
{ {
$client = $index->getClient(); $client = $index->getClient();
@ -45,7 +47,16 @@ class AliasProcessor
$oldIndexName = false; $oldIndexName = false;
$newIndexName = $index->getName(); $newIndexName = $index->getName();
$aliasedIndexes = array();
try {
$aliasedIndexes = $this->getAliasedIndexes($client, $aliasName); $aliasedIndexes = $this->getAliasedIndexes($client, $aliasName);
} catch(AliasIsIndexException $e) {
if ($force) {
$this->deleteIndex($client, $aliasName);
} else {
throw new \RuntimeException($e->getMessage());
}
}
if (count($aliasedIndexes) > 1) { if (count($aliasedIndexes) > 1) {
throw new \RuntimeException( throw new \RuntimeException(
@ -125,6 +136,9 @@ class AliasProcessor
$aliasedIndexes = array(); $aliasedIndexes = array();
foreach ($aliasesInfo as $indexName => $indexInfo) { foreach ($aliasesInfo as $indexName => $indexInfo) {
if ($indexName == $aliasName) {
throw new AliasIsIndexException($indexName);
}
$aliases = array_keys($indexInfo['aliases']); $aliases = array_keys($indexInfo['aliases']);
if (in_array($aliasName, $aliases)) { if (in_array($aliasName, $aliases)) {
$aliasedIndexes[] = $indexName; $aliasedIndexes[] = $indexName;
@ -133,4 +147,15 @@ class AliasProcessor
return $aliasedIndexes; return $aliasedIndexes;
} }
/**
* Delete an index
*
* @param string $indexName Index name to delete
*/
private function deleteIndex($client, $indexName)
{
$path = sprintf("%s", $indexName);
$client->request($path, \Elastica\Request::DELETE);
}
} }

View file

@ -45,10 +45,10 @@ class Resetter
/** /**
* Deletes and recreates all indexes * Deletes and recreates all indexes
*/ */
public function resetAllIndexes($populating = false) public function resetAllIndexes($populating = false, $force = false)
{ {
foreach ($this->configManager->getIndexNames() as $name) { foreach ($this->configManager->getIndexNames() as $name) {
$this->resetIndex($name, $populating); $this->resetIndex($name, $populating, $force);
} }
} }
@ -58,9 +58,10 @@ class Resetter
* *
* @param string $indexName * @param string $indexName
* @param bool $populating * @param bool $populating
* @param bool $force If index exists with same name as alias, remove it
* @throws \InvalidArgumentException if no index exists for the given name * @throws \InvalidArgumentException if no index exists for the given name
*/ */
public function resetIndex($indexName, $populating = false) public function resetIndex($indexName, $populating = false, $force = false)
{ {
$indexConfig = $this->configManager->getIndexConfiguration($indexName); $indexConfig = $this->configManager->getIndexConfiguration($indexName);
$index = $this->indexManager->getIndex($indexName); $index = $this->indexManager->getIndex($indexName);
@ -73,7 +74,7 @@ class Resetter
$index->create($mapping, true); $index->create($mapping, true);
if (!$populating and $indexConfig->isUseAlias()) { if (!$populating and $indexConfig->isUseAlias()) {
$this->aliasProcessor->switchIndexAlias($indexConfig, $index); $this->aliasProcessor->switchIndexAlias($indexConfig, $index, $force);
} }
} }