migration to repair duplicate discussions

refs #5078

Change-Id: I12d0a952d860cacb81d2f4c4c0b2c40dc67d2af6
Reviewed-on: https://gerrit.instructure.com/5023
Tested-by: Hudson <hudson@instructure.com>
Reviewed-by: Zach Wily <zach@instructure.com>
This commit is contained in:
Cody Cutrer 2011-08-09 14:03:39 -06:00
parent 742d59713d
commit fe85551af6
1 changed files with 26 additions and 0 deletions

View File

@ -0,0 +1,26 @@
class FixDuplicateDiscussions < ActiveRecord::Migration
def self.up
DiscussionTopic.transaction do
duplicate_topics = DiscussionTopic.find(:all,
:select => 'old_assignment_id, context_id, context_type',
:conditions => "old_assignment_id IS NOT NULL AND workflow_state <> 'deleted' AND root_topic_id IS NULL AND context_type='Course'",
:group => 'old_assignment_id, context_id, context_type HAVING count(*) > 1')
duplicate_topics.each do |topic|
duplicates = DiscussionTopic.find(:all,
:conditions => ["context_id=? and context_type=? and old_assignment_id=? and workflow_state<>'deleted' and root_topic_id is null",
topic.context_id, topic.context_type, topic.old_assignment_id],
:order => 'updated_at DESC')
to_keep = duplicates.shift
duplicate_ids = duplicates.map(&:id)
DiscussionEntry.update_all("discussion_topic_id=#{to_keep.id}", :discussion_topic_id => duplicate_ids)
DiscussionTopic.update_all("root_topic_id=#{to_keep.id}", :root_topic_id => duplicate_ids)
DiscussionTopic.delete(duplicate_ids)
end
end
end
def self.down
end
end