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 5 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
69 changes: 63 additions & 6 deletions Tests/Functional/Command/CreateIndexCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,49 @@ 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 testExecuteWhenIndexExists($managerName, $arguments, $options)
{
$manager = $this->getManager($managerName);

// Try to create index in prior
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().

}

// Initialize command
$commandName = 'ongr:es:index:create';
$commandTester = $this->getCommandTester($commandName);
$arguments['command'] = $commandName;
$arguments['--manager'] = $managerName;
$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(),
$managerName
);

// Test if the command output matches the expected output or not
$this->assertStringMatchesFormat($expectedOutput . '%a', $commandTester->getDisplay());

$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 Expand Up @@ -115,13 +158,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);
Expand All @@ -139,4 +179,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;
}
}