DatabaseAgainCommand   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 11.36%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 130
ccs 5
cts 44
cp 0.1136
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 23 4
B doAgain() 0 33 4
B checkInstalledVersion() 0 27 4
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 DatabaseAgainCommand 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:again
21
        {group : Group name}
22
        {--seed= : Indicates if the seed task should be re-run.}
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 = 'Migrations rollback latest version and upgrade';
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
        $migrator->makeLogTable();
54
55
        $this->doAgain($migrator, $group);
56
57
        $seed = $this->option('seed');
58
59
        if ($seed) {
60
            $this->call('database:seed', ['name' => $seed, '--force' => true]);
61
        }
62
    }
63
64
    /**
65
     * Execute rollback and upgrade one version.
66
     *
67
     * @param \Jumilla\Versionia\Laravel\Migrator $migrator
68
     * @param string                              $group
69
     */
70
    protected function doAgain(Migrator $migrator, $group)
71
    {
72
        // retreive installed versions
73
        $installed_migrations = $migrator->installedLatestMigrations();
74
75
        $installed_version = data_get($installed_migrations, $group.'.version', Migrator::VERSION_NULL);
76
77
        $definition_versions = $migrator->migrationVersionsByDesc($group);
78
79
        if (!$this->checkInstalledVersion($installed_version, $definition_versions)) {
80
            return;
81
        }
82
83
        // remove migration log
84
        $definition_latest_version = array_get(array_keys($definition_versions), 0, Migrator::VERSION_NULL);
85
        if ($migrator->compareMigrationVersion($installed_version, $definition_latest_version) >= 0) {
86
            $this->line("<info>Remove log [$group/$installed_version]</info>");
87
            $migrator->removeMigrationLog($group, $installed_version);
88
        }
89
90
        // downgrade & upgrade
91
        foreach ($definition_versions as $version => $class) {
92
            $this->infoDowngrade($group, $version, $class);
93
94
            $migrator->doDowngrade($group, $version, $class);
95
96
            $this->infoUpgrade($group, $version, $class);
97
98
            $migrator->doUpgrade($group, $version, $class);
99
100
            break;
101
        }
102
    }
103
104
    /**
105
     * Check installed version.
106
     *
107
     * @param string $installed_version
108
     * @param array  $definition_versions
109
     *
110
     * @return bool
111
     */
112
    protected function checkInstalledVersion($installed_version, array $definition_versions)
113
    {
114
        if ($installed_version === null) {
115
            $this->error('Nothing installed version.');
116
            $this->line('Please run <comment>database:upgrade<comment>.');
117
118
            return false;
119
        }
120
121
        $index = array_search($installed_version, array_keys($definition_versions));
122
123
        if ($index === false) {
124
            $versions = json_encode(array_keys($definition_versions));
125
            $this->error("Installed version '{$installed_version}' was not found in definition {$versions}.");
126
127
            return false;
128
        }
129
130
        if ($index >= 2) {
131
            $this->error("Installed version '$installed_version' was older.");
132
            $this->line('Please run <comment>database:upgrade<comment>.');
133
134
            return false;
135
        }
136
137
        return true;
138
    }
139
}
140