From cbe6b3bd70c3edef43bc8799b87334c4aec4179c Mon Sep 17 00:00:00 2001 From: Molly Struve Date: Mon, 6 Apr 2020 10:18:05 -0500 Subject: [PATCH] [deploy] Skip updating a root comment if it doesnt exist (#7080) --- app/models/comment.rb | 10 +++++++++- app/workers/comments/calculate_score_worker.rb | 2 +- spec/workers/comments/calculate_score_worker_spec.rb | 1 + 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/models/comment.rb b/app/models/comment.rb index 04af345d2..e7e8d6833 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -185,7 +185,11 @@ class Comment < ApplicationRecord end def expire_root_fragment - root.touch + if root_exists? + root.touch + else + touch + end end def create_first_reaction @@ -214,6 +218,10 @@ class Comment < ApplicationRecord expire_root_fragment end + def root_exists? + ancestry && Comment.exists?(id: ancestry) + end + def send_email_notification Comments::SendEmailNotificationWorker.perform_async(id) end diff --git a/app/workers/comments/calculate_score_worker.rb b/app/workers/comments/calculate_score_worker.rb index 597281fd3..0d444df7a 100644 --- a/app/workers/comments/calculate_score_worker.rb +++ b/app/workers/comments/calculate_score_worker.rb @@ -12,7 +12,7 @@ module Comments spaminess_rating = BlackBox.calculate_spaminess(comment) comment.update_columns(score: score, spaminess_rating: spaminess_rating) - comment.root.save! unless comment.is_root? + comment.root.save! if !comment.is_root? && comment.root_exists? end end end diff --git a/spec/workers/comments/calculate_score_worker_spec.rb b/spec/workers/comments/calculate_score_worker_spec.rb index c7999d537..89fbfd52d 100644 --- a/spec/workers/comments/calculate_score_worker_spec.rb +++ b/spec/workers/comments/calculate_score_worker_spec.rb @@ -30,6 +30,7 @@ RSpec.describe Comments::CalculateScoreWorker, type: :worker do allow(root_comment).to receive(:save!) allow(child_comment).to receive(:update_columns) allow(child_comment).to receive(:is_root?).and_return(false) + allow(child_comment).to receive(:root_exists?).and_return(true) allow(child_comment).to receive(:root).and_return(root_comment) allow(Comment).to receive(:find_by).with(id: 1).and_return(child_comment)