Skip to content

Commit

Permalink
Merge branch 'v2.0'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Console/FreshCommand.php
#	src/Console/MakeCommand.php
#	src/Console/MigrateCommand.php
#	src/Console/RefreshCommand.php
#	src/Console/ResetCommand.php
#	src/Console/RollbackCommand.php
#	src/Console/StatusCommand.php
  • Loading branch information
babenkoivan committed Oct 5, 2021
2 parents 358e205 + 9c76897 commit af3ce2b
Show file tree
Hide file tree
Showing 20 changed files with 323 additions and 84 deletions.
12 changes: 4 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@ jobs:
runs-on: ubuntu-18.04
strategy:
matrix:
php: [7.2, 7.3, 7.4, 8.0]
php: [7.3, 7.4, 8.0]
include:
- php: 7.2
testbench: 4.0
phpunit: 8.5

- php: 7.3
testbench: 5.0
testbench: 4.0
phpunit: 9.5

- php: 7.4
testbench: 6.0
testbench: 5.0
phpunit: 9.5

- php: 8.0
Expand Down Expand Up @@ -47,7 +43,7 @@ jobs:
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
run: composer require --no-interaction orchestra/testbench:^${{ matrix.testbench }} phpunit/phpunit:^${{ matrix.phpunit }}
run: composer require --no-interaction --dev orchestra/testbench:^${{ matrix.testbench }} phpunit/phpunit:^${{ matrix.phpunit }}

- name: Run tests
run: make test
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ coverage: ## Run tests and generate the code coverage report

style-check: ## Check the code style
@printf "\033[93m→ Checking the code style\033[0m\n"
@bin/php-cs-fixer fix --allow-risky=yes --dry-run --diff-format=udiff --show-progress=dots --verbose
@bin/php-cs-fixer fix --allow-risky=yes --dry-run --diff --show-progress=dots --verbose
@printf "\n\033[92m✔︎ Code style is checked\033[0m\n"

static-analysis: ## Do static code analysis
Expand Down
105 changes: 86 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

---

Elasticsearch migrations for Laravel allow you to easily modify and share indices schema across the application's environments.
Elastic Migrations for Laravel allow you to easily modify and share indices schema across the application's environments.

## Contents

Expand All @@ -33,7 +33,7 @@ Elasticsearch migrations for Laravel allow you to easily modify and share indice

The current version of Elastic Migrations has been tested with the following configuration:

* PHP 7.2-8.0
* PHP 7.3-8.0
* Elasticsearch 7.x
* Laravel 6.x-8.x

Expand All @@ -59,14 +59,19 @@ 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 **table name**, the default **migrations directory** or set **prefixes** for indices
and aliases, publish Elastic Migrations settings as well:
You can also publish Elastic Migrations settings:

```bash
php artisan vendor:publish --provider="ElasticMigrations\ServiceProvider"
```

The published configuration can be found in the `config/elastic.migrations.php` file.
This will create the `config/elastic.migrations.php` file, which allows you to configure the following options:

* `table` - the migration table name
* `connection` - the database connection
* `storage_directory` - the migrations directory
* `index_name_prefix` - the indices prefix
* `alias_name_prefix` - the aliases prefix

Finally, don't forget to run Laravel database migrations to create Elastic Migrations table:

Expand All @@ -90,18 +95,18 @@ You can use `ElasticMigrations\Facades\Index` facade to perform basic operations

#### Create Index

Create an index with the default settings:
You can create an index with the default settings:

```php
Index::create('my-index');
```

or use a modifier to configure mapping and settings:
You can use a modifier to configure mapping and settings:

```php
Index::create('my-index', function (Mapping $mapping, Settings $settings) {
// to add a new field to the mapping use method name as a field type (in Camel Case),
// first argument as a field name and optional second argument as additional field parameters
// first argument as a field name and optional second argument for additional field parameters
$mapping->text('title', ['boost' => 2]);
$mapping->float('price');

Expand All @@ -113,13 +118,12 @@ Index::create('my-index', function (Mapping $mapping, Settings $settings) {
],
]);

// you can also change the index settings
// you can also change the index settings and the analysis configuration
$settings->index([
'number_of_replicas' => 2,
'refresh_interval' => -1
]);

// and analysis configuration
$settings->analysis([
'analyzer' => [
'title' => [
Expand All @@ -131,15 +135,36 @@ Index::create('my-index', function (Mapping $mapping, Settings $settings) {
});
```

There is also an option to create an index only if it doesn't exist:
There is also the `createRaw` method in your disposal:

```php
Index::createIfNotExists('my-index');
```
$mapping = [
'properties' => [
'title' => [
'type' => 'text'
]
]
];

$settings = [
'number_of_replicas' => 2
];

Index::createRaw('my-index', $mapping, $settings);
```

Finally, it is possible to create an index only if it doesn't exist:

```php
// you can use a modifier as shown above
Index::createIfNotExists('my-index', $modifier);
// or you can use raw mapping and settings
Index::createIfNotExistsRaw('my-index', $mapping, $settings);
```

#### Update Mapping

Use the modifier to adjust the mapping:
You can use a modifier to adjust the mapping:

```php
Index::putMapping('my-index', function (Mapping $mapping) {
Expand All @@ -148,9 +173,25 @@ Index::putMapping('my-index', function (Mapping $mapping) {
});
```

Alternatively, you can use the `putMappingRaw` method as follows:

```php
Index::putMappingRaw('my-index', [
'properties' => [
'title' => [
'type' => 'text',
'boost' => 2
],
'price' => [
'price' => 'float'
]
]
]);
```

#### Update Settings

Use the modifier to change the index configuration:
You can use a modifier to change an index configuration:

```php
Index::putSettings('my-index', function (Settings $settings) {
Expand All @@ -161,11 +202,22 @@ Index::putSettings('my-index', function (Settings $settings) {
});
```

You can update analysis settings only on closed indices. The `putSettingsHard` method closes the index, updates the configuration and
opens the index again:
The same result can be achieved with the `putSettingsRaw` method:

```php
Index::putSettingsHard('my-index', function (Settings $settings) {
Index::putSettingsRaw('my-index', [
'index' => [
'number_of_replicas' => 2,
'refresh_interval' => -1
]
]);
```

It is possible to update analysis settings only on closed indices. The `pushSettings` method closes the index,
updates the configuration and opens the index again:

```php
Index::pushSettings('my-index', function (Settings $settings) {
$settings->analysis([
'analyzer' => [
'title' => [
Expand All @@ -175,7 +227,22 @@ Index::putSettingsHard('my-index', function (Settings $settings) {
]
]);
});
```
```

The same can be done with the `pushSettingsRaw` method:

```php
Index::pushSettingsRaw('my-index', [
'analysis' => [
'analyzer' => [
'title' => [
'type' => 'custom',
'tokenizer' => 'whitespace'
]
]
]
]);
```

#### Drop Index

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@
}
},
"require": {
"php": "^7.2 || ^8.0",
"php": "^7.3 || ^8.0",
"babenkoivan/elastic-client": "^1.2",
"babenkoivan/elastic-adapter": "^1.14"
"babenkoivan/elastic-adapter": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"orchestra/testbench": "^6.12",
"friendsofphp/php-cs-fixer": "^2.16",
"friendsofphp/php-cs-fixer": "^3.0",
"phpstan/phpstan": "^0.12.32"
},
"config": {
Expand Down
1 change: 1 addition & 0 deletions config/elastic.migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

return [
'table' => env('ELASTIC_MIGRATIONS_TABLE', 'elastic_migrations'),
'connection' => env('ELASTIC_MIGRATIONS_CONNECTION'),
'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', '')),
Expand Down
62 changes: 58 additions & 4 deletions src/Adapters/IndexManagerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace ElasticMigrations\Adapters;

use ElasticAdapter\Indices\Alias;
use ElasticAdapter\Indices\Index;
use ElasticAdapter\Indices\IndexBlueprint;
use ElasticAdapter\Indices\IndexManager;
use ElasticAdapter\Indices\Mapping;
use ElasticAdapter\Indices\Settings;
Expand Down Expand Up @@ -33,16 +33,25 @@ public function create(string $indexName, ?callable $modifier = null): IndexMana

$modifier($mapping, $settings);

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

$this->indexManager->create($index);

return $this;
}

public function createRaw(string $indexName, ?array $mapping = null, ?array $settings = null): IndexManagerInterface
{
$prefixedIndexName = prefix_index_name($indexName);

$this->indexManager->createRaw($prefixedIndexName, $mapping, $settings);

return $this;
}

public function createIfNotExists(string $indexName, ?callable $modifier = null): IndexManagerInterface
{
$prefixedIndexName = prefix_index_name($indexName);
Expand All @@ -54,29 +63,63 @@ public function createIfNotExists(string $indexName, ?callable $modifier = null)
return $this;
}

public function createIfNotExistsRaw(
string $indexName,
?array $mapping = null,
?array $settings = null
): IndexManagerInterface {
$prefixedIndexName = prefix_index_name($indexName);

if (!$this->indexManager->exists($prefixedIndexName)) {
$this->createRaw($indexName, $mapping, $settings);
}

return $this;
}

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

$mapping = new Mapping();
$modifier($mapping);

$this->indexManager->putMapping($prefixedIndexName, $mapping);

return $this;
}

public function putMappingRaw(string $indexName, array $mapping): IndexManagerInterface
{
$prefixedIndexName = prefix_index_name($indexName);

$this->indexManager->putMappingRaw($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($prefixedIndexName, $settings);

return $this;
}

public function putSettingsHard(string $indexName, callable $modifier): IndexManagerInterface
public function putSettingsRaw(string $indexName, array $settings): IndexManagerInterface
{
$prefixedIndexName = prefix_index_name($indexName);

$this->indexManager->putSettingsRaw($prefixedIndexName, $settings);

return $this;
}

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

Expand All @@ -87,6 +130,17 @@ public function putSettingsHard(string $indexName, callable $modifier): IndexMan
return $this;
}

public function pushSettingsRaw(string $indexName, array $settings): IndexManagerInterface
{
$prefixedIndexName = prefix_index_name($indexName);

$this->indexManager->close($prefixedIndexName);
$this->putSettingsRaw($indexName, $settings);
$this->indexManager->open($prefixedIndexName);

return $this;
}

public function drop(string $indexName): IndexManagerInterface
{
$prefixedIndexName = prefix_index_name($indexName);
Expand Down
5 changes: 1 addition & 4 deletions src/Console/FreshCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ class FreshCommand extends Command
*/
protected $description = 'Drop all indices and re-run all migrations';

/**
* @return int
*/
public function handle(
Migrator $migrator,
MigrationRepository $migrationRepository,
IndexManagerInterface $indexManager
) {
): int {
$migrator->setOutput($this->output);

if (! $this->confirmToProceed() || ! $migrator->isReady()) {
Expand Down
Loading

0 comments on commit af3ce2b

Please sign in to comment.