CreatePermissionTables   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 62
c 1
b 0
f 0
dl 0
loc 104
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A up() 0 77 3
A down() 0 13 2
1
<?php
2
3
use Illuminate\Database\Migrations\Migration;
4
use Illuminate\Database\Schema\Blueprint;
5
use Illuminate\Support\Facades\Schema;
6
7
class CreatePermissionTables extends Migration
8
{
9
    /**
10
     * Run the migrations.
11
     *
12
     * @return void
13
     */
14
    public function up()
15
    {
16
        $tableNames  = config('permission.table_names');
17
        $columnNames = config('permission.column_names');
18
19
        if (empty($tableNames)) {
20
            throw new \Exception('Error: config/permission.php not found and defaults could not be merged. Please publish the package configuration before proceeding.');
21
        }
22
23
        Schema::create($tableNames['permissions'], function (Blueprint $table) {
24
            $table->bigIncrements('id');
25
            $table->uuid('uuid')->default('-');
26
            $table->string('name');
27
            $table->string('guard_name');
28
            $table->timestamps();
29
        });
30
31
        Schema::create($tableNames['roles'], function (Blueprint $table) {
32
            $table->bigIncrements('id');
33
            $table->uuid('uuid')->default('-');
34
            $table->string('name');
35
            $table->string('guard_name');
36
            $table->timestamps();
37
        });
38
39
        Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) {
40
            $table->unsignedBigInteger('permission_id');
41
42
            $table->string('model_type');
43
            $table->unsignedBigInteger($columnNames['model_morph_key']);
44
            $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index');
45
46
            $table->foreign('permission_id')
47
                ->references('id')
48
                ->on($tableNames['permissions'])
49
                ->onDelete('cascade');
50
51
            $table->primary(['permission_id', $columnNames['model_morph_key'], 'model_type'],
52
                    'model_has_permissions_permission_model_type_primary');
53
        });
54
55
        Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) {
56
            $table->unsignedBigInteger('role_id');
57
58
            $table->string('model_type');
59
            $table->unsignedBigInteger($columnNames['model_morph_key']);
60
            $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_roles_model_id_model_type_index');
61
62
            $table->foreign('role_id')
63
                ->references('id')
64
                ->on($tableNames['roles'])
65
                ->onDelete('cascade');
66
67
            $table->primary(['role_id', $columnNames['model_morph_key'], 'model_type'],
68
                    'model_has_roles_role_model_type_primary');
69
        });
70
71
        Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
72
            $table->unsignedBigInteger('permission_id');
73
            $table->unsignedBigInteger('role_id');
74
75
            $table->foreign('permission_id')
76
                ->references('id')
77
                ->on($tableNames['permissions'])
78
                ->onDelete('cascade');
79
80
            $table->foreign('role_id')
81
                ->references('id')
82
                ->on($tableNames['roles'])
83
                ->onDelete('cascade');
84
85
            $table->primary(['permission_id', 'role_id'], 'role_has_permissions_permission_id_role_id_primary');
86
        });
87
88
        app('cache')
89
            ->store('default' != config('permission.cache.store') ? config('permission.cache.store') : null)
90
            ->forget(config('permission.cache.key'));
91
    }
92
93
    /**
94
     * Reverse the migrations.
95
     *
96
     * @return void
97
     */
98
    public function down()
99
    {
100
        $tableNames = config('permission.table_names');
101
102
        if (empty($tableNames)) {
103
            throw new \Exception('Error: config/permission.php not found and defaults could not be merged. Please publish the package configuration before proceeding, or drop the tables manually.');
104
        }
105
106
        Schema::drop($tableNames['role_has_permissions']);
107
        Schema::drop($tableNames['model_has_roles']);
108
        Schema::drop($tableNames['model_has_permissions']);
109
        Schema::drop($tableNames['roles']);
110
        Schema::drop($tableNames['permissions']);
111
    }
112
}
113