Passed
Push — master ( 3d397a...3f0190 )
by Andrey
12:15
created

ModifyRolesAndPermissionsTableOptimizeIndexes   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 29
rs 10
c 1
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A up() 0 4 1
A revertIndexes() 0 6 1
A down() 0 4 1
A optimizeIndexes() 0 6 1
1
<?php
2
3
use Illuminate\Database\Schema\Blueprint;
4
5
class ModifyRolesAndPermissionsTableOptimizeIndexes extends BaseMigration
6
{
7
    public function up()
8
    {
9
        $this->optimizeIndexes($this->user_roles, 'user_id', 'role_id');
10
        $this->optimizeIndexes($this->role_permissions, 'role_id', 'permission_id');
11
    }
12
13
    public function down()
14
    {
15
        $this->revertIndexes($this->user_roles, 'user_id', 'role_id');
16
        $this->revertIndexes($this->role_permissions, 'role_id', 'permission_id');
17
    }
18
19
    protected function optimizeIndexes(string $table, string $first_key, string $second_key)
20
    {
21
        $this->table($table, function (Blueprint $table) use ($first_key, $second_key) {
22
            $table->dropPrimary([$first_key, $second_key]);
23
24
            $table->index($first_key);
25
        });
26
    }
27
28
    protected function revertIndexes(string $table, string $first_key, string $second_key)
29
    {
30
        $this->table($table, function (Blueprint $table) use ($first_key, $second_key) {
31
            $table->dropIndex($first_key);
32
33
            $table->primary([$first_key, $second_key]);
34
        });
35
    }
36
}
37