summaryrefslogtreecommitdiff
path: root/db/migrations/2019_11_29_000000_create_questions_table.php
blob: d06749319bf5bf5b7f883b7d8d61afce403dd78e (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
<?php

declare(strict_types=1);

namespace Engelsystem\Migrations;

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

/**
 * This migration creates the "questions" table and migrates the existing "Questions" records.
 */
class CreateQuestionsTable extends Migration
{
    use ChangesReferences;
    use Reference;

    /**
     * @return void
     */
    public function up(): void
    {
        $hasPreviousQuestionsTable = $this->schema->hasTable('Questions');

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

        $this->createNewQuestionsTable();

        if ($hasPreviousQuestionsTable) {
            $this->copyPreviousToNewQuestionsTable();
            $this->changeReferences(
                'PreviousQuestions',
                'QID',
                'questions',
                'id',
                'unsignedInteger'
            );
            $this->schema->drop('PreviousQuestions');
        }
    }

    /**
     * @return void
     */
    public function down(): void
    {
        // Rename as some SQL DBMS handle identifiers case insensitive
        $this->schema->rename('questions', 'new_questions');

        $this->createPreviousQuestionsTable();
        $this->copyNewToPreviousQuestionsTable();
        $this->changeReferences(
            'new_questions',
            'id',
            'Questions',
            'QID',
            'unsignedInteger'
        );

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

    /**
     * @return void
     */
    private function createNewQuestionsTable(): void
    {
        $this->schema->create(
            'questions',
            function (Blueprint $table) {
                $table->increments('id');
                $this->referencesUser($table);
                $table->text('text');
                $table->text('answer')
                    ->nullable();
                $this->references($table, 'users', 'answerer_id')
                    ->nullable();
            }
        );
    }

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

        foreach ($previousQuestionsRecords as $previousQuestionRecord) {
            $connection->table('questions')->insert([
                'id'          => $previousQuestionRecord->QID,
                'user_id'     => $previousQuestionRecord->UID,
                'text'        => $previousQuestionRecord->Question,
                'answerer_id' => $previousQuestionRecord->AID,
                'answer'      => $previousQuestionRecord->Answer,
            ]);
        }
    }

    /**
     * @return void
     */
    private function createPreviousQuestionsTable(): void
    {
        $this->schema->create(
            'Questions',
            function (Blueprint $table) {
                $table->increments('QID');
                $this->references($table, 'users', 'UID');
                $table->text('Question');
                $this->references($table, 'users', 'AID')
                    ->nullable();
                $table->text('Answer')
                    ->nullable();
            }
        );
    }

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

        foreach ($questionRecords as $questionRecord) {
            $connection->table('Questions')->insert([
                'QID'      => $questionRecord->id,
                'UID'      => $questionRecord->user_id,
                'Question' => $questionRecord->text,
                'AID'      => $questionRecord->answerer_id,
                'Answer'   => $questionRecord->answer,
            ]);
        }
    }
}