2019-12-03 20:09:22 +01:00
|
|
|
<?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;
|
|
|
|
|
|
|
|
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',
|
2020-09-03 11:15:16 +02:00
|
|
|
'id'
|
2019-12-03 20:09:22 +01:00
|
|
|
);
|
|
|
|
$this->schema->drop('PreviousQuestions');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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',
|
2020-09-03 11:15:16 +02:00
|
|
|
'QID'
|
2019-12-03 20:09:22 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->schema->drop('new_questions');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createNewQuestionsTable(): void
|
|
|
|
{
|
2019-12-08 01:56:51 +01:00
|
|
|
$this->schema->create(
|
|
|
|
'questions',
|
2019-12-03 20:09:22 +01:00
|
|
|
function (Blueprint $table): void {
|
|
|
|
$table->increments('id');
|
2019-12-08 02:09:52 +01:00
|
|
|
$this->referencesUser($table);
|
2019-12-03 20:09:22 +01:00
|
|
|
$table->text('text');
|
|
|
|
$table->text('answer')
|
|
|
|
->nullable();
|
|
|
|
$this->references($table, 'users', 'answerer_id')
|
|
|
|
->nullable();
|
2019-12-08 01:56:51 +01:00
|
|
|
}
|
|
|
|
);
|
2019-12-03 20:09:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createPreviousQuestionsTable(): void
|
|
|
|
{
|
2019-12-08 01:56:51 +01:00
|
|
|
$this->schema->create(
|
|
|
|
'Questions',
|
2019-12-03 20:09:22 +01:00
|
|
|
function (Blueprint $table): void {
|
|
|
|
$table->increments('QID');
|
|
|
|
$this->references($table, 'users', 'UID');
|
|
|
|
$table->text('Question');
|
|
|
|
$this->references($table, 'users', 'AID')
|
|
|
|
->nullable();
|
|
|
|
$table->text('Answer')
|
|
|
|
->nullable();
|
2019-12-08 01:56:51 +01:00
|
|
|
}
|
|
|
|
);
|
2019-12-03 20:09:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|