From 6df532bac7bf366514acf67cb8a65ea5ee8dbb53 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sat, 21 Jul 2018 18:35:50 +0930 Subject: [PATCH] Revert notifications_from table I didn't think this change through and it's going to be too difficult to implement right now. It can wait until we do the notifications revamp. For now reverting back to the old structure, with the `sender_id` column renamed to `from_user_id`. --- ...132900_create_notifications_from_table.php | 24 ------------- ...8_132901_seed_notifications_from_table.php | 34 ------------------- ...18_133000_change_notifications_columns.php | 5 +-- ..._change_notifications_add_foreign_keys.php | 10 +++++- .../core/src/Notification/Notification.php | 8 ++--- .../Notification/NotificationRepository.php | 2 +- .../src/Notification/NotificationSyncer.php | 6 ++-- framework/core/src/User/User.php | 2 +- 8 files changed, 21 insertions(+), 70 deletions(-) delete mode 100644 framework/core/migrations/2018_01_18_132900_create_notifications_from_table.php delete mode 100644 framework/core/migrations/2018_01_18_132901_seed_notifications_from_table.php diff --git a/framework/core/migrations/2018_01_18_132900_create_notifications_from_table.php b/framework/core/migrations/2018_01_18_132900_create_notifications_from_table.php deleted file mode 100644 index 9ee23e2ed..000000000 --- a/framework/core/migrations/2018_01_18_132900_create_notifications_from_table.php +++ /dev/null @@ -1,24 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -use Flarum\Database\Migration; -use Illuminate\Database\Schema\Blueprint; - -return Migration::createTable( - 'notifications_from', - function (Blueprint $table) { - $table->integer('id')->unsigned(); - $table->integer('from_user_id')->unsigned(); - - $table->foreign('id')->references('id')->on('notifications')->onDelete('cascade'); - $table->foreign('from_user_id')->references('id')->on('users')->onDelete('cascade'); - } -); diff --git a/framework/core/migrations/2018_01_18_132901_seed_notifications_from_table.php b/framework/core/migrations/2018_01_18_132901_seed_notifications_from_table.php deleted file mode 100644 index c58ba7df2..000000000 --- a/framework/core/migrations/2018_01_18_132901_seed_notifications_from_table.php +++ /dev/null @@ -1,34 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -use Illuminate\Database\Schema\Builder; - -return [ - 'up' => function (Builder $schema) { - $query = $schema->getConnection()->table('notifications') - ->whereExists(function ($query) { - $query->selectRaw(1)->from('users')->whereRaw('id = sender_id'); - }); - - foreach ($query->cursor() as $notification) { - $insert = [ - 'id' => $notification->id, - 'from_user_id' => $notification->sender_id - ]; - - $schema->getConnection()->table('notifications_from')->updateOrInsert($insert, $insert); - } - }, - - 'down' => function (Builder $schema) { - $schema->getConnection()->table('notifications_from')->truncate(); - } -]; diff --git a/framework/core/migrations/2018_01_18_133000_change_notifications_columns.php b/framework/core/migrations/2018_01_18_133000_change_notifications_columns.php index 56fef9ec8..fd4a71fe8 100644 --- a/framework/core/migrations/2018_01_18_133000_change_notifications_columns.php +++ b/framework/core/migrations/2018_01_18_133000_change_notifications_columns.php @@ -16,9 +16,10 @@ use Illuminate\Database\Schema\Builder; return [ 'up' => function (Builder $schema) { $schema->table('notifications', function (Blueprint $table) { - $table->dropColumn('sender_id', 'subject_type'); + $table->dropColumn('subject_type'); $table->renameColumn('time', 'created_at'); + $table->renameColumn('sender_id', 'from_user_id'); $table->dateTime('read_at')->nullable(); }); @@ -34,10 +35,10 @@ return [ 'down' => function (Builder $schema) { $schema->table('notifications', function (Blueprint $table) { - $table->integer('sender_id')->unsigned()->nullable(); $table->string('subject_type', 200)->nullable(); $table->renameColumn('created_at', 'time'); + $table->renameColumn('from_user_id', 'sender_id'); $table->boolean('is_read'); }); diff --git a/framework/core/migrations/2018_01_18_133100_change_notifications_add_foreign_keys.php b/framework/core/migrations/2018_01_18_133100_change_notifications_add_foreign_keys.php index c71327d10..442ae1dbd 100644 --- a/framework/core/migrations/2018_01_18_133100_change_notifications_add_foreign_keys.php +++ b/framework/core/migrations/2018_01_18_133100_change_notifications_add_foreign_keys.php @@ -23,14 +23,22 @@ return [ }) ->delete(); + $schema->getConnection() + ->table('notifications') + ->whereNotExists(function ($query) { + $query->selectRaw(1)->from('users')->whereRaw('id = from_user_id'); + }) + ->update(['from_user_id' => null]); + $schema->table('notifications', function (Blueprint $table) { $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); + $table->foreign('from_user_id')->references('id')->on('users')->onDelete('set null'); }); }, 'down' => function (Builder $schema) { $schema->table('notifications', function (Blueprint $table) { - $table->dropForeign(['user_id']); + $table->dropForeign(['user_id', 'from_user_id']); }); } ]; diff --git a/framework/core/src/Notification/Notification.php b/framework/core/src/Notification/Notification.php index bbae7c4ff..c36bff190 100644 --- a/framework/core/src/Notification/Notification.php +++ b/framework/core/src/Notification/Notification.php @@ -49,7 +49,7 @@ class Notification extends AbstractModel * * @var array */ - protected $dates = ['created_at', 'read_at', 'deleted_at']; + protected $dates = ['created_at', 'read_at']; /** * A map of notification types and the model classes to use for their @@ -112,7 +112,7 @@ class Notification extends AbstractModel */ public function user() { - return $this->belongsTo(User::class, 'user_id'); + return $this->belongsTo(User::class); } /** @@ -122,7 +122,7 @@ class Notification extends AbstractModel */ public function sender() { - return $this->belongsTo(User::class, 'sender_id'); + return $this->belongsTo(User::class, 'from_user_id'); } /** @@ -132,7 +132,7 @@ class Notification extends AbstractModel */ public function subject() { - return $this->morphTo('subject', 'subjectModel', 'subject_id'); + return $this->morphTo('subject', 'subjectModel'); } /** diff --git a/framework/core/src/Notification/NotificationRepository.php b/framework/core/src/Notification/NotificationRepository.php index 726206cb3..b8e2b6681 100644 --- a/framework/core/src/Notification/NotificationRepository.php +++ b/framework/core/src/Notification/NotificationRepository.php @@ -32,7 +32,7 @@ class NotificationRepository ) ->where('user_id', $user->id) ->whereIn('type', $user->getAlertableNotificationTypes()) - ->whereNull('deleted_at') + ->where('is_deleted', false) ->groupBy('type', 'subject_id') ->orderByRaw('MAX(created_at) DESC') ->skip($offset) diff --git a/framework/core/src/Notification/NotificationSyncer.php b/framework/core/src/Notification/NotificationSyncer.php index e4b21af80..a8cdd66ab 100644 --- a/framework/core/src/Notification/NotificationSyncer.php +++ b/framework/core/src/Notification/NotificationSyncer.php @@ -225,10 +225,10 @@ class NotificationSyncer protected function getAttributes(Blueprint\BlueprintInterface $blueprint) { return [ - 'type' => $blueprint::getType(), - 'sender_id' => ($sender = $blueprint->getSender()) ? $sender->id : null, + 'type' => $blueprint::getType(), + 'from_user_id' => ($sender = $blueprint->getSender()) ? $sender->id : null, 'subject_id' => ($subject = $blueprint->getSubject()) ? $subject->id : null, - 'data' => ($data = $blueprint->getData()) ? json_encode($data) : null + 'data' => ($data = $blueprint->getData()) ? json_encode($data) : null ]; } } diff --git a/framework/core/src/User/User.php b/framework/core/src/User/User.php index dc009f51d..d1322fbf2 100644 --- a/framework/core/src/User/User.php +++ b/framework/core/src/User/User.php @@ -435,7 +435,7 @@ class User extends AbstractModel $cached = $this->notifications() ->whereIn('type', $this->getAlertableNotificationTypes()) ->whereNull('read_at') - ->whereNull('deleted_at') + ->where('is_deleted', false) ->get(); }