DatabaseRollbackCommand::doRollback()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 11
cp 0
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 3
crap 20
1
<?php
2
3
namespace Jumilla\Versionia\Laravel\Commands;
4
5
use UnexpectedValueException;
6
use Illuminate\Console\Command;
7
use Illuminate\Console\ConfirmableTrait;
8
use Jumilla\Versionia\Laravel\Migrator;
9
10
class DatabaseRollbackCommand extends Command
11
{
12
    use DatabaseCommandTrait;
13
    use ConfirmableTrait;
14
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'database:rollback
21
        {group : The name of the migration group.}
22
        {--all : Includes all version.}
23
        {--force : Force the operation to run when in production.}
24
    ';
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'Database migrate to back to one or specifed version';
32
33
    /**
34
     * Execute the console command.
35
     *
36
     * @param \Jumilla\Versionia\Laravel\Migrator $migrator
37
     *
38
     * @return mixed
39
     */
40 2
    public function handle(Migrator $migrator)
41
    {
42 2
        if (!$this->confirmToProceed()) {
43
            return;
44
        }
45
46 2
        $group = $this->argument('group');
47
48
        // check valid group
49 2
        if (!in_array($group, $migrator->migrationGroups())) {
50 2
            throw new UnexpectedValueException("Migation group '$group' is not defined.");
51
        }
52
53
        $version = $this->option('all') ? Migrator::VERSION_NULL : $migrator->migrationLatestVersion($group);
54
55
        $migrator->makeLogTable();
56
57
        $this->doRollback($migrator, $group, $version);
58
    }
59
60
    /**
61
     * Execute rollback.
62
     *
63
     * @param \Jumilla\Versionia\Laravel\Migrator $migrator
64
     * @param string                              $target_group
65
     * @param string                              $target_version
66
     */
67
    protected function doRollback(Migrator $migrator, $target_group, $target_version)
68
    {
69
        $installed_migrations = $migrator->installedMigrationsByDesc();
70
71
        if (!isset($installed_migrations[$target_group])) {
72
            $this->info("Nothing migrations for group '$target_group'.");
73
74
            return;
75
        }
76
77
        foreach ($installed_migrations[$target_group] as $data) {
78
            // check version
79
            if ($migrator->compareMigrationVersion($data->version, $target_version) < 0) {
80
                continue;
81
            }
82
83
            $this->infoDowngrade($target_group, $data->version, $data->class);
84
85
            $migrator->doDowngrade($target_group, $data->version);
86
        }
87
    }
88
}
89