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

UpgradeV7ToV8::dbChangeReactantReactionCounters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
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->dbChangeReactionTypes();
48
        $this->dbChangeReactions();
49
        $this->dbChangeReactantReactionCounters();
50
        $this->dbChangeReactantReactionTotals();
51
    }
52
53
    private function dbChangeReactionTypes(): void
54
    {
55
        $this->getDbSchema()->table('love_reaction_types', function (Blueprint $table) {
56
            $table->renameColumn('weight', 'mass');
57
        });
58
    }
59
60
    private function dbChangeReactions(): void
61
    {
62
        $this->getDbSchema()->table('love_reactions', function (Blueprint $table) {
63
            $table->decimal('rate', 4, 2)->after('reaction_type_id');
64
        });
65
66
        $this
67
            ->getDbQuery()
68
            ->table('love_reactions')
69
            ->where('rate', 0.0)
70
            ->update([
71
                'rate' => 1.0,
72
            ]);
73
    }
74
75
    private function dbChangeReactantReactionCounters(): void
76
    {
77
        $this->getDbSchema()->table('love_reactant_reaction_counters', function (Blueprint $table) {
78
            $table->unsignedBigInteger('count')->default(null)->change();
79
            $table->decimal('weight', 22, 2)->default(null)->change();
80
        });
81
    }
82
83
    private function dbChangeReactantReactionTotals(): void
84
    {
85
        $this->getDbSchema()->table('love_reactant_reaction_totals', function (Blueprint $table) {
86
            $table->unsignedBigInteger('count')->default(null)->change();
87
            $table->decimal('weight', 22, 2)->default(null)->change();
88
        });
89
    }
90
91
    private function getDbSchema(): Builder
92
    {
93
        return Schema::connection($this->getDatabaseConnection());
94
    }
95
96
    private function getDbQuery(): ConnectionInterface
97
    {
98
        return DB::connection($this->getDatabaseConnection());
99
    }
100
101
    private function getDatabaseConnection(): ?string
102
    {
103
        return Config::get('love.storage.database.connection');
104
    }
105
}
106