Skip to content

Commit

Permalink
Add aliases support
Browse files Browse the repository at this point in the history
  • Loading branch information
babenkoivan committed Apr 7, 2021
1 parent 3e371dc commit f2e7704
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 12 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,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 migration **default table name**, the **migrations directory** or set an **index name prefix**,
publish Elastic Migrations settings as well:
If you want to change the migration **table name**, the default **migrations directory** or set **prefixes** for indices
and aliases, publish Elastic Migrations settings as well:

```bash
php artisan vendor:publish --provider="ElasticMigrations\ServiceProvider"
Expand Down Expand Up @@ -190,6 +190,22 @@ or delete it only if it exists:
Index::dropIfExists('my-index');
```

#### Create Alias

You can create an alias with optional filter query:

```php
Index::putAlias('my-index', 'my-alias', ['term' => ['user_id' => 1]]);
```

#### Delete Alias

You can delete an alias by its name:

```php
Index::deleteAlias('my-index', 'my-alias');
```

#### More

Finally, you are free to inject `Elasticsearch\Client` in the migration constructor and execute any supported by client actions.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"require": {
"php": "^7.2 || ^8.0",
"babenkoivan/elastic-client": "^1.2",
"babenkoivan/elastic-adapter": "^1.13"
"babenkoivan/elastic-adapter": "dev-feature-aliases as 1.14"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
Expand Down
1 change: 1 addition & 0 deletions config/elastic.migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
'table' => env('ELASTIC_MIGRATIONS_TABLE', 'elastic_migrations'),
'storage_directory' => env('ELASTIC_MIGRATIONS_DIRECTORY', base_path('elastic/migrations')),
'index_name_prefix' => env('ELASTIC_MIGRATIONS_INDEX_NAME_PREFIX', env('SCOUT_PREFIX', '')),
'alias_name_prefix' => env('ELASTIC_MIGRATIONS_ALIAS_NAME_PREFIX', env('SCOUT_PREFIX', '')),
];
22 changes: 22 additions & 0 deletions src/Adapters/IndexManagerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace ElasticMigrations\Adapters;

use ElasticAdapter\Indices\Alias;
use ElasticAdapter\Indices\Index;
use ElasticAdapter\Indices\IndexManager;
use ElasticAdapter\Indices\Mapping;
use ElasticAdapter\Indices\Settings;
use ElasticMigrations\IndexManagerInterface;
use function ElasticMigrations\prefix_alias_name;
use function ElasticMigrations\prefix_index_name;

class IndexManagerAdapter implements IndexManagerInterface
Expand Down Expand Up @@ -104,4 +106,24 @@ public function dropIfExists(string $indexName): IndexManagerInterface

return $this;
}

public function putAlias(string $indexName, string $aliasName, array $filter = null): IndexManagerInterface
{
$prefixedIndexName = prefix_index_name($indexName);
$prefixedAliasName = prefix_alias_name($aliasName);

$this->indexManager->putAlias($prefixedIndexName, new Alias($prefixedAliasName, $filter));

return $this;
}

public function deleteAlias(string $indexName, string $aliasName): IndexManagerInterface
{
$prefixedIndexName = prefix_index_name($indexName);
$prefixedAliasName = prefix_alias_name($aliasName);

$this->indexManager->deleteAlias($prefixedIndexName, $prefixedAliasName);

return $this;
}
}
5 changes: 5 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ function prefix_index_name(string $indexName): string
{
return config('elastic.migrations.index_name_prefix') . $indexName;
}

function prefix_alias_name(string $aliasName): string
{
return config('elastic.migrations.alias_name_prefix') . $aliasName;
}
55 changes: 46 additions & 9 deletions tests/Integration/Adapters/IndexManagerAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ElasticMigrations\Tests\Integration\Adapters;

use ElasticAdapter\Indices\Alias;
use ElasticAdapter\Indices\Index;
use ElasticAdapter\Indices\IndexManager;
use ElasticAdapter\Indices\Mapping;
Expand Down Expand Up @@ -33,7 +34,7 @@ protected function setUp(): void
}

/**
* @dataProvider indexNamePrefixProvider
* @dataProvider prefixProvider
*/
public function test_index_can_be_created_without_modifier(string $indexNamePrefix): void
{
Expand All @@ -50,7 +51,7 @@ public function test_index_can_be_created_without_modifier(string $indexNamePref
}

/**
* @dataProvider indexNamePrefixProvider
* @dataProvider prefixProvider
*/
public function test_index_can_be_created_with_modifier(string $indexNamePrefix): void
{
Expand All @@ -76,7 +77,7 @@ public function test_index_can_be_created_with_modifier(string $indexNamePrefix)
}

/**
* @dataProvider indexNamePrefixProvider
* @dataProvider prefixProvider
*/
public function test_index_can_be_created_only_if_it_does_not_exist(string $indexNamePrefix): void
{
Expand All @@ -99,7 +100,7 @@ public function test_index_can_be_created_only_if_it_does_not_exist(string $inde
}

/**
* @dataProvider indexNamePrefixProvider
* @dataProvider prefixProvider
*/
public function test_mapping_can_be_updated(string $indexNamePrefix): void
{
Expand All @@ -123,7 +124,7 @@ public function test_mapping_can_be_updated(string $indexNamePrefix): void
}

/**
* @dataProvider indexNamePrefixProvider
* @dataProvider prefixProvider
*/
public function test_settings_can_be_updated(string $indexNamePrefix): void
{
Expand All @@ -147,7 +148,7 @@ public function test_settings_can_be_updated(string $indexNamePrefix): void
}

/**
* @dataProvider indexNamePrefixProvider
* @dataProvider prefixProvider
*/
public function test_settings_can_be_updated_in_a_hard_way(string $indexNamePrefix): void
{
Expand Down Expand Up @@ -181,7 +182,7 @@ public function test_settings_can_be_updated_in_a_hard_way(string $indexNamePref
}

/**
* @dataProvider indexNamePrefixProvider
* @dataProvider prefixProvider
*/
public function test_index_can_be_dropped(string $indexNamePrefix): void
{
Expand All @@ -198,7 +199,7 @@ public function test_index_can_be_dropped(string $indexNamePrefix): void
}

/**
* @dataProvider indexNamePrefixProvider
* @dataProvider prefixProvider
*/
public function test_index_can_be_dropped_only_if_exists(string $indexNamePrefix): void
{
Expand All @@ -220,7 +221,43 @@ public function test_index_can_be_dropped_only_if_exists(string $indexNamePrefix
$this->indexManagerAdapter->dropIfExists($indexName);
}

public function indexNamePrefixProvider(): array
/**
* @dataProvider prefixProvider
*/
public function test_alias_can_be_created(string $aliasNamePrefix): void
{
$this->app['config']->set('elastic.migrations.alias_name_prefix', $aliasNamePrefix);

$indexName = 'foo';
$aliasName = 'bar';

$this->indexManagerMock
->expects($this->once())
->method('putAlias')
->with($indexName, new Alias($aliasNamePrefix . $aliasName));

$this->indexManagerAdapter->putAlias($indexName, $aliasName);
}

/**
* @dataProvider prefixProvider
*/
public function test_alias_can_be_deleted(string $aliasNamePrefix): void
{
$this->app['config']->set('elastic.migrations.alias_name_prefix', $aliasNamePrefix);

$indexName = 'foo';
$aliasName = 'bar';

$this->indexManagerMock
->expects($this->once())
->method('deleteAlias')
->with($indexName, $aliasNamePrefix . $aliasName);

$this->indexManagerAdapter->deleteAlias($indexName, $aliasName);
}

public function prefixProvider(): array
{
return [
'no prefix' => [''],
Expand Down

0 comments on commit f2e7704

Please sign in to comment.