Passed
Push — master ( 0a2dad...a9b714 )
by Anton
04:22 queued 01:15
created

CreateLoveReactionsTable::up()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 43
rs 9.36
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
use Cog\Laravel\Love\Reactant\Models\Reactant;
15
use Cog\Laravel\Love\Reacter\Models\Reacter;
16
use Cog\Laravel\Love\Reaction\Models\Reaction;
17
use Cog\Laravel\Love\ReactionType\Models\ReactionType;
18
use Cog\Laravel\Love\Support\Database\Migration;
19
use Illuminate\Database\Schema\Blueprint;
20
21
return new class extends Migration
22
{
23
    /**
24
     * Run the migrations.
25
     */
26
    public function up(): void
27
    {
28
        $this->schema->create((new Reaction())->getTable(), function (Blueprint $table) {
29
            $table->bigIncrements('id');
30
            $table->unsignedBigInteger('reactant_id');
31
            $table->unsignedBigInteger('reacter_id');
32
            $table->unsignedBigInteger('reaction_type_id');
33
            $table->decimal('rate', 4, 2);
34
            $table->timestamps();
35
36
            $table->index([
37
                'reactant_id',
38
                'reaction_type_id',
39
            ]);
40
            $table->index([
41
                'reactant_id',
42
                'reacter_id',
43
                'reaction_type_id',
44
            ]);
45
            $table->index([
46
                'reactant_id',
47
                'reacter_id',
48
            ]);
49
            $table->index([
50
                'reacter_id',
51
                'reaction_type_id',
52
            ]);
53
54
            $table
55
                ->foreign('reactant_id')
56
                ->references('id')
57
                ->on((new Reactant())->getTable())
58
                ->onDelete('cascade');
59
            $table
60
                ->foreign('reacter_id')
61
                ->references('id')
62
                ->on((new Reacter())->getTable())
63
                ->onDelete('cascade');
64
            $table
65
                ->foreign('reaction_type_id')
66
                ->references('id')
67
                ->on((new ReactionType())->getTable())
68
                ->onDelete('cascade');
69
        });
70
    }
71
72
    /**
73
     * Reverse the migrations.
74
     */
75
    public function down(): void
76
    {
77
        $this->schema->dropIfExists((new Reaction())->getTable());
78
    }
79
};
80