summaryrefslogtreecommitdiff
path: root/db/migrations/2018_09_24_000000_create_event_config_table.php
blob: dd457d3f9fe0d2bcb13d2a41dbdd4606f3b84d25 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php

namespace Engelsystem\Migrations;

use Carbon\Carbon;
use Engelsystem\Database\Migration\Migration;
use Engelsystem\Models\EventConfig;
use Illuminate\Database\QueryException;
use Illuminate\Database\Schema\Blueprint;

class CreateEventConfigTable extends Migration
{
    protected $mapping = [
        'buildup_start_date' => 'buildup_start',
        'event_start_date'   => 'event_start',
        'event_end_date'     => 'event_end',
        'teardown_end_date'  => 'teardown_end',
    ];

    /**
     * Run the migration
     */
    public function up()
    {
        foreach (['json', 'text'] as $type) {
            try {
                $this->schema->create('event_config', function (Blueprint $table) use ($type) {
                    $table->string('name')->index()->unique();
                    $table->{$type}('value');
                    $table->timestamps();
                });
            } catch (QueryException $e) {
                if ($type != 'json') {
                    throw $e;
                }

                continue;
            }

            break;
        }

        if ($this->schema->hasTable('EventConfig')) {
            $config = $this->schema->getConnection()
                ->table('EventConfig')
                ->first();

            if (!empty($config)) {
                (new EventConfig([
                    'name'  => 'name',
                    'value' => $config->event_name,
                ]))->save();

                (new EventConfig([
                    'name'  => 'welcome_msg',
                    'value' => $config->event_welcome_msg,
                ]))->save();

                foreach ($this->mapping as $old => $new) {
                    (new EventConfig([
                        'name'  => $new,
                        'value' => (new Carbon())->setTimestamp($config->{$old}),
                    ]))->save();
                }
            }

            $this->schema->drop('EventConfig');
        }
    }

    /**
     * Reverse the migration
     */
    public function down()
    {
        $this->schema->create('EventConfig', function (Blueprint $table) {
            $table->string('event_name')->nullable();
            $table->integer('buildup_start_date')->nullable();
            $table->integer('event_start_date')->nullable();
            $table->integer('event_end_date')->nullable();
            $table->integer('teardown_end_date')->nullable();
            $table->string('event_welcome_msg')->nullable();
        });

        $config = new EventConfig();
        $data = [
            'event_name'        => $config->findOrNew('name')->value,
            'event_welcome_msg' => $config->findOrNew('welcome_msg')->value,
        ];
        foreach ($this->mapping as $new => $old) {
            /** @var Carbon $value */
            $value = $config->findOrNew($old)->value;

            if (!$value) {
                continue;
            }

            $data[$new] = $value->getTimestamp();
        }

        $dataNotEmpty = false;
        foreach ($data as $value) {
            $dataNotEmpty |= !empty($value);
        }

        if ($dataNotEmpty) {
            $this->schema->getConnection()
                ->table('EventConfig')
                ->insert($data);
        }

        $this->schema->dropIfExists('event_config');
    }
}