From 67c7b45dc85945bd6deaae554d0176f7a02748f9 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Mon, 17 Sep 2018 04:20:16 +0930 Subject: [PATCH] Database changes (#34) * Implement database changes * Split foreign keys in to their own migrations; rename pivot tables * Use whereColumn * Update core attribute names --- .../js/src/forum/addComposerAutocomplete.js | 2 +- .../components/PostMentionedNotification.js | 2 +- .../components/UserMentionedNotification.js | 2 +- ...e_mentions_posts_to_post_mentions_post.php | 14 +++++++ ...e_mentions_users_to_post_mentions_user.php | 14 +++++++ ...rename_mentions_id_to_mentions_post_id.php | 14 +++++++ ...ge_post_mentions_post_add_foreign_keys.php | 40 +++++++++++++++++++ ...rename_mentions_id_to_mentions_user_id.php | 14 +++++++ ...ge_post_mentions_user_add_foreign_keys.php | 40 +++++++++++++++++++ .../src/Listener/AddFilterByMentions.php | 4 +- .../AddPostMentionedByRelationship.php | 6 +-- .../Notification/PostMentionedBlueprint.php | 2 +- .../Notification/UserMentionedBlueprint.php | 2 +- 13 files changed, 146 insertions(+), 10 deletions(-) create mode 100644 extensions/mentions/migrations/2018_06_27_102000_rename_mentions_posts_to_post_mentions_post.php create mode 100644 extensions/mentions/migrations/2018_06_27_102100_rename_mentions_users_to_post_mentions_user.php create mode 100644 extensions/mentions/migrations/2018_06_27_102200_change_post_mentions_post_rename_mentions_id_to_mentions_post_id.php create mode 100644 extensions/mentions/migrations/2018_06_27_102300_change_post_mentions_post_add_foreign_keys.php create mode 100644 extensions/mentions/migrations/2018_06_27_102400_change_post_mentions_user_rename_mentions_id_to_mentions_user_id.php create mode 100644 extensions/mentions/migrations/2018_06_27_102500_change_post_mentions_user_add_foreign_keys.php diff --git a/extensions/mentions/js/src/forum/addComposerAutocomplete.js b/extensions/mentions/js/src/forum/addComposerAutocomplete.js index c0d63ec04..596243a91 100644 --- a/extensions/mentions/js/src/forum/addComposerAutocomplete.js +++ b/extensions/mentions/js/src/forum/addComposerAutocomplete.js @@ -126,7 +126,7 @@ export default function addComposerAutocomplete() { if (discussion) { discussion.posts() .filter(post => post && post.contentType() === 'comment' && (!composerPost || post.number() < composerPost.number())) - .sort((a, b) => b.time() - a.time()) + .sort((a, b) => b.createdAt() - a.createdAt()) .filter(post => { const user = post.user(); return user && userMatches(user); diff --git a/extensions/mentions/js/src/forum/components/PostMentionedNotification.js b/extensions/mentions/js/src/forum/components/PostMentionedNotification.js index b9ff41117..a3f69bf90 100644 --- a/extensions/mentions/js/src/forum/components/PostMentionedNotification.js +++ b/extensions/mentions/js/src/forum/components/PostMentionedNotification.js @@ -19,7 +19,7 @@ export default class PostMentionedNotification extends Notification { content() { const notification = this.props.notification; const auc = notification.additionalUnreadCount(); - const user = notification.sender(); + const user = notification.fromUser(); return app.translator.transChoice('flarum-mentions.forum.notifications.post_mentioned_text', auc + 1, { user, diff --git a/extensions/mentions/js/src/forum/components/UserMentionedNotification.js b/extensions/mentions/js/src/forum/components/UserMentionedNotification.js index 16b84f0df..0cf183676 100644 --- a/extensions/mentions/js/src/forum/components/UserMentionedNotification.js +++ b/extensions/mentions/js/src/forum/components/UserMentionedNotification.js @@ -12,7 +12,7 @@ export default class UserMentionedNotification extends Notification { } content() { - const user = this.props.notification.sender(); + const user = this.props.notification.fromUser(); return app.translator.trans('flarum-mentions.forum.notifications.user_mentioned_text', {user}); } diff --git a/extensions/mentions/migrations/2018_06_27_102000_rename_mentions_posts_to_post_mentions_post.php b/extensions/mentions/migrations/2018_06_27_102000_rename_mentions_posts_to_post_mentions_post.php new file mode 100644 index 000000000..a618d653b --- /dev/null +++ b/extensions/mentions/migrations/2018_06_27_102000_rename_mentions_posts_to_post_mentions_post.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('mentions_posts', 'post_mentions_post'); diff --git a/extensions/mentions/migrations/2018_06_27_102100_rename_mentions_users_to_post_mentions_user.php b/extensions/mentions/migrations/2018_06_27_102100_rename_mentions_users_to_post_mentions_user.php new file mode 100644 index 000000000..2f5fb5357 --- /dev/null +++ b/extensions/mentions/migrations/2018_06_27_102100_rename_mentions_users_to_post_mentions_user.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('mentions_users', 'post_mentions_user'); diff --git a/extensions/mentions/migrations/2018_06_27_102200_change_post_mentions_post_rename_mentions_id_to_mentions_post_id.php b/extensions/mentions/migrations/2018_06_27_102200_change_post_mentions_post_rename_mentions_id_to_mentions_post_id.php new file mode 100644 index 000000000..12aa24205 --- /dev/null +++ b/extensions/mentions/migrations/2018_06_27_102200_change_post_mentions_post_rename_mentions_id_to_mentions_post_id.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::renameColumn('post_mentions_post', 'mentions_id', 'mentions_post_id'); diff --git a/extensions/mentions/migrations/2018_06_27_102300_change_post_mentions_post_add_foreign_keys.php b/extensions/mentions/migrations/2018_06_27_102300_change_post_mentions_post_add_foreign_keys.php new file mode 100644 index 000000000..1482d1501 --- /dev/null +++ b/extensions/mentions/migrations/2018_06_27_102300_change_post_mentions_post_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_mentions_post') + ->whereNotExists(function ($query) { + $query->selectRaw(1)->from('posts')->whereColumn('id', 'post_id'); + }) + ->orWhereNotExists(function ($query) { + $query->selectRaw(1)->from('posts')->whereColumn('id', 'mentions_post_id'); + }) + ->delete(); + + $schema->table('post_mentions_post', function (Blueprint $table) { + $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade'); + $table->foreign('mentions_post_id')->references('id')->on('posts')->onDelete('cascade'); + }); + }, + + 'down' => function (Builder $schema) { + $schema->table('posts_mentions_posts', function (Blueprint $table) { + $table->dropForeign(['post_id', 'mentions_post_id']); + }); + } +]; diff --git a/extensions/mentions/migrations/2018_06_27_102400_change_post_mentions_user_rename_mentions_id_to_mentions_user_id.php b/extensions/mentions/migrations/2018_06_27_102400_change_post_mentions_user_rename_mentions_id_to_mentions_user_id.php new file mode 100644 index 000000000..f3cb8540e --- /dev/null +++ b/extensions/mentions/migrations/2018_06_27_102400_change_post_mentions_user_rename_mentions_id_to_mentions_user_id.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::renameColumn('post_mentions_user', 'mentions_id', 'mentions_user_id'); diff --git a/extensions/mentions/migrations/2018_06_27_102500_change_post_mentions_user_add_foreign_keys.php b/extensions/mentions/migrations/2018_06_27_102500_change_post_mentions_user_add_foreign_keys.php new file mode 100644 index 000000000..76b584c33 --- /dev/null +++ b/extensions/mentions/migrations/2018_06_27_102500_change_post_mentions_user_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_mentions_user') + ->whereNotExists(function ($query) { + $query->selectRaw(1)->from('posts')->whereColumn('id', 'post_id'); + }) + ->orWhereNotExists(function ($query) { + $query->selectRaw(1)->from('users')->whereColumn('id', 'mentions_user_id'); + }) + ->delete(); + + $schema->table('post_mentions_user', function (Blueprint $table) { + $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade'); + $table->foreign('mentions_user_id')->references('id')->on('users')->onDelete('cascade'); + }); + }, + + 'down' => function (Builder $schema) { + $schema->table('post_mentions_user', function (Blueprint $table) { + $table->dropForeign(['post_id', 'mentions_user_id']); + }); + } +]; diff --git a/extensions/mentions/src/Listener/AddFilterByMentions.php b/extensions/mentions/src/Listener/AddFilterByMentions.php index e5f943e81..993fce78d 100755 --- a/extensions/mentions/src/Listener/AddFilterByMentions.php +++ b/extensions/mentions/src/Listener/AddFilterByMentions.php @@ -30,8 +30,8 @@ class AddFilterByMentions public function addFilter(ConfigurePostsQuery $event) { if ($mentionedId = array_get($event->filter, 'mentioned')) { - $event->query->join('mentions_users', 'posts.id', '=', 'mentions_users.post_id') - ->where('mentions_users.mentions_id', '=', $mentionedId); + $event->query->join('post_mentions_user', 'posts.id', '=', 'post_mentions_user.post_id') + ->where('post_mentions_user.mentions_user_id', '=', $mentionedId); } } } diff --git a/extensions/mentions/src/Listener/AddPostMentionedByRelationship.php b/extensions/mentions/src/Listener/AddPostMentionedByRelationship.php index 22c71589b..fbf34b4ec 100755 --- a/extensions/mentions/src/Listener/AddPostMentionedByRelationship.php +++ b/extensions/mentions/src/Listener/AddPostMentionedByRelationship.php @@ -57,15 +57,15 @@ class AddPostMentionedByRelationship public function getModelRelationship(GetModelRelationship $event) { if ($event->isRelationship(Post::class, 'mentionedBy')) { - return $event->model->belongsToMany(Post::class, 'mentions_posts', 'mentions_id', 'post_id', null, null, 'mentionedBy'); + return $event->model->belongsToMany(Post::class, 'post_mentions_post', 'mentions_post_id', 'post_id', null, null, 'mentionedBy'); } if ($event->isRelationship(Post::class, 'mentionsPosts')) { - return $event->model->belongsToMany(Post::class, 'mentions_posts', 'post_id', 'mentions_id', null, null, 'mentionsPosts'); + return $event->model->belongsToMany(Post::class, 'post_mentions_post', 'post_id', 'mentions_post_id', null, null, 'mentionsPosts'); } if ($event->isRelationship(Post::class, 'mentionsUsers')) { - return $event->model->belongsToMany(User::class, 'mentions_users', 'post_id', 'mentions_id', null, null, 'mentionsUsers'); + return $event->model->belongsToMany(User::class, 'post_mentions_user', 'post_id', 'mentions_user_id', null, null, 'mentionsUsers'); } } diff --git a/extensions/mentions/src/Notification/PostMentionedBlueprint.php b/extensions/mentions/src/Notification/PostMentionedBlueprint.php index 029b45406..23dd48712 100644 --- a/extensions/mentions/src/Notification/PostMentionedBlueprint.php +++ b/extensions/mentions/src/Notification/PostMentionedBlueprint.php @@ -48,7 +48,7 @@ class PostMentionedBlueprint implements BlueprintInterface, MailableInterface /** * {@inheritdoc} */ - public function getSender() + public function getFromUser() { return $this->reply->user; } diff --git a/extensions/mentions/src/Notification/UserMentionedBlueprint.php b/extensions/mentions/src/Notification/UserMentionedBlueprint.php index 46fd8f445..0a3fd4648 100644 --- a/extensions/mentions/src/Notification/UserMentionedBlueprint.php +++ b/extensions/mentions/src/Notification/UserMentionedBlueprint.php @@ -41,7 +41,7 @@ class UserMentionedBlueprint implements BlueprintInterface, MailableInterface /** * {@inheritdoc} */ - public function getSender() + public function getFromUser() { return $this->post->user; }