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

optimizeIndexes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 6
rs 10
c 1
b 0
f 0
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