diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2019-11-20 00:00:57 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-20 00:00:57 +0100 |
commit | aa91ab4cf7219268bbddcd2f3a12bc5c1c5da96f (patch) | |
tree | 23b2f1f0c5293902be0f2734beb0845102f76837 /db/migrations | |
parent | d83d60ce8d986e4e7cf28680189b5ef43b780e10 (diff) | |
parent | 17192a2c412a6f5c4d8c10d8d25ef1a680bbce01 (diff) |
Merge pull request #675 from weeman1337/feature-news-comments-migration
Introduce the NewsComments model
Diffstat (limited to 'db/migrations')
-rw-r--r-- | db/migrations/2019_10_15_000000_create_news_table.php | 2 | ||||
-rw-r--r-- | db/migrations/2019_11_12_000000_create_news_comments_table.php | 130 |
2 files changed, 131 insertions, 1 deletions
diff --git a/db/migrations/2019_10_15_000000_create_news_table.php b/db/migrations/2019_10_15_000000_create_news_table.php index c87972ef..540348cd 100644 --- a/db/migrations/2019_10_15_000000_create_news_table.php +++ b/db/migrations/2019_10_15_000000_create_news_table.php @@ -128,7 +128,7 @@ class CreateNewsTable extends Migration private function copyNewToPreviousNewsTable(): void { $connection = $this->schema->getConnection(); - /** @var Collection[]|stdClass[] $newsRecords */ + /** @var Collection|stdClass[] $newsRecords */ $newsRecords = $connection ->table('new_news') ->get(); 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..00cf99b2 --- /dev/null +++ b/db/migrations/2019_11_12_000000_create_news_comments_table.php @@ -0,0 +1,130 @@ +<?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, + ]); + } + } +} |