Completed
Push — master ( aed5e6...e7b04e )
by Fumio
01:47
created

DatabaseUpgradeCommand::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 16
loc 16
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 3
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 5 View Code Duplication
    public function handle(Migrator $migrator)
39
    {
40 5
        if (!$this->confirmToProceed()) {
41 1
            return;
42
        }
43
44 4
        $migrator->makeLogTable();
45
46 4
        $this->migrateToLatest($migrator);
47
48 4
        $seed = $this->option('seed');
49
50 4
        if ($seed) {
51 1
            $this->call('database:seed', ['name' => $seed, '--force' => true]);
52
        }
53 4
    }
54
55
    /**
56
     * Migrate dataase to latest version.
57
     *
58
     * @param \Jumilla\Versionia\Laravel\Migrator $migrator
59
     */
60 4
    protected function migrateToLatest(Migrator $migrator)
61
    {
62 4
        $installed_migrations = $migrator->installedLatestMigrations();
63
64 4
        $migration_count = 0;
65
66 4
        foreach ($migrator->migrationGroups() as $group) {
67
            // [$group => ['version'=>$version, 'class'=>$class]] to $version
68 2
            $latest_installed_version = data_get($installed_migrations, $group.'.version', Migrator::VERSION_NULL);
69
70 2
            foreach ($migrator->migrationVersions($group) as $version => $class) {
71 2
                if ($migrator->compareMigrationVersion($version, $latest_installed_version) > 0) {
72 2
                    $this->line("<info>Up [$group/$version]</info> Run class <comment>$class</comment>");
73
74 2
                    $migrator->doUpgrade($group, $version, $class);
75
76 2
                    ++$migration_count;
77
                }
78
            }
79
        }
80
81 4
        if ($migration_count == 0) {
82 2
            $this->line('<info>Nothing to migrate.</info>');
83
        }
84 4
    }
85
}
86