From 7a722f690cffecf93a5c0d252219380f6e34a091 Mon Sep 17 00:00:00 2001 From: Jacob Fugal Date: Tue, 6 May 2014 16:49:31 -0600 Subject: [PATCH] update timestamp on discussions when merging users fixes CNVS-12782 discussions cache some material in the "materialized view". that material may include references to the use being deleted in a user merge. the source material was already being updated correctly by the merge, but the materialized view was not expiring. updating the updated_at on the appropriate topics will force the necessary materialized views to be refreshed. test-plan: - create two distinct users, A and B - as user A, create a discussion topic - as user B, create a second discussion topic - as user A, reply to user B's discussion topic - merge user A into user B - view the first discussion topic; user B should display as the author - view the second discussion topic; user B should display as the reply's author Change-Id: I48948e171b6645a371e92c8413fa289695d3d1a2 Reviewed-on: https://gerrit.instructure.com/34445 Tested-by: Jenkins Reviewed-by: Joel Hough Reviewed-by: Rob Orton QA-Review: Trevor deHaan Product-Review: Jacob Fugal --- lib/user_merge.rb | 14 +++++++++++++- spec/lib/user_merge_spec.rb | 29 +++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/lib/user_merge.rb b/lib/user_merge.rb index 5e6a4ae0fad..828a35c09de 100644 --- a/lib/user_merge.rb +++ b/lib/user_merge.rb @@ -144,11 +144,23 @@ class UserMerge end end from_user.all_conversations.find_each { |c| c.move_to_user(target_user) } unless Shard.current != target_user.shard + + # all topics changing ownership or with entries changing ownership need to be + # flagged as updated so the materialized views update + begin + entries = DiscussionEntry.where(user_id: from_user) + DiscussionTopic.where(id: entries.select(['discussion_topic_id'])).update_all(updated_at: Time.now.utc) + entries.update_all(user_id: target_user.id) + DiscussionTopic.where(user_id: from_user).update_all(user_id: target_user.id, updated_at: Time.now.utc) + rescue => e + Rails.logger.error "migrating discussions failed: #{e.to_s}" + end + updates = {} ['account_users', 'access_tokens', 'asset_user_accesses', 'attachments', 'calendar_events', 'collaborations', - 'context_module_progressions', 'discussion_entries', 'discussion_topics', + 'context_module_progressions', 'group_memberships', 'page_comments', 'rubric_assessments', 'submission_comment_participants', 'user_services', 'web_conferences', diff --git a/spec/lib/user_merge_spec.rb b/spec/lib/user_merge_spec.rb index 05c695cfaa5..cc43f6120d2 100644 --- a/spec/lib/user_merge_spec.rb +++ b/spec/lib/user_merge_spec.rb @@ -352,6 +352,35 @@ describe UserMerge do new_attachment = user2.attachments.not_deleted.detect{|a| a.md5 == attachment2.md5} new_attachment.display_name.should_not == "test.txt" # attachment2 should be copied and renamed because it has unique file data end + + it "should move discussion topics and entries" do + topic = course1.discussion_topics.create!(user: user2) + entry = topic.discussion_entries.create!(user: user2) + + UserMerge.from(user2).into(user1) + + topic.reload.user.should == user1 + entry.reload.user.should == user1 + end + + it "should freshen moved topics" do + topic = course1.discussion_topics.create!(user: user2) + now = Time.at(5.minutes.from_now.to_i) # truncate milliseconds + Timecop.freeze(now) do + UserMerge.from(user2).into(user1) + topic.reload.updated_at.should == now + end + end + + it "should freshen topics with moved entries" do + topic = course1.discussion_topics.create!(user: user1) + entry = topic.discussion_entries.create!(user: user2) + now = Time.at(5.minutes.from_now.to_i) # truncate milliseconds + Timecop.freeze(now) do + UserMerge.from(user2).into(user1) + topic.reload.updated_at.should == now + end + end end it "should update account associations" do