summaryrefslogtreecommitdiff
path: root/db/migrations/2019_10_15_000000_create_news_table.php
blob: 540348cdd22421f9706648e6e6809eb79f40f190 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php

declare(strict_types=1);

namespace Engelsystem\Migrations;

use Carbon\Carbon;
use Engelsystem\Database\Migration\Migration;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Schema\Blueprint;
use stdClass;

/**
 * This migration creates the news table and copies the existing News table records to the new one.
 */
class CreateNewsTable extends Migration
{
    use ChangesReferences;
    use Reference;

    /**
     * Creates the news table, copies the data and drops the News table.
     */
    public function up(): void
    {
        $hasPreviousNewsTable = $this->schema->hasTable('News');

        if ($hasPreviousNewsTable) {
            // Rename because some SQL DBMS handle identifiers case insensitive
            $this->schema->rename('News', 'PreviousNews');
        }

        $this->createNewNewsTable();

        if ($hasPreviousNewsTable) {
            $this->copyPreviousToNewNewsTable();
            $this->changeReferences(
                'PreviousNews',
                'ID',
                'news',
                'id',
                'unsignedInteger'
            );
            $this->schema->drop('PreviousNews');
        }
    }

    /**
     * Recreates the previous News table, copies back the data and drops the new news table.
     */
    public function down(): void
    {
        // Rename as some SQL DBMS handle identifiers case insensitive
        $this->schema->rename('news', 'new_news');

        $this->createPreviousNewsTable();
        $this->copyNewToPreviousNewsTable();
        $this->changeReferences(
            'new_news',
            'id',
            'News',
            'ID',
            'unsignedInteger'
        );

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

    /**
     * @return void
     */
    private function createNewNewsTable(): void
    {
        $this->schema->create('news', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title', 150);
            $table->text('text');
            $table->boolean('is_meeting')->default(0);
            $this->referencesUser($table, false);
            $table->timestamps();
        });
    }

    /**
     * @return void
     */
    private function copyPreviousToNewNewsTable(): void
    {
        $connection = $this->schema->getConnection();
        /** @var stdClass[] $previousNewsRecords */
        $previousNewsRecords = $connection
            ->table('PreviousNews')
            ->get();

        foreach ($previousNewsRecords as $previousNews) {
            $date = Carbon::createFromTimestamp($previousNews->Datum);
            $connection->table('news')->insert([
                'id'         => $previousNews->ID,
                'title'      => $previousNews->Betreff,
                'text'       => $previousNews->Text,
                'is_meeting' => $previousNews->Treffen,
                'user_id'    => $previousNews->UID,
                'created_at' => $date,
                'updated_at' => $date,
            ]);
        }
    }

    /**
     * @return void
     */
    private function createPreviousNewsTable(): void
    {
        $this->schema->create('News', function (Blueprint $table) {
            $table->increments('ID');
            $table->integer('Datum');
            $table->string('Betreff', 150)
                ->default('');
            $table->text('Text');
            $this->references($table, 'users', 'UID');
            $table->boolean('Treffen')->default(false);
        });
    }

    /**
     * @return void
     */
    private function copyNewToPreviousNewsTable(): void
    {
        $connection = $this->schema->getConnection();
        /** @var Collection|stdClass[] $newsRecords */
        $newsRecords = $connection
            ->table('new_news')
            ->get();

        foreach ($newsRecords as $newsRecord) {
            $date = Carbon::createFromFormat('Y-m-d H:i:s', $newsRecord->created_at)
                ->getTimestamp();

            $connection->table('News')->insert([
                'ID'      => $newsRecord->id,
                'Datum'   => $date,
                'Betreff' => $newsRecord->title,
                'Text'    => $newsRecord->text,
                'UID'     => $newsRecord->user_id,
                'Treffen' => $newsRecord->is_meeting,
            ]);
        }
    }
}