Stop RemoveDuplicateStreamItemInstances from running forever

Don't use active record to avoid mixing global and shard local ids
Add an automated test.

Fixes CNVS-29370

Test Plan: Specs pass

Change-Id: If938289d854c6da05897fb675a36922180c0e3a9
Reviewed-on: https://gerrit.instructure.com/79647
Tested-by: Jenkins
Reviewed-by: Cody Cutrer <cody@instructure.com>
Product-Review: James Williams  <jamesw@instructure.com>
QA-Review: James Williams  <jamesw@instructure.com>
This commit is contained in:
Matthew Wheeler 2016-05-14 13:31:59 -06:00 committed by James Williams
parent 050b3767fd
commit c9c9abbc4f
2 changed files with 37 additions and 1 deletions

View File

@ -1,6 +1,6 @@
module DataFixup::RemoveDuplicateStreamItemInstances
def self.run
while (dups = StreamItemInstance.group(:stream_item_id, :user_id).having("COUNT(*) > 1").pluck(:stream_item_id, :user_id)) && dups.any?
while (dups = ActiveRecord::Base.connection.select_rows(%Q(SELECT stream_item_id, user_id FROM #{StreamItemInstance.quoted_table_name} GROUP BY stream_item_id, user_id HAVING COUNT(*) > 1))) && dups.any?
dups.each do |stream_item_id, user_id|
StreamItemInstance.where(:stream_item_id => stream_item_id, :user_id => user_id).offset(1).delete_all
end

View File

@ -0,0 +1,36 @@
#
# Copyright (C) 2016 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
require 'spec_helper'
require 'db/migrate/20160212204337_remove_duplicate_stream_item_instances.rb'
describe DataFixup::RemoveDuplicateStreamItemInstances do
it "should find and remove duplicates" do
mig = RemoveDuplicateStreamItemInstances.new
mig.down
user = User.create!
context = Course.create!
dt = DiscussionTopic.create!(:context => context)
dt.generate_stream_items([user])
stream_item = user.stream_item_instances.first.stream_item
StreamItemInstance.create!(context: stream_item.context, stream_item: stream_item, user: user)
expect{mig.up}.to change{StreamItemInstance.count}.from(2).to(1)
end
end