ApplyMigrationCommand::handle()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 24
rs 9.7
cc 2
nc 3
nop 0
1
<?php
2
3
namespace Nord\Lumen\Elasticsearch\Console;
4
5
use League\Pipeline\Pipeline;
6
use Nord\Lumen\Elasticsearch\Exceptions\IndexExistsException;
7
use Nord\Lumen\Elasticsearch\IndexNamePrefixer;
8
use Nord\Lumen\Elasticsearch\Pipelines\Payloads\ApplyMigrationPayload;
9
use Nord\Lumen\Elasticsearch\Pipelines\Stages\CheckIndexExistsStage;
10
use Nord\Lumen\Elasticsearch\Pipelines\Stages\CreateIndexStage;
11
use Nord\Lumen\Elasticsearch\Pipelines\Stages\DetermineTargetVersionStage;
12
use Nord\Lumen\Elasticsearch\Pipelines\Stages\ReIndexStage;
13
use Nord\Lumen\Elasticsearch\Pipelines\Stages\StoreIndexSettingsStage;
14
use Nord\Lumen\Elasticsearch\Pipelines\Stages\UpdateIndexAliasStage;
15
16
/**
17
 * Class ApplyMigrationCommand
18
 * @package Nord\Lumen\Elasticsearch\Commands\Migrations
19
 */
20
class ApplyMigrationCommand extends AbstractCommand
21
{
22
    public const DEFAULT_BATCH_SIZE = 1000;
23
24
    /**
25
     * @var string
26
     */
27
    protected $signature = 'elastic:migrations:migrate 
28
                            { config : The path to the index configuration file } 
29
                            { --batchSize=' . self::DEFAULT_BATCH_SIZE . ' : The number of documents to handle per batch while re-indexing }
30
                            { --updateAllTypes : Forces update across all types }';
31
32
    /**
33
     * The console command description.
34
     *
35
     * @var string
36
     */
37
    protected $description = 'Migrates the specified index to a new index using the newest configuration version';
38
39
    /**
40
     * @inheritDoc
41
     */
42
    public function handle()
43
    {
44
        $configurationPath = (string)$this->argument('config');
45
        $batchSize         = (int)$this->option('batchSize');
46
        $updateAllTypes    = (bool)$this->option('updateAllTypes');
47
48
        $pipeline = new Pipeline([
49
            new DetermineTargetVersionStage(),
50
            new CheckIndexExistsStage($this->elasticsearchService),
51
            new CreateIndexStage($this->elasticsearchService),
52
            new StoreIndexSettingsStage($this->elasticsearchService),
53
            new ReIndexStage($this->elasticsearchService),
54
            new UpdateIndexAliasStage($this->elasticsearchService),
55
        ]);
56
57
        $payload = new ApplyMigrationPayload($configurationPath, $batchSize, $updateAllTypes);
58
59
        try {
60
            $pipeline->process($payload);
61
62
            $this->output->writeln(sprintf('Migrated %s to %s', $payload->getPrefixedIndexName(),
63
                $payload->getPrefixedTargetVersionName()));
64
        } catch (IndexExistsException $e) {
65
            $this->output->writeln('No migration required');
66
        }
67
    }
68
}
69