From 065ef29e31181bd4f0ed92b47c6fd987ac7582ea Mon Sep 17 00:00:00 2001 From: Anna Buianova Date: Wed, 1 May 2019 20:15:06 +0300 Subject: [PATCH] Added unique index to notifications #2124 (#2213) * Added unique index to notifications #2124 * Fix migration + add index to schema.rb * Shoulda matcher notification unique validation --- app/models/notification.rb | 2 +- app/services/notifications/new_follower/send.rb | 2 +- .../20190326085046_add_unique_index_to_notifications.rb | 5 +++++ db/schema.rb | 1 + spec/models/notification_spec.rb | 2 ++ 5 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20190326085046_add_unique_index_to_notifications.rb diff --git a/app/models/notification.rb b/app/models/notification.rb index 6d068aefc..44b4562fb 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -8,7 +8,7 @@ class Notification < ApplicationRecord before_create :mark_notified_at_time - validates :user_id, uniqueness: { scope: %i[notifiable_id notifiable_type action] } + validates :user_id, uniqueness: { scope: %i[organization_id notifiable_id notifiable_type action] } class << self def send_new_follower_notification(follow, is_read = false) diff --git a/app/services/notifications/new_follower/send.rb b/app/services/notifications/new_follower/send.rb index f4683cacc..e86c2f287 100644 --- a/app/services/notifications/new_follower/send.rb +++ b/app/services/notifications/new_follower/send.rb @@ -37,7 +37,7 @@ module Notifications notification = Notification.find_by(notification_params)&.destroy else json_data = { user: user_data(follower), aggregated_siblings: aggregated_siblings } - notification = Notification.find_or_create_by(notification_params) + notification = Notification.find_or_initialize_by(notification_params) notification.notifiable_id = recent_follows.first.id notification.notifiable_type = "Follow" notification.json_data = json_data diff --git a/db/migrate/20190326085046_add_unique_index_to_notifications.rb b/db/migrate/20190326085046_add_unique_index_to_notifications.rb new file mode 100644 index 000000000..06dc31525 --- /dev/null +++ b/db/migrate/20190326085046_add_unique_index_to_notifications.rb @@ -0,0 +1,5 @@ +class AddUniqueIndexToNotifications < ActiveRecord::Migration[5.1] + def change + add_index :notifications, %i[user_id organization_id notifiable_id notifiable_type action], unique: true, name: "index_notifications_on_user_organization_notifiable_and_action" + end +end diff --git a/db/schema.rb b/db/schema.rb index 60cf90a2f..5258d0edd 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -515,6 +515,7 @@ ActiveRecord::Schema.define(version: 2019_04_30_123156) do t.index ["json_data"], name: "index_notifications_on_json_data", using: :gin t.index ["notifiable_id"], name: "index_notifications_on_notifiable_id" t.index ["notifiable_type"], name: "index_notifications_on_notifiable_type" + t.index ["user_id", "organization_id", "notifiable_id", "notifiable_type", "action"], name: "index_notifications_on_user_organization_notifiable_and_action", unique: true t.index ["user_id"], name: "index_notifications_on_user_id" end diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb index 3783f00b1..9faa58331 100644 --- a/spec/models/notification_spec.rb +++ b/spec/models/notification_spec.rb @@ -9,6 +9,8 @@ RSpec.describe Notification, type: :model do let(:follow_instance) { user.follow(user2) } let(:badge_achievement) { create(:badge_achievement) } + it { is_expected.to validate_uniqueness_of(:user_id).scoped_to(%i[organization_id notifiable_id notifiable_type action]) } + describe "when trying to #send_new_follower_notification after following a tag" do let(:tag) { create(:tag) } let(:tag_follow) { user.follow(tag) }