make canvadocs_submissions entries unique
closes CNVS-26089 Test plan: * in a rails console, call DataFixup::CreateCanvadocsSubmissionsRecords.run a few times * run migrations * there should be no duplicate entries in canvadocs_submissions * you should not be able to create duplicate entries in canvadocs_submissions Change-Id: I38cd3c990c13434d5daa016f3b4d192bdc877e16 Reviewed-on: https://gerrit.instructure.com/69327 Tested-by: Jenkins Reviewed-by: Keith T. Garner <kgarner@instructure.com> QA-Review: Amber Taniuchi <amber@instructure.com> Product-Review: Cameron Matheson <cameron@instructure.com>
This commit is contained in:
parent
f94d1a5d98
commit
987b6c2bb1
|
@ -0,0 +1,30 @@
|
|||
class EnsureUniquenessOnCanvadocsSubmissions < ActiveRecord::Migration
|
||||
tag :postdeploy
|
||||
disable_ddl_transaction!
|
||||
|
||||
def up
|
||||
DataFixup::RemoveDuplicateCanvadocsSubmissions.run
|
||||
|
||||
remove_index :canvadocs_submissions, :canvadoc_id
|
||||
remove_index :canvadocs_submissions, :crocodoc_document_id
|
||||
|
||||
add_index :canvadocs_submissions, [:submission_id, :canvadoc_id],
|
||||
where: "canvadoc_id IS NOT NULL",
|
||||
name: "unique_submissions_and_canvadocs",
|
||||
unique: true, algorithm: :concurrently
|
||||
add_index :canvadocs_submissions, [:submission_id, :crocodoc_document_id],
|
||||
where: "crocodoc_document_id IS NOT NULL",
|
||||
name: "unique_submissions_and_crocodocs",
|
||||
unique: true, algorithm: :concurrently
|
||||
end
|
||||
|
||||
def down
|
||||
remove_index "canvadocs_submissions", name: "unique_submissions_and_canvadocs"
|
||||
remove_index "canvadocs_submissions", name: "unique_submissions_and_crocodocs"
|
||||
|
||||
add_index :canvadocs_submissions, :canvadoc_id,
|
||||
where: "canvadoc_id IS NOT NULL"
|
||||
add_index :canvadocs_submissions, :crocodoc_document_id,
|
||||
where: "crocodoc_document_id IS NOT NULL"
|
||||
end
|
||||
end
|
|
@ -0,0 +1,21 @@
|
|||
module DataFixup::RemoveDuplicateCanvadocsSubmissions
|
||||
def self.run
|
||||
%w[crocodoc_document_id canvadoc_id].each do |column|
|
||||
duplicates = CanvadocsSubmission.
|
||||
select("#{column}, submission_id").
|
||||
group("#{column}, submission_id").
|
||||
having("count(*) > 1")
|
||||
|
||||
duplicates.find_each do |dup|
|
||||
scope = CanvadocsSubmission.where(
|
||||
column => dup[column],
|
||||
submission_id: dup.submission_id
|
||||
)
|
||||
keeper = scope.first
|
||||
scope.where("id <> ?", keeper.id).delete_all
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class CanvadocsSubmission < ActiveRecord::Base; end
|
||||
end
|
Loading…
Reference in New Issue