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

DatabaseRefreshCommand::doRefresh()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 23
Code Lines 10

Duplication

Lines 7
Ratio 30.43 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
dl 7
loc 23
ccs 11
cts 11
cp 1
rs 8.5906
c 0
b 0
f 0
cc 5
eloc 10
nc 9
nop 1
crap 5
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 DatabaseRefreshCommand 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:refresh
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 migrate to clean, and migrate 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->doRefresh($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
     * Execute clean and upgrade.
57
     *
58
     * @param \Jumilla\Versionia\Laravel\Migrator $migrator
59
     */
60 4
    protected function doRefresh(Migrator $migrator)
61
    {
62
        // retreive installed versions
63 4
        $installed_migrations = $migrator->installedMigrationsByDesc();
64
65
        // downgrade
66 4 View Code Duplication
        foreach ($installed_migrations as $group => $migrations) {
67 1
            foreach ($migrations as $data) {
68 1
                $this->infoDowngrade($group, $data->version, $data->class);
69
70 1
                $migrator->doDowngrade($group, $data->version);
71
            }
72
        }
73
74
        // upgrade
75 4
        foreach ($migrator->migrationGroups() as $group) {
76 1
            foreach ($migrator->migrationVersions($group) as $version => $class) {
77 1
                $this->infoUpgrade($group, $version, $class);
78
79 1
                $migrator->doUpgrade($group, $version, $class);
80
            }
81
        }
82 4
    }
83
}
84