Issues (28)

src/Console/Commands/UpgradeV7ToV8.php (2 issues)

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
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
use Symfony\Component\Console\Attribute\AsCommand;
25
26
#[AsCommand(name: 'love:upgrade-v7-to-v8', description: 'Upgrade Love package from v7 to v8')]
27
final class UpgradeV7ToV8 extends Command
28
{
29
    /**
30
     * Execute the console command.
31
     */
32
    public function handle(): int
33
    {
34
        $this->assertRequirements();
35
        $this->warn('Started Laravel Love v7 to v8 upgrade process.');
36
        $this->dbChangeReactionTypes();
37
        $this->dbChangeReactions();
38
        $this->dbChangeReactantReactionCounters();
39
        $this->dbChangeReactantReactionTotals();
40
        $this->info('Completed Laravel Love v7 to v8 upgrade process.');
41
42
        return self::SUCCESS;
43
    }
44
45
    private function assertRequirements(): void
46
    {
47
        if (interface_exists(DoctrineDbalDriver::class)) {
48
            return;
49
        }
50
51
        $this->error('Doctrine DBAL is missing!');
52
        $this->info('<comment>Install it with Composer:</comment> composer require doctrine/dbal');
53
        exit;
0 ignored issues
show
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...
54
    }
55
56
    private function dbChangeReactionTypes(): void
57
    {
58
        $this->warn('DB: Renaming reaction types weight column.');
59
        $this->getDbSchema()->table('love_reaction_types', function (Blueprint $table) {
60
            $table->renameColumn('weight', 'mass');
61
        });
62
        $this->info('DB: Renamed reaction types weight column.');
63
    }
64
65
    private function dbChangeReactions(): void
66
    {
67
        $this->warn('DB: Adding rate column to reactions.');
68
        $this->getDbSchema()->table('love_reactions', function (Blueprint $table) {
69
            $table->decimal('rate', 4, 2)->after('reaction_type_id');
70
        });
71
        $this->info('DB: Added rate column to reactions.');
72
73
        $this->warn('DB: Updating reaction rate column values for existing records.');
74
        $this
75
            ->getDbQuery()
76
            ->table('love_reactions')
77
            ->where('rate', 0.0)
78
            ->update([
79
                'rate' => 1.0,
80
            ]);
81
        $this->info('DB: Updated reaction rate column values for existing records.');
82
    }
83
84
    private function dbChangeReactantReactionCounters(): void
85
    {
86
        $this->warn('DB: Changing default reaction counters values.');
87
        $this->getDbSchema()->table('love_reactant_reaction_counters', function (Blueprint $table) {
88
            $table->unsignedBigInteger('count')->default(null)->change();
89
            $table->decimal('weight', 13, 2)->default(null)->change();
90
        });
91
        $this->info('DB: Changed default reaction counters values.');
92
    }
93
94
    private function dbChangeReactantReactionTotals(): void
95
    {
96
        $this->warn('DB: Changing default reaction totals values.');
97
        $this->getDbSchema()->table('love_reactant_reaction_totals', function (Blueprint $table) {
98
            $table->unsignedBigInteger('count')->default(null)->change();
99
            $table->decimal('weight', 13, 2)->default(null)->change();
100
        });
101
        $this->info('DB: Changed default reaction counters values.');
102
    }
103
104
    private function getDbSchema(): Builder
105
    {
106
        return Schema::connection($this->getDatabaseConnection());
107
    }
108
109
    private function getDbQuery(): ConnectionInterface
110
    {
111
        return DB::connection($this->getDatabaseConnection());
112
    }
113
114
    private function getDatabaseConnection(): string | null
115
    {
116
        return Config::get('love.storage.database.connection');
117
    }
118
}
119