Completed
Pull Request — master (#91)
by Anton
02:46
created

UpgradeV7ToV8::getDbQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of Laravel Love.
5
 *
6
 * (c) Anton Komarev <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Cog\Laravel\Love\Console\Commands;
15
16
use Illuminate\Console\Command;
17
use Illuminate\Database\ConnectionInterface;
18
use Illuminate\Database\Schema\Blueprint;
19
use Illuminate\Database\Schema\Builder;
20
use Illuminate\Support\Facades\Config;
21
use Illuminate\Support\Facades\DB;
22
use Illuminate\Support\Facades\Schema;
23
24
final class UpgradeV7ToV8 extends Command
25
{
26
    /**
27
     * The name and signature of the console command.
28
     *
29
     * @var string
30
     */
31
    protected $signature = 'love:upgrade-v7-to-v8';
32
33
    /**
34
     * The console command description.
35
     *
36
     * @var string
37
     */
38
    protected $description = 'Upgrade love package from v7 to v8';
39
40
    /**
41
     * Execute the console command.
42
     *
43
     * @return void
44
     */
45
    public function handle(): void
46
    {
47
        $this->dbChangeReactionType();
48
        $this->dbChangeReaction();
49
        // TODO: Remove `reactant_reaction_counters.count` default value
50
        // TODO: Remove `reactant_reaction_counters.weight` default value
51
        // TODO: Remove `reactant_reaction_totals.count` default value
52
        // TODO: Remove `reactant_reaction_totals.weight` default value
53
    }
54
55
    private function dbChangeReactionType(): void
56
    {
57
        $this->getDbSchema()->table('love_reaction_types', function (Blueprint $table) {
58
            $table->renameColumn('weight', 'mass');
59
        });
60
    }
61
62
    private function dbChangeReaction(): void
63
    {
64
        $this->getDbSchema()->table('love_reactions', function (Blueprint $table) {
65
            $table->decimal('rate', 4, 2)->after('reaction_type_id');
66
        });
67
68
        $this
69
            ->getDbQuery()
70
            ->table('love_reactions')
71
            ->where('rate', 0.0)
72
            ->update([
73
                'rate' => 1.0,
74
            ]);
75
    }
76
77
    private function getDbSchema(): Builder
78
    {
79
        return Schema::connection(Config::get('love.storage.database.connection'));
80
    }
81
82
    private function getDbQuery(): ConnectionInterface
83
    {
84
        return DB::connection(Config::get('love.storage.database.connection'));
85
    }
86
}
87