Failed Conditions
Push — master ( 130b7c...9f9094 )
by Dallas
19:30 queued 14:09
created

RollbackMissingMigrations::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Umbrellio\RollbackMissingMigrations\Console;
6
7
use Illuminate\Database\Console\Migrations\BaseCommand;
8
use Illuminate\Support\Facades\App;
9
use Symfony\Component\Process\Process;
10
use Umbrellio\RollbackMissingMigrations\Helpers\DbHelper;
11
12
class RollbackMissingMigrations extends BaseCommand
13
{
14
    protected $signature = 'rollback_missing_migrations:rollback {path_to_artisan}
15
        {--path=* : Path(s) where migrations located in current release}
16
        {--old-path=* : Path(s) where migrations located in previous release}
17
        {--realpath : Indicate current release migrations files paths are pre-resolved absolute paths}';
18
19
    protected $description = 'Rollback missing migrations';
20
21
    protected $migrator;
22
    protected $dbHelper;
23
24 3
    public function __construct(DbHelper $dbHelper)
25
    {
26 3
        $this->migrator = App::make('migrator');
27 3
        $this->dbHelper = $dbHelper;
28
29 3
        parent::__construct();
30
    }
31
32 3
    public function handle()
33
    {
34 3
        $migrationsForRollback = $this->getMigrationsNamesForRollback();
35
36 3
        if (empty($migrationsForRollback)) {
37 1
            $this->info('Nothing to rollback');
38 1
            return;
39
        }
40
41 2
        $backup = $this->dbHelper->backupBatchNumbers($migrationsForRollback);
42 2
        $nextBatchNumber = $this->migrator
43 2
            ->getRepository()
44 2
            ->getNextBatchNumber();
45 2
        $this->dbHelper->updateBatch($migrationsForRollback, $nextBatchNumber);
46 2
        $this->rollback($this->argument('path_to_artisan'));
47 2
        $this->dbHelper->restoreBatchNumbers($backup);
48 2
        $this->dbHelper->checkIfRollbackIsSuccessful($migrationsForRollback);
49
    }
50
51 3
    protected function getMigrationsNamesForRollback(): array
52
    {
53 3
        $migrationsFromFiles = array_keys($this->migrator->getMigrationFiles($this->getMigrationPaths()));
54 3
        $migrationsFromDb = $this->migrator
55 3
            ->getRepository()
56 3
            ->getRan();
57 3
        return array_diff($migrationsFromDb, $migrationsFromFiles);
58
    }
59
60 2
    protected function rollback(string $artisanPath): void
61
    {
62 2
        $command = sprintf(
63 2
            'php %s migrate:rollback %s %s',
64 2
            $artisanPath,
65 2
            $this->getOldPaths(),
66 2
            $this->getRealpath()
67 2
        );
68 2
        $process = Process::fromShellCommandline($command);
69 2
        $process->run();
70
71 2
        $this->line($process->getOutput());
72
    }
73
74 2
    protected function getOldPaths(): string
75
    {
76 2
        $targetPaths = [];
77 2
        if ($this->input->hasOption('old-path') && $this->option('old-path')) {
78 2
            $targetPaths = (array) $this->option('old-path');
79
        }
80
81 2
        return implode(' ', array_map(function (string $path) {
82 2
            return "--path={$path}";
83 2
        }, $targetPaths));
84
    }
85
86 2
    protected function getRealpath(): string
87
    {
88 2
        return $this->usingRealPath() ? '--realpath' : '';
89
    }
90
}
91