From c0bf0b56f1359025d4bd95f7480c45aed6ad9c9a Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Tue, 12 Nov 2019 21:49:31 +0100 Subject: Add the news_comments table migration --- ...019_11_12_000000_create_news_comments_table.php | 129 +++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 db/migrations/2019_11_12_000000_create_news_comments_table.php (limited to 'db/migrations/2019_11_12_000000_create_news_comments_table.php') diff --git a/db/migrations/2019_11_12_000000_create_news_comments_table.php b/db/migrations/2019_11_12_000000_create_news_comments_table.php new file mode 100644 index 00000000..cb7065b8 --- /dev/null +++ b/db/migrations/2019_11_12_000000_create_news_comments_table.php @@ -0,0 +1,129 @@ +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, + ]); + } + } +} -- cgit v1.2.3-70-g09d2 From 17192a2c412a6f5c4d8c10d8d25ef1a680bbce01 Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Tue, 12 Nov 2019 22:04:39 +0100 Subject: Adapt code style to PSR-12 --- db/migrations/2019_11_12_000000_create_news_comments_table.php | 1 + src/Models/News.php | 1 + src/Models/NewsComment.php | 1 + tests/Unit/Models/NewsCommentsTest.php | 1 + 4 files changed, 4 insertions(+) (limited to 'db/migrations/2019_11_12_000000_create_news_comments_table.php') diff --git a/db/migrations/2019_11_12_000000_create_news_comments_table.php b/db/migrations/2019_11_12_000000_create_news_comments_table.php index cb7065b8..00cf99b2 100644 --- a/db/migrations/2019_11_12_000000_create_news_comments_table.php +++ b/db/migrations/2019_11_12_000000_create_news_comments_table.php @@ -1,4 +1,5 @@