Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the option '--if-not-exists' in the command 'ongr:es:index:create' #535

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion Command/IndexCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', 'nm', 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'
);
}

/**
Expand All @@ -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(
'<info>Index `<comment>%s</comment>` already exists in `<comment>%s</comment>` manager.</info>',
$manager->getIndexName(),
$input->getOption('manager')
)
);

return 0;
}

$manager->createIndex($input->getOption('no-mapping'));

$output->writeln(
Expand Down
1 change: 1 addition & 0 deletions Resources/doc/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Creates a new index in Elasticsearch (including with mapping if not skipped) for
| `--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.

Expand Down
31 changes: 31 additions & 0 deletions Tests/Functional/Command/CreateIndexCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,37 @@ public function testExecute($managerName, $arguments, $options)
$manager->dropIndex();
}

/**
* Tests creating index in case of existing this index. Configuration from tests yaml.
*
* @param string $managerName
* @param array $arguments
* @param array $options
*
* @dataProvider getTestExecuteData
*/
public function testExecuteWithIndexExistence($managerName, $arguments, $options)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not use different test case data provider and try to behave inside test case to match expectations. To test --if-not-exists please write separate test case, the data provider is not necessary.

{
$manager = $this->getManager($managerName);

if (!$manager->indexExists()) {
$manager->createIndex();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Index is automatically created when you call $this->getManager().

}

try {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you will write test case, we do not need exception catch here.

$arguments['--if-not-exists'] = null;
$this->runIndexCreateCommand($managerName, $arguments, $options);
} catch (\Exception $ex) {
$message = $ex->getMessage();
$expectedClassName = 'Elasticsearch\\Common\\Exceptions\\BadRequest400Exception';
$isExpectedException = $ex instanceof $expectedClassName;
if ($isExpectedException) {
$this->assertNotContains('IndexAlreadyExistsException', $message);
}
}
$manager->dropIndex();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created indexes are automatically removed after each test case.

}

/**
* Tests if right exception is thrown when manager is read only.
*
Expand Down