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
|
<?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 news_comments table and copies the existing NewsComments table records to the new one.
*/
class CreateNewsCommentsTable extends Migration
{
use ChangesReferences;
use Reference;
/**
* Creates the news_comments table, copies the data and drops the NewsComments table.
*/
public function up(): void
{
$this->createNewNewsCommentsTable();
if ($this->schema->hasTable('NewsComments')) {
$this->copyPreviousToNewNewsCommentsTable();
$this->changeReferences(
'NewsComments',
'ID',
'news_comments',
'id',
'unsignedInteger'
);
$this->schema->drop('NewsComments');
}
}
/**
* Recreates the previous NewsComments table, copies back the data and drops the new news_comments table.
*/
public function down(): void
{
$this->createPreviousNewsCommentsTable();
$this->copyNewToPreviousNewsCommentsTable();
$this->changeReferences(
'news_comments',
'id',
'NewsComments',
'ID',
'unsignedInteger'
);
$this->schema->drop('news_comments');
}
/**
* @return void
*/
private function createNewNewsCommentsTable(): void
{
$this->schema->create('news_comments', function (Blueprint $table) {
$table->increments('id');
$this->references($table, 'news', 'news_id');
$table->text('text');
$this->referencesUser($table, false);
$table->timestamps();
});
}
/**
* @return void
*/
private function copyPreviousToNewNewsCommentsTable(): void
{
$connection = $this->schema->getConnection();
/** @var stdClass[] $previousNewsCommentsRecords */
$previousNewsCommentsRecords = $connection
->table('NewsComments')
->get();
foreach ($previousNewsCommentsRecords as $previousNewsComment) {
$connection->table('news_comments')->insert([
'id' => $previousNewsComment->ID,
'news_id' => $previousNewsComment->Refid,
'text' => $previousNewsComment->Text,
'user_id' => $previousNewsComment->UID,
'created_at' => $previousNewsComment->Datum,
'updated_at' => $previousNewsComment->Datum,
]);
}
}
/**
* @return void
*/
private function createPreviousNewsCommentsTable(): void
{
$this->schema->create('NewsComments', function (Blueprint $table) {
$table->increments('ID');
$this->references($table, 'news', 'Refid');
$table->dateTime('Datum');
$table->text('Text');
$this->references($table, 'users', 'UID');
});
}
/**
* @return void
*/
private function copyNewToPreviousNewsCommentsTable(): void
{
$connection = $this->schema->getConnection();
/** @var Collection|stdClass[] $newsCommentsRecords */
$newsCommentsRecords = $connection
->table('news_comments')
->get();
foreach ($newsCommentsRecords as $newsCommentRecord) {
$connection->table('NewsComments')->insert([
'ID' => $newsCommentRecord->id,
'Datum' => $newsCommentRecord->created_at,
'Refid' => $newsCommentRecord->news_id,
'Text' => $newsCommentRecord->text,
'UID' => $newsCommentRecord->user_id,
]);
}
}
}
|