From 2958833012b977ab6f6a831e03bdc28c8c5d6279 Mon Sep 17 00:00:00 2001 From: Patrick McAndrew Date: Thu, 28 Aug 2014 17:59:58 +0100 Subject: [PATCH] Ability to delete an index if expecting an alias --- Command/ResetCommand.php | 4 +++- Exception/AliasIsIndexException.php | 12 ++++++++++++ Index/AliasProcessor.php | 29 +++++++++++++++++++++++++++-- Index/Resetter.php | 9 +++++---- 4 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 Exception/AliasIsIndexException.php diff --git a/Command/ResetCommand.php b/Command/ResetCommand.php index 06cfe48..ed14e6c 100755 --- a/Command/ResetCommand.php +++ b/Command/ResetCommand.php @@ -33,6 +33,7 @@ class ResetCommand extends ContainerAwareCommand ->setName('fos:elastica:reset') ->addOption('index', null, InputOption::VALUE_OPTIONAL, 'The index 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') ; } @@ -53,6 +54,7 @@ class ResetCommand extends ContainerAwareCommand { $index = $input->getOption('index'); $type = $input->getOption('type'); + $force = (true == $input->getOption('force')); if (null === $index && null !== $type) { throw new \InvalidArgumentException('Cannot specify type option without an index.'); @@ -69,7 +71,7 @@ class ResetCommand extends ContainerAwareCommand foreach ($indexes as $index) { $output->writeln(sprintf('Resetting %s', $index)); - $this->resetter->resetIndex($index); + $this->resetter->resetIndex($index, false, $force); } } } diff --git a/Exception/AliasIsIndexException.php b/Exception/AliasIsIndexException.php new file mode 100644 index 0000000..87f546b --- /dev/null +++ b/Exception/AliasIsIndexException.php @@ -0,0 +1,12 @@ +getClient(); @@ -45,7 +47,16 @@ class AliasProcessor $oldIndexName = false; $newIndexName = $index->getName(); - $aliasedIndexes = $this->getAliasedIndexes($client, $aliasName); + $aliasedIndexes = array(); + try { + $aliasedIndexes = $this->getAliasedIndexes($client, $aliasName); + } catch(AliasIsIndexException $e) { + if ($force) { + $this->deleteIndex($client, $aliasName); + } else { + throw new \RuntimeException($e->getMessage()); + } + } if (count($aliasedIndexes) > 1) { throw new \RuntimeException( @@ -125,6 +136,9 @@ class AliasProcessor $aliasedIndexes = array(); foreach ($aliasesInfo as $indexName => $indexInfo) { + if ($indexName == $aliasName) { + throw new AliasIsIndexException($indexName); + } $aliases = array_keys($indexInfo['aliases']); if (in_array($aliasName, $aliases)) { $aliasedIndexes[] = $indexName; @@ -133,4 +147,15 @@ class AliasProcessor 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); + } } diff --git a/Index/Resetter.php b/Index/Resetter.php index 3f07fa1..c93ae2d 100644 --- a/Index/Resetter.php +++ b/Index/Resetter.php @@ -45,10 +45,10 @@ class Resetter /** * Deletes and recreates all indexes */ - public function resetAllIndexes($populating = false) + public function resetAllIndexes($populating = false, $force = false) { 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 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 */ - public function resetIndex($indexName, $populating = false) + public function resetIndex($indexName, $populating = false, $force = false) { $indexConfig = $this->configManager->getIndexConfiguration($indexName); $index = $this->indexManager->getIndex($indexName); @@ -73,7 +74,7 @@ class Resetter $index->create($mapping, true); if (!$populating and $indexConfig->isUseAlias()) { - $this->aliasProcessor->switchIndexAlias($indexConfig, $index); + $this->aliasProcessor->switchIndexAlias($indexConfig, $index, $force); } }