diff --git a/Command/IndexCreateCommand.php b/Command/IndexCreateCommand.php index f2ba2146..b200426a 100644 --- a/Command/IndexCreateCommand.php +++ b/Command/IndexCreateCommand.php @@ -38,7 +38,13 @@ protected function configure() InputOption::VALUE_NONE, 'If the time suffix is used, its nice to create an alias to the configured index name.' ) - ->addOption('no-mapping', null, InputOption::VALUE_NONE, 'Do not include mapping'); + ->addOption('no-mapping', 'nm', InputOption::VALUE_NONE, 'Do not include mapping') + ->addOption( + 'if-not-exists', + null, + InputOption::VALUE_NONE, + 'Don\'t trigger an error, when the index already exists' + ); } /** @@ -55,6 +61,18 @@ protected function execute(InputInterface $input, OutputInterface $output) $finder->setNextFreeIndex($manager); } + if ($input->getOption('if-not-exists') && $manager->indexExists()) { + $output->writeln( + sprintf( + 'Index `%s` already exists in `%s` manager.', + $manager->getIndexName(), + $input->getOption('manager') + ) + ); + + return 0; + } + $manager->createIndex($input->getOption('no-mapping')); $output->writeln( diff --git a/Resources/doc/commands.md b/Resources/doc/commands.md index 5662bf8e..e3f2d083 100644 --- a/Resources/doc/commands.md +++ b/Resources/doc/commands.md @@ -8,12 +8,13 @@ Command name: `ongr:es:index:create` Creates a new index in Elasticsearch (including with mapping if not skipped) for the specified manager (see: [configuration chapter](configuration.md)). -| Options | Value | What it does | -|:--------------:|:----------------------------:|:--------------------------------------------------------------------------------------:| -| `--manager` | *Manager name. e.g.* `default` | Used to select manager to create index for. If not specified, default manager is used. | -| `--time` | *none* | Creates an index with current timestamp appended to its name. | -| `--alias` | *none* | Creates an alias with index name specified in the configuration. | -| `--no-mapping` | *none* | Skips the mapping configuration on index create action. | +| Options | Short name | Value | What it does | +|:--------------:|:----------:|:----------------------------:|:--------------------------------------------------------------------------------------:| +| `--manager` | `-mng` | *Manager name. e.g.* `default` | Used to select manager to create index for. If not specified, default manager is used. | +| `--time` | `-t` | *not required* | Creates an index with current timestamp appended to its name. | +| `--alias` | `-a` | *not required* | Creates an alias with index name specified in the configuration. | +| `--no-mapping` | `-nm` | *not required* | Skips the mapping configuration on index create action. | +| `--if-not-exists` | *no value* | *not required* | Skips an index creation, when the index already exists. | If you want to use timestabale indexes it's very handy to use it together with `-a` option. `-t` adds a date as the suffix and `-a` adds an alias as defined index name in manager configuration. So the code will work fine without any configuration changes, you dont need to do any other actions. diff --git a/Tests/Functional/Command/CreateIndexCommandTest.php b/Tests/Functional/Command/CreateIndexCommandTest.php index a75c547e..b0fd7b0d 100644 --- a/Tests/Functional/Command/CreateIndexCommandTest.php +++ b/Tests/Functional/Command/CreateIndexCommandTest.php @@ -40,6 +40,41 @@ public function getTestExecuteData() ]; } + /** + * Tests creating index in case of existing this index. Configuration from tests yaml. + */ + public function testExecuteWhenIndexExists() + { + $manager = $this->getManager(); + + if (!$manager->indexExists()) { + $manager->createIndex(); + } + + // Initialize command + $commandName = 'ongr:es:index:create'; + $commandTester = $this->getCommandTester($commandName); + $options = []; + $arguments['command'] = $commandName; + $arguments['--manager'] = $manager->getName(); + $arguments['--if-not-exists'] = null; + + // Test if the command returns 0 or not + $this->assertSame( + 0, + $commandTester->execute($arguments, $options) + ); + + $expectedOutput = sprintf( + 'Index `%s` already exists in `%s` manager.', + $manager->getIndexName(), + $manager->getName() + ); + + // Test if the command output matches the expected output or not + $this->assertStringMatchesFormat($expectedOutput . '%a', $commandTester->getDisplay()); + } + /** * Tests creating index. Configuration from tests yaml. * @@ -115,13 +150,10 @@ public function testAliasIsCreatedCorrectly() */ protected function runIndexCreateCommand($managerName, array $arguments = [], array $options = []) { - $app = new Application(); - $app->add($this->getCreateCommand()); - // Creates index. - $command = $app->find('ongr:es:index:create'); - $commandTester = new CommandTester($command); - $arguments['command'] = $command->getName(); + $commandName = 'ongr:es:index:create'; + $commandTester = $this->getCommandTester($commandName); + $arguments['command'] = $commandName; $arguments['--manager'] = $managerName; $commandTester->execute($arguments, $options); @@ -139,4 +171,21 @@ protected function getCreateCommand() return $command; } + + /** + * Returns command tester. + * @param string commandName + * + * @return CommandTester + */ + protected function getCommandTester($commandName) + { + $app = new Application(); + $app->add($this->getCreateCommand()); + + $command = $app->find($commandName); + $commandTester = new CommandTester($command); + + return $commandTester; + } }