consistency check, need to also ensure delete cleans stuff up, maybe make notifications trashable

This commit is contained in:
Sam 2013-05-16 17:50:14 +10:00
parent 88417725b5
commit 04b8cd5c95
3 changed files with 35 additions and 0 deletions

View file

@ -13,6 +13,17 @@ class Notification < ActiveRecord::Base
after_save :refresh_notification_count
after_destroy :refresh_notification_count
def self.ensure_consistency!
Notification.exec_sql("
DELETE FROM Notifications n WHERE notification_type = :id AND
NOT EXISTS(
SELECT 1 FROM posts p
JOIN topics t ON t.id = p.topic_id
WHERE p.deleted_at is null AND t.deleted_at IS NULL
AND p.post_number = n.post_number AND t.id = n.topic_id
)" , id: Notification.types[:private_message])
end
def self.types
@types ||= Enum.new(
:mentioned, :replied, :quoted, :edited, :liked, :private_message,

View file

@ -5,6 +5,7 @@ module Jobs
TopicUser.ensure_consistency!
UserVisit.ensure_consistency!
Group.refresh_automatic_groups!
Notification.ensure_consistency!
end
end
end

View file

@ -201,4 +201,27 @@ describe Notification do
end
end
describe 'ensure consistency' do
it 'deletes notifications if post is missing or deleted' do
ActiveRecord::Base.observers.disable :all
p = Fabricate(:post)
p2 = Fabricate(:post)
Notification.create!(read: false, user_id: p.user_id, topic_id: p.topic_id, post_number: p.post_number, data: '[]',
notification_type: Notification.types[:private_message])
Notification.create!(read: false, user_id: p2.user_id, topic_id: p2.topic_id, post_number: p2.post_number, data: '[]',
notification_type: Notification.types[:private_message])
Notification.create!(read: false, user_id: p2.user_id, topic_id: p2.topic_id, post_number: p2.post_number, data: '[]',
notification_type: Notification.types[:liked])
p2.trash!
# we may want to make notification "trashable" but for now we nuke pm notifications from deleted topics/posts
Notification.ensure_consistency!
Notification.count.should == 2
end
end
end