Skip to content
This repository was archived by the owner on Nov 4, 2021. It is now read-only.

Commit a8ed3dd

Browse files
committed
Added migratable trait for index configurators. It allows to create write alias for indices, that will be used during migration to another index.
1 parent 3319615 commit a8ed3dd

13 files changed

+161
-41
lines changed

src/Console/ElasticIndexCreateCommand.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ class ElasticIndexCreateCommand extends Command
1515

1616
protected $description = 'Create an Elasticsearch index';
1717

18-
public function handle()
18+
protected function createIndex()
1919
{
20-
if (!$configurator = $this->getIndexConfigurator()) {
21-
return;
22-
}
20+
$configurator = $this->getIndexConfigurator();
2321

2422
$payload = (new IndexPayload($configurator))
2523
->setIfNotEmpty('body.settings', $configurator->getSettings())
@@ -34,4 +32,33 @@ public function handle()
3432
$configurator->getName()
3533
));
3634
}
35+
36+
protected function createWriteAlias()
37+
{
38+
$configurator = $this->getIndexConfigurator();
39+
40+
if (!method_exists($configurator, 'getWriteAlias')) {
41+
return;
42+
}
43+
44+
$payload = (new IndexPayload($configurator))
45+
->set('name', $configurator->getWriteAlias())
46+
->get();
47+
48+
ElasticClient::indices()
49+
->putAlias($payload);
50+
51+
$this->info(sprintf(
52+
'The %s alias for the %s index was created!',
53+
$configurator->getWriteAlias(),
54+
$configurator->getName()
55+
));
56+
}
57+
58+
public function handle()
59+
{
60+
$this->createIndex();
61+
62+
$this->createWriteAlias();
63+
}
3764
}

src/Console/ElasticIndexDropCommand.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ class ElasticIndexDropCommand extends Command
1717

1818
public function handle()
1919
{
20-
if (!$configurator = $this->getIndexConfigurator()) {
21-
return;
22-
}
20+
$configurator = $this->getIndexConfigurator();
2321

2422
$payload = (new IndexPayload($configurator))
2523
->get();

src/Console/ElasticIndexUpdateCommand.php

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace ScoutElastic\Console;
44

5+
use Exception;
6+
use LogicException;
57
use Illuminate\Console\Command;
68
use ScoutElastic\Console\Features\requiresIndexConfiguratorArgument;
79
use ScoutElastic\Facades\ElasticClient;
8-
use Exception;
910
use ScoutElastic\Payloads\IndexPayload;
1011

1112
class ElasticIndexUpdateCommand extends Command
@@ -16,23 +17,19 @@ class ElasticIndexUpdateCommand extends Command
1617

1718
protected $description = 'Update settings and mappings of an Elasticsearch index';
1819

19-
public function handle()
20+
protected function updateIndex()
2021
{
21-
if (!$configurator = $this->getIndexConfigurator()) {
22-
return;
23-
}
22+
$configurator = $this->getIndexConfigurator();
2423

2524
$indexPayload = (new IndexPayload($configurator))->get();
2625

2726
$indices = ElasticClient::indices();
2827

2928
if (!$indices->exists($indexPayload)) {
30-
$this->error(sprintf(
29+
throw new LogicException(sprintf(
3130
'Index %s doesn\'t exist',
3231
$configurator->getName()
3332
));
34-
35-
return;
3633
}
3734

3835
try {
@@ -67,4 +64,38 @@ public function handle()
6764
$configurator->getName()
6865
));
6966
}
67+
68+
protected function createWriteAlias()
69+
{
70+
$configurator = $this->getIndexConfigurator();
71+
72+
if (!method_exists($configurator, 'getWriteAlias')) {
73+
return;
74+
}
75+
76+
$indices = ElasticClient::indices();
77+
78+
if ($indices->existsAlias(['name' => $configurator->getWriteAlias()])) {
79+
return;
80+
}
81+
82+
$payload = (new IndexPayload($configurator))
83+
->set('name', $configurator->getWriteAlias())
84+
->get();
85+
86+
$indices->putAlias($payload);
87+
88+
$this->info(sprintf(
89+
'The %s alias for the %s index was created!',
90+
$configurator->getWriteAlias(),
91+
$configurator->getName()
92+
));
93+
}
94+
95+
public function handle()
96+
{
97+
$this->updateIndex();
98+
99+
$this->createWriteAlias();
100+
}
70101
}

src/Console/ElasticUpdateMappingCommand.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ScoutElastic\Console;
44

5+
use LogicException;
56
use Illuminate\Console\Command;
67
use ScoutElastic\Console\Features\requiresModelArgument;
78
use ScoutElastic\Facades\ElasticClient;
@@ -23,9 +24,7 @@ public function handle() {
2324
$mapping = array_merge_recursive($configurator->getDefaultMapping(), $model->getMapping());
2425

2526
if (empty($mapping)) {
26-
$this->error('Nothing to update: the mapping is not specified.');
27-
28-
return;
27+
throw new LogicException('Nothing to update: the mapping is not specified.');
2928
}
3029

3130
$payload = (new TypePayload($model))

src/Console/Features/requiresIndexConfiguratorArgument.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace ScoutElastic\Console\Features;
44

5+
use InvalidArgumentException;
56
use ScoutElastic\IndexConfigurator;
67
use Symfony\Component\Console\Input\InputArgument;
78

89
trait requiresIndexConfiguratorArgument
910
{
1011
/**
11-
* @return IndexConfigurator
12+
* @return IndexConfigurator|null
1213
*/
1314
protected function getIndexConfigurator()
1415
{
@@ -17,13 +18,11 @@ protected function getIndexConfigurator()
1718
$configuratorInstance = new $configuratorClass;
1819

1920
if (!($configuratorInstance instanceof IndexConfigurator)) {
20-
$this->error(sprintf(
21+
throw new InvalidArgumentException(sprintf(
2122
'The class %s must extend %s.',
2223
$configuratorClass,
2324
IndexConfigurator::class
2425
));
25-
26-
return null;
2726
}
2827

2928
return (new $configuratorClass);

src/Console/Features/requiresModelArgument.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,29 @@
22

33
namespace ScoutElastic\Console\Features;
44

5-
use Exception;
5+
use InvalidArgumentException;
66
use Illuminate\Database\Eloquent\Model;
77
use ScoutElastic\Searchable;
8-
use ScoutElastic\SearchableModel;
98
use Symfony\Component\Console\Input\InputArgument;
109

1110
trait requiresModelArgument
1211
{
12+
/**
13+
* @return Model|null
14+
*/
1315
protected function getModel()
1416
{
1517
$modelClass = trim($this->argument('model'));
1618

1719
$modelInstance = new $modelClass;
1820

1921
if (!($modelInstance instanceof Model) || !method_exists($modelInstance, 'getIndexConfigurator')) {
20-
$this->error(sprintf(
22+
throw new InvalidArgumentException(sprintf(
2123
'The %s class must extend %s and use the %s trait.',
2224
$modelClass,
2325
Model::class,
2426
Searchable::class
2527
));
26-
27-
return null;
2828
}
2929

3030
return $modelInstance;

src/Console/stubs/index_configurator.stub

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@
33
namespace DummyNamespace;
44

55
use ScoutElastic\IndexConfigurator;
6+
use ScoutElastic\Migratable;
67

78
class DummyClass extends IndexConfigurator
89
{
9-
//
10+
use Migratable;
11+
12+
protected $settings = [
13+
//
14+
];
15+
16+
protected $defaultMapping = [
17+
//
18+
];
1019
}

src/ElasticEngine.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,16 @@ public function update($models)
3232
);
3333
}
3434

35+
$indexConfigurator = $model->getIndexConfigurator();
36+
3537
$payload = (new DocumentPayload($model))
36-
->setIfNotEmpty('body', $model->toSearchableArray())
37-
->get();
38+
->setIfNotEmpty('body', $model->toSearchableArray());
39+
40+
if (method_exists($indexConfigurator, 'getWriteAlias')) {
41+
$payload->useAlias('write');
42+
}
3843

39-
ElasticClient::index($payload);
44+
ElasticClient::index($payload->get());
4045
});
4146

4247
$this->updateMapping = false;

src/Migratable.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace ScoutElastic;
4+
5+
trait Migratable
6+
{
7+
public function getWriteAlias()
8+
{
9+
return $this->getName().'_write';
10+
}
11+
}

src/Payloads/IndexPayload.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ScoutElastic\Payloads;
44

5+
use Exception;
56
use ScoutElastic\IndexConfigurator;
67

78
class IndexPayload
@@ -12,11 +13,30 @@ class IndexPayload
1213
'index'
1314
];
1415

16+
protected $indexConfigurator;
17+
1518
public function __construct(IndexConfigurator $indexConfigurator)
1619
{
17-
$this->payload = [
18-
'index' => $indexConfigurator->getName()
19-
];
20+
$this->indexConfigurator = $indexConfigurator;
21+
22+
$this->payload['index'] = $indexConfigurator->getName();
23+
}
24+
25+
public function useAlias($alias)
26+
{
27+
$aliasGetter = 'get'.ucfirst($alias).'Alias';
28+
29+
if (!method_exists($this->indexConfigurator, $aliasGetter)) {
30+
throw new Exception(sprintf(
31+
'The index configurator %s doesn\'t have getter for the %s alias.',
32+
get_class($this->indexConfigurator),
33+
$alias
34+
));
35+
}
36+
37+
$this->payload['index'] = call_user_func([$this->indexConfigurator, $aliasGetter]);
38+
39+
return $this;
2040
}
2141

2242
public function set($key, $value)
@@ -41,11 +61,4 @@ public function get($key = null)
4161
{
4262
return array_get($this->payload, $key);
4363
}
44-
45-
public function __call($method, array $args)
46-
{
47-
if (strpos('set', $method) === 0) {
48-
49-
}
50-
}
5164
}

0 commit comments

Comments
 (0)