Passed
Push — master ( 4e7933...848497 )
by Anton
02:48
created

UpgradeV7ToV8::dbChangeReactionTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
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 Doctrine\DBAL\Driver as DoctrineDbalDriver;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Driver was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Illuminate\Console\Command;
18
use Illuminate\Database\ConnectionInterface;
19
use Illuminate\Database\Schema\Blueprint;
20
use Illuminate\Database\Schema\Builder;
21
use Illuminate\Support\Facades\Config;
22
use Illuminate\Support\Facades\DB;
23
use Illuminate\Support\Facades\Schema;
24
25
final class UpgradeV7ToV8 extends Command
26
{
27
    /**
28
     * The name and signature of the console command.
29
     *
30
     * @var string
31
     */
32
    protected $signature = 'love:upgrade-v7-to-v8';
33
34
    /**
35
     * The console command description.
36
     *
37
     * @var string
38
     */
39
    protected $description = 'Upgrade Love package from v7 to v8';
40
41
    /**
42
     * Execute the console command.
43
     *
44
     * @return void
45
     */
46
    public function handle(): void
47
    {
48
        $this->assertRequirements();
49
        $this->warn('Started Laravel Love v7 to v8 upgrade process.');
50
        $this->dbChangeReactionTypes();
51
        $this->dbChangeReactions();
52
        $this->dbChangeReactantReactionCounters();
53
        $this->dbChangeReactantReactionTotals();
54
        $this->info('Completed Laravel Love v7 to v8 upgrade process.');
55
    }
56
57
    private function assertRequirements(): void
58
    {
59
        if (interface_exists(DoctrineDbalDriver::class)) {
60
            return;
61
        }
62
63
        $this->error('Doctrine DBAL is missing!');
64
        $this->info('<comment>Install it with Composer:</comment> composer require doctrine/dbal');
65
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
66
    }
67
68
    private function dbChangeReactionTypes(): void
69
    {
70
        $this->warn('DB: Renaming reaction types weight column.');
71
        $this->getDbSchema()->table('love_reaction_types', function (Blueprint $table) {
72
            $table->renameColumn('weight', 'mass');
73
        });
74
        $this->info('DB: Renamed reaction types weight column.');
75
    }
76
77
    private function dbChangeReactions(): void
78
    {
79
        $this->warn('DB: Adding rate column to reactions.');
80
        $this->getDbSchema()->table('love_reactions', function (Blueprint $table) {
81
            $table->decimal('rate', 4, 2)->after('reaction_type_id');
82
        });
83
        $this->info('DB: Added rate column to reactions.');
84
85
        $this->warn('DB: Updating reaction rate column values for existing records.');
86
        $this
87
            ->getDbQuery()
88
            ->table('love_reactions')
89
            ->where('rate', 0.0)
90
            ->update([
91
                'rate' => 1.0,
92
            ]);
93
        $this->info('DB: Updated reaction rate column values for existing records.');
94
    }
95
96
    private function dbChangeReactantReactionCounters(): void
97
    {
98
        $this->warn('DB: Changing default reaction counters values.');
99
        $this->getDbSchema()->table('love_reactant_reaction_counters', function (Blueprint $table) {
100
            $table->unsignedBigInteger('count')->default(null)->change();
101
            $table->decimal('weight', 13, 2)->default(null)->change();
102
        });
103
        $this->info('DB: Changed default reaction counters values.');
104
    }
105
106
    private function dbChangeReactantReactionTotals(): void
107
    {
108
        $this->warn('DB: Changing default reaction totals values.');
109
        $this->getDbSchema()->table('love_reactant_reaction_totals', function (Blueprint $table) {
110
            $table->unsignedBigInteger('count')->default(null)->change();
111
            $table->decimal('weight', 13, 2)->default(null)->change();
112
        });
113
        $this->info('DB: Changed default reaction counters values.');
114
    }
115
116
    private function getDbSchema(): Builder
117
    {
118
        return Schema::connection($this->getDatabaseConnection());
119
    }
120
121
    private function getDbQuery(): ConnectionInterface
122
    {
123
        return DB::connection($this->getDatabaseConnection());
124
    }
125
126
    private function getDatabaseConnection(): ?string
127
    {
128
        return Config::get('love.storage.database.connection');
129
    }
130
}
131