DatabaseUpgradeCommand::migrateToLatest()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 13
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 12
nc 8
nop 1
crap 30
1
<?php
2
3
namespace Jumilla\Versionia\Laravel\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Console\ConfirmableTrait;
7
use Jumilla\Versionia\Laravel\Migrator;
8
9
class DatabaseUpgradeCommand extends Command
10
{
11
    use DatabaseCommandTrait;
12
    use ConfirmableTrait;
13
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'database:upgrade
20
        {--seed= : Indicates if the seed task should be re-run.}
21
        {--force : Force the operation to run when in production.}
22
    ';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Database migration to latest version';
30
31
    /**
32
     * Execute the console command.
33
     *
34
     * @param \Jumilla\Versionia\Laravel\Migrator $migrator
35
     *
36
     * @return mixed
37
     */
38 View Code Duplication
    public function handle(Migrator $migrator)
39
    {
40
        if (!$this->confirmToProceed()) {
41
            return;
42
        }
43
44
        $migrator->makeLogTable();
45
46
        $this->migrateToLatest($migrator);
47
48
        $seed = $this->option('seed');
49
50
        if ($seed) {
51
            $this->call('database:seed', ['name' => $seed, '--force' => true]);
52
        }
53
    }
54
55
    /**
56
     * Migrate dataase to latest version.
57
     *
58
     * @param \Jumilla\Versionia\Laravel\Migrator $migrator
59
     */
60
    protected function migrateToLatest(Migrator $migrator)
61
    {
62
        $installed_migrations = $migrator->installedLatestMigrations();
63
64
        $migration_count = 0;
65
66
        foreach ($migrator->migrationGroups() as $group) {
67
            // [$group => ['version'=>$version, 'class'=>$class]] to $version
68
            $latest_installed_version = data_get($installed_migrations, $group.'.version', Migrator::VERSION_NULL);
69
70
            foreach ($migrator->migrationVersions($group) as $version => $class) {
71
                if ($migrator->compareMigrationVersion($version, $latest_installed_version) > 0) {
72
                    $this->line("<info>Up [$group/$version]</info> Run class <comment>$class</comment>");
73
74
                    $migrator->doUpgrade($group, $version, $class);
75
76
                    ++$migration_count;
77
                }
78
            }
79
        }
80
81
        if ($migration_count == 0) {
82
            $this->line('<info>Nothing to migrate.</info>');
83
        }
84
    }
85
}
86