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 all 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', 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'
);
}

/**
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
13 changes: 7 additions & 6 deletions Resources/doc/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
61 changes: 55 additions & 6 deletions Tests/Functional/Command/CreateIndexCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
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);
$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.
*
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
}