Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database changes #34

Merged
merged 4 commits into from
Sep 16, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/*
* This file is part of Flarum.
*
* (c) Toby Zerner <[email protected]>
*
* 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');
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/*
* This file is part of Flarum.
*
* (c) Toby Zerner <[email protected]>
*
* 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');
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/*
* This file is part of Flarum.
*
* (c) Toby Zerner <[email protected]>
*
* 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');
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of Flarum.
*
* (c) Toby Zerner <[email protected]>
*
* 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']);
});
}
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/*
* This file is part of Flarum.
*
* (c) Toby Zerner <[email protected]>
*
* 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');
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of Flarum.
*
* (c) Toby Zerner <[email protected]>
*
* 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']);
});
}
];
4 changes: 2 additions & 2 deletions src/Listener/AddFilterByMentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function subscribe(Dispatcher $events)
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);
}
}
}
6 changes: 3 additions & 3 deletions src/Listener/AddPostMentionedByRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ public function subscribe(Dispatcher $events)
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');
}
}

Expand Down