From 74fac8c2063f7eed140055ba817096d35ed99df8 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Mon, 17 Sep 2018 04:19:51 +0930 Subject: [PATCH] Database changes (#16) * Add foreign keys * Rename table * Use whereColumn * Update core attribute names --- .../forum/components/PostLikedNotification.js | 2 +- ...00600_rename_posts_likes_to_post_likes.php | 14 +++++++ ...700_change_post_likes_add_foreign_keys.php | 40 +++++++++++++++++++ .../src/Listener/AddPostLikesRelationship.php | 2 +- .../src/Notification/PostLikedBlueprint.php | 2 +- 5 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 extensions/likes/migrations/2018_06_27_100600_rename_posts_likes_to_post_likes.php create mode 100644 extensions/likes/migrations/2018_06_27_100700_change_post_likes_add_foreign_keys.php diff --git a/extensions/likes/js/src/forum/components/PostLikedNotification.js b/extensions/likes/js/src/forum/components/PostLikedNotification.js index 37cf19bff..f399acfdf 100644 --- a/extensions/likes/js/src/forum/components/PostLikedNotification.js +++ b/extensions/likes/js/src/forum/components/PostLikedNotification.js @@ -13,7 +13,7 @@ export default class PostLikedNotification extends Notification { content() { const notification = this.props.notification; - const user = notification.sender(); + const user = notification.fromUser(); const auc = notification.additionalUnreadCount(); return app.translator.transChoice('flarum-likes.forum.notifications.post_liked_text', auc + 1, { diff --git a/extensions/likes/migrations/2018_06_27_100600_rename_posts_likes_to_post_likes.php b/extensions/likes/migrations/2018_06_27_100600_rename_posts_likes_to_post_likes.php new file mode 100644 index 000000000..7b639904c --- /dev/null +++ b/extensions/likes/migrations/2018_06_27_100600_rename_posts_likes_to_post_likes.php @@ -0,0 +1,14 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Flarum\Database\Migration; + +return Migration::renameTable('posts_likes', 'post_likes'); diff --git a/extensions/likes/migrations/2018_06_27_100700_change_post_likes_add_foreign_keys.php b/extensions/likes/migrations/2018_06_27_100700_change_post_likes_add_foreign_keys.php new file mode 100644 index 000000000..c11762096 --- /dev/null +++ b/extensions/likes/migrations/2018_06_27_100700_change_post_likes_add_foreign_keys.php @@ -0,0 +1,40 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Schema\Builder; + +return [ + 'up' => function (Builder $schema) { + // Delete rows with non-existent entities so that we will be able to create + // foreign keys without any issues. + $schema->getConnection() + ->table('post_likes') + ->whereNotExists(function ($query) { + $query->selectRaw(1)->from('posts')->whereColumn('id', 'post_id'); + }) + ->orWhereNotExists(function ($query) { + $query->selectRaw(1)->from('users')->whereColumn('id', 'user_id'); + }) + ->delete(); + + $schema->table('post_likes', function (Blueprint $table) { + $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade'); + $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); + }); + }, + + 'down' => function (Builder $schema) { + $schema->table('post_likes', function (Blueprint $table) { + $table->dropForeign(['post_id', 'user_id']); + }); + } +]; diff --git a/extensions/likes/src/Listener/AddPostLikesRelationship.php b/extensions/likes/src/Listener/AddPostLikesRelationship.php index bbfa80e19..7bf008c21 100755 --- a/extensions/likes/src/Listener/AddPostLikesRelationship.php +++ b/extensions/likes/src/Listener/AddPostLikesRelationship.php @@ -42,7 +42,7 @@ class AddPostLikesRelationship public function getModelRelationship(GetModelRelationship $event) { if ($event->isRelationship(Post::class, 'likes')) { - return $event->model->belongsToMany(User::class, 'posts_likes', 'post_id', 'user_id', null, null, 'likes'); + return $event->model->belongsToMany(User::class, 'post_likes', 'post_id', 'user_id', null, null, 'likes'); } } diff --git a/extensions/likes/src/Notification/PostLikedBlueprint.php b/extensions/likes/src/Notification/PostLikedBlueprint.php index 62509310e..851d0f009 100644 --- a/extensions/likes/src/Notification/PostLikedBlueprint.php +++ b/extensions/likes/src/Notification/PostLikedBlueprint.php @@ -48,7 +48,7 @@ class PostLikedBlueprint implements BlueprintInterface /** * {@inheritdoc} */ - public function getSender() + public function getFromUser() { return $this->user; }