Skip to content

Commit

Permalink
Merge branch 'feature-index-name-prefix'
Browse files Browse the repository at this point in the history
  • Loading branch information
babenkoivan committed Jun 10, 2020
2 parents d4dc1f4 + c61647d commit 6ba334b
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 34 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ php artisan vendor:publish --provider="ElasticClient\ServiceProvider"
You can change Elasticsearch host and other client settings in the `config/elastic.client.php` file. Please refer to
[babenkoivan/elastic-client](https://github.com/babenkoivan/elastic-client) for more details.

If you want to change the migrations **default table name** or **migrations directory**, publish Elastic Migrations settings as well:
If you want to change the migration **default table name**, the **migrations directory** or set an **index name prefix**,
publish Elastic Migrations settings as well:

```bash
php artisan vendor:publish --provider="ElasticMigrations\ServiceProvider"
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
"autoload": {
"psr-4": {
"ElasticMigrations\\": "src"
}
},
"files": [
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
3 changes: 2 additions & 1 deletion config/elastic.migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

return [
'table' => env('ELASTIC_MIGRATIONS_TABLE', 'elastic_migrations'),
'storage_directory' => env('ELASTIC_MIGRATIONS_DIRECTORY', base_path('elastic/migrations'))
'storage_directory' => env('ELASTIC_MIGRATIONS_DIRECTORY', base_path('elastic/migrations')),
'index_name_prefix' => env('ELASTIC_MIGRATIONS_INDEX_NAME_PREFIX', ''),
];
33 changes: 24 additions & 9 deletions src/Adapters/IndexManagerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use ElasticAdapter\Indices\Mapping;
use ElasticAdapter\Indices\Settings;
use ElasticMigrations\IndexManagerInterface;
use function ElasticMigrations\prefix_index_name;

class IndexManagerAdapter implements IndexManagerInterface
{
Expand All @@ -23,15 +24,17 @@ public function __construct(IndexManager $indexManager)

public function create(string $indexName, ?callable $modifier = null): IndexManagerInterface
{
$prefixedIndexName = prefix_index_name($indexName);

if (isset($modifier)) {
$mapping = new Mapping();
$settings = new Settings();

$modifier($mapping, $settings);

$index = new Index($indexName, $mapping, $settings);
$index = new Index($prefixedIndexName, $mapping, $settings);
} else {
$index = new Index($indexName);
$index = new Index($prefixedIndexName);
}

$this->indexManager->create($index);
Expand All @@ -41,7 +44,9 @@ public function create(string $indexName, ?callable $modifier = null): IndexMana

public function createIfNotExists(string $indexName, ?callable $modifier = null): IndexManagerInterface
{
if (!$this->indexManager->exists($indexName)) {
$prefixedIndexName = prefix_index_name($indexName);

if (!$this->indexManager->exists($prefixedIndexName)) {
$this->create($indexName, $modifier);
}

Expand All @@ -50,41 +55,51 @@ public function createIfNotExists(string $indexName, ?callable $modifier = null)

public function putMapping(string $indexName, callable $modifier): IndexManagerInterface
{
$prefixedIndexName = prefix_index_name($indexName);

$mapping = new Mapping();
$modifier($mapping);
$this->indexManager->putMapping($indexName, $mapping);
$this->indexManager->putMapping($prefixedIndexName, $mapping);

return $this;
}

public function putSettings(string $indexName, callable $modifier): IndexManagerInterface
{
$prefixedIndexName = prefix_index_name($indexName);

$settings = new Settings();
$modifier($settings);
$this->indexManager->putSettings($indexName, $settings);
$this->indexManager->putSettings($prefixedIndexName, $settings);

return $this;
}

public function putSettingsHard(string $indexName, callable $modifier): IndexManagerInterface
{
$this->indexManager->close($indexName);
$prefixedIndexName = prefix_index_name($indexName);

$this->indexManager->close($prefixedIndexName);
$this->putSettings($indexName, $modifier);
$this->indexManager->open($indexName);
$this->indexManager->open($prefixedIndexName);

return $this;
}

public function drop(string $indexName): IndexManagerInterface
{
$this->indexManager->drop($indexName);
$prefixedIndexName = prefix_index_name($indexName);

$this->indexManager->drop($prefixedIndexName);

return $this;
}

public function dropIfExists(string $indexName): IndexManagerInterface
{
if ($this->indexManager->exists($indexName)) {
$prefixedIndexName = prefix_index_name($indexName);

if ($this->indexManager->exists($prefixedIndexName)) {
$this->drop($indexName);
}

Expand Down
9 changes: 9 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace ElasticMigrations;

function prefix_index_name(string $indexName): string
{
return config('elastic.migrations.index_name_prefix').$indexName;
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php
declare(strict_types=1);

namespace ElasticMigrations\Tests\Unit\Adapters;
namespace ElasticMigrations\Tests\Integration\Adapters;

use ElasticAdapter\Indices\Index;
use ElasticAdapter\Indices\IndexManager;
use ElasticAdapter\Indices\Mapping;
use ElasticAdapter\Indices\Settings;
use ElasticMigrations\Adapters\IndexManagerAdapter;
use ElasticMigrations\Tests\Integration\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* @covers \ElasticMigrations\Adapters\IndexManagerAdapter
Expand All @@ -33,20 +33,30 @@ protected function setUp(): void
$this->indexManagerAdapter = new IndexManagerAdapter($this->indexManagerMock);
}

public function test_index_can_be_created_without_modifier(): void
/**
* @dataProvider indexNamePrefixProvider
*/
public function test_index_can_be_created_without_modifier(string $indexNamePrefix): void
{
$this->app['config']->set('elastic.migrations.index_name_prefix', $indexNamePrefix);

$indexName = 'test';

$this->indexManagerMock
->expects($this->once())
->method('create')
->with(new Index($indexName));
->with(new Index($indexNamePrefix.$indexName));

$this->indexManagerAdapter->create($indexName);
}

public function test_index_can_be_created_with_modifier(): void
/**
* @dataProvider indexNamePrefixProvider
*/
public function test_index_can_be_created_with_modifier(string $indexNamePrefix): void
{
$this->app['config']->set('elastic.migrations.index_name_prefix', $indexNamePrefix);

$indexName = 'test';

$modifier = function (Mapping $mapping, Settings $settings) {
Expand All @@ -58,33 +68,44 @@ public function test_index_can_be_created_with_modifier(): void
->expects($this->once())
->method('create')
->with(new Index(
$indexName,
$indexNamePrefix.$indexName,
(new Mapping())->text('title'),
(new Settings())->numberOfReplicas(2)
));

$this->indexManagerAdapter->create($indexName, $modifier);
}

public function test_index_can_be_created_only_if_it_does_not_exist(): void
/**
* @dataProvider indexNamePrefixProvider
*/
public function test_index_can_be_created_only_if_it_does_not_exist(string $indexNamePrefix): void
{
$this->app['config']->set('elastic.migrations.index_name_prefix', $indexNamePrefix);

$indexName = 'test';

$this->indexManagerMock
->expects($this->once())
->method('exists')
->with($indexName)
->with($indexNamePrefix.$indexName)
->willReturn(false);

$this->indexManagerMock
->expects($this->once())
->method('create');
->method('create')
->with(new Index($indexNamePrefix.$indexName));

$this->indexManagerAdapter->createIfNotExists($indexName);
}

public function test_mapping_can_be_updated(): void
/**
* @dataProvider indexNamePrefixProvider
*/
public function test_mapping_can_be_updated(string $indexNamePrefix): void
{
$this->app['config']->set('elastic.migrations.index_name_prefix', $indexNamePrefix);

$indexName = 'test';

$modifier = function (Mapping $mapping) {
Expand All @@ -95,15 +116,20 @@ public function test_mapping_can_be_updated(): void
->expects($this->once())
->method('putMapping')
->with(
$indexName,
$indexNamePrefix.$indexName,
(new Mapping())->disableSource()->text('title')
);

$this->indexManagerAdapter->putMapping($indexName, $modifier);
}

public function test_settings_can_be_updated(): void
/**
* @dataProvider indexNamePrefixProvider
*/
public function test_settings_can_be_updated(string $indexNamePrefix): void
{
$this->app['config']->set('elastic.migrations.index_name_prefix', $indexNamePrefix);

$indexName = 'test';

$modifier = function (Settings $settings) {
Expand All @@ -114,15 +140,20 @@ public function test_settings_can_be_updated(): void
->expects($this->once())
->method('putSettings')
->with(
$indexName,
$indexNamePrefix.$indexName,
(new Settings())->numberOfReplicas(2)->refreshInterval(-1)
);

$this->indexManagerAdapter->putSettings($indexName, $modifier);
}

public function test_settings_can_be_updated_in_a_hard_way(): void
/**
* @dataProvider indexNamePrefixProvider
*/
public function test_settings_can_be_updated_in_a_hard_way(string $indexNamePrefix): void
{
$this->app['config']->set('elastic.migrations.index_name_prefix', $indexNamePrefix);

$indexName = 'test';

$modifier = function (Settings $settings) {
Expand All @@ -132,51 +163,70 @@ public function test_settings_can_be_updated_in_a_hard_way(): void
$this->indexManagerMock
->expects($this->once())
->method('close')
->with($indexName);
->with($indexNamePrefix.$indexName);

$this->indexManagerMock
->expects($this->once())
->method('putSettings')
->with(
$indexName,
$indexNamePrefix.$indexName,
(new Settings())->numberOfReplicas(2)
);

$this->indexManagerMock
->expects($this->once())
->method('open')
->with($indexName);
->with($indexNamePrefix.$indexName);

$this->indexManagerAdapter->putSettingsHard($indexName, $modifier);
}

public function test_index_can_be_dropped(): void
/**
* @dataProvider indexNamePrefixProvider
*/
public function test_index_can_be_dropped(string $indexNamePrefix): void
{
$this->app['config']->set('elastic.migrations.index_name_prefix', $indexNamePrefix);

$indexName = 'test';

$this->indexManagerMock
->expects($this->once())
->method('drop')
->with($indexName);
->with($indexNamePrefix.$indexName);

$this->indexManagerAdapter->drop($indexName);
}

public function test_index_can_be_dropped_only_if_exists(): void
/**
* @dataProvider indexNamePrefixProvider
*/
public function test_index_can_be_dropped_only_if_exists(string $indexNamePrefix): void
{
$this->app['config']->set('elastic.migrations.index_name_prefix', $indexNamePrefix);

$indexName = 'test';

$this->indexManagerMock
->expects($this->once())
->method('exists')
->with($indexName)
->with($indexNamePrefix.$indexName)
->willReturn(true);

$this->indexManagerMock
->expects($this->once())
->method('drop')
->with($indexName);
->with($indexNamePrefix.$indexName);

$this->indexManagerAdapter->dropIfExists($indexName);
}

public function indexNamePrefixProvider(): array
{
return [
'no prefix' => [''],
'short prefix' => ['foo_'],
'long prefix' => ['foo_bar_'],
];
}
}

0 comments on commit 6ba334b

Please sign in to comment.