From 060c79ecc09061da00b7d40a54c9f1b65b1db7f2 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 23 Jul 2013 12:43:34 +1000 Subject: [PATCH] remove dupes before correcting topic_ids in user action consistency check job --- app/models/user_action.rb | 21 +++++++++++++++++++++ spec/models/user_action_spec.rb | 10 ++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/app/models/user_action.rb b/app/models/user_action.rb index 0660f24299b..1a6c5da4270 100644 --- a/app/models/user_action.rb +++ b/app/models/user_action.rb @@ -215,6 +215,27 @@ ORDER BY p.created_at desc def self.synchronize_target_topic_ids(post_ids = nil) + # nuke all dupes, using magic + builder = SqlBuilder.new < 0 AND + user_actions.id > ua2.id +SQL + + if post_ids + builder.where("user_actions.target_post_id in (:post_ids)", post_ids: post_ids) + end + + builder.exec + builder = SqlBuilder.new("UPDATE user_actions SET target_topic_id = (select topic_id from posts where posts.id = target_post_id) /*where*/") diff --git a/spec/models/user_action_spec.rb b/spec/models/user_action_spec.rb index c09c50e0f8b..e845e854429 100644 --- a/spec/models/user_action_spec.rb +++ b/spec/models/user_action_spec.rb @@ -273,10 +273,16 @@ describe UserAction do target_post_id: post.id, ) - action.reload - action.target_topic_id.should == -1 + UserAction.log_action!( + action_type: UserAction::NEW_PRIVATE_MESSAGE, + user_id: post.user.id, + acting_user_id: post.user.id, + target_topic_id: -2, + target_post_id: post.id, + ) UserAction.ensure_consistency! + action.reload action.target_topic_id.should == post.topic_id