AddAgentNameHash::migrateDown()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
use PragmaRX\Tracker\Support\Migration;
4
use PragmaRX\Tracker\Vendor\Laravel\Models\Agent;
5
6
class AddAgentNameHash extends Migration
7
{
8
    /**
9
     * Table related to this migration.
10
     *
11
     * @var string
12
     */
13
    private $table = 'tracker_agents';
14
15
    /**
16
     * Run the migrations.
17
     *
18
     * @return void
19
     */
20
    public function migrateUp()
21
    {
22
        try {
23
            $this->builder->table(
24
                $this->table,
25
                function ($table) {
26
                    $table->dropUnique('tracker_agents_name_unique');
27
28
                    $table->string('name_hash', 65)->nullable();
29
                }
30
            );
31
32
            Agent::all()->each(function ($agent) {
33
                $agent->name_hash = hash('sha256', $agent->name);
34
35
                $agent->save();
36
            });
37
38
            $this->builder->table(
39
                $this->table,
40
                function ($table) {
41
                    $table->unique('name_hash');
42
                }
43
            );
44
        } catch (\Exception $e) {
45
            dd($e->getMessage());
46
        }
47
    }
48
49
    /**
50
     * Reverse the migrations.
51
     *
52
     * @return void
53
     */
54
    public function migrateDown()
55
    {
56
        try {
57
            $this->builder->table(
58
                $this->table,
59
                function ($table) {
60
                    $table->dropUnique('tracker_agents_name_hash_unique');
61
62
                    $table->dropColumn('name_hash');
63
64
                    $table->mediumText('name')->unique()->change();
65
                }
66
            );
67
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
68
        }
69
    }
70
}
71