Completed
Push — develop ( 1d24d0...76e78e )
by Sam
12s
created

ApplyMigrationCommand::handle()   B

Complexity

Conditions 3
Paths 6

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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