summaryrefslogtreecommitdiff
path: root/db/migrations/2019_07_21_000001_fix_old_groups_table_id_and_name.php
blob: 044ba132915ad4645f2a52e49308a1fc656b7056 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php

namespace Engelsystem\Migrations;

use Engelsystem\Database\Migration\Migration;

class FixOldGroupsTableIdAndName extends Migration
{
    /** @var array */
    protected $naming = [
        '1-Gast'              => 'Guest',
        '2-Engel'             => 'Angel',
        'Shirt-Manager'       => 'Shirt Manager',
        '3-Shift Coordinator' => 'Shift Coordinator',
        '4-Team Coordinator'  => 'Team Coordinator',
        '5-Bürokrat'          => 'Bureaucrat',
        '6-Developer'         => 'Developer',
    ];

    /** @var array */
    protected $ids = [
        -65 => -80,
        -70 => -90,
    ];

    /**
     * Run the migration
     */
    public function up()
    {
        $this->migrate($this->naming, $this->ids);
    }

    /**
     * Reverse the migration
     */
    public function down()
    {
        $this->migrate(array_flip($this->naming), array_flip($this->ids));
    }

    /**
     * @param array $naming
     * @param array $ids
     */
    protected function migrate($naming, $ids)
    {
        if (!$this->schema->hasTable('Groups')) {
            return;
        }

        $connection = $this->schema->getConnection();
        foreach ($connection->table('Groups')->get() as $data) {
            if (isset($naming[$data->Name])) {
                $data->Name = $naming[$data->Name];
            }

            $data->oldId = $data->UID;
            if (isset($ids[$data->oldId])) {
                $data->UID = $ids[$data->oldId];
            } elseif (isset($ids[$data->oldId * -1])) {
                $data->UID = $ids[$data->oldId * -1] * -1;
            }

            $connection
                ->table('Groups')
                ->where('UID', $data->oldId)
                ->update([
                    'UID'  => $data->UID * -1,
                    'Name' => $data->Name,
                ]);
        }
    }
}