ensure default sections are unique

prevent race conditions possibly creating multiple
default course sections

closes #CORE-2357

Change-Id: I4c3a6fe6a561afd7d67dbdc8da26dfa9b214af61
Reviewed-on: https://gerrit.instructure.com/178286
Reviewed-by: Rob Orton <rob@instructure.com>
QA-Review: Rob Orton <rob@instructure.com>
Product-Review: Rob Orton <rob@instructure.com>
Tested-by: Jenkins
This commit is contained in:
James Williams 2019-01-17 08:36:31 -07:00
parent ae4b5a1e22
commit 0b31194649
2 changed files with 46 additions and 2 deletions

View File

@ -2083,8 +2083,16 @@ class Course < ActiveRecord::Base
section.default_section = true
section.course = self
section.root_account_id = self.root_account_id
Shackles.activate(:master) do
section.save unless new_record?
unless new_record?
Shackles.activate(:master) do
CourseSection.unique_constraint_retry do |retry_count|
if retry_count > 0
section = course_sections.active.where(default_section: true).first
else
section.save
end
end
end
end
end
section

View File

@ -0,0 +1,36 @@
#
# Copyright (C) 2019 - present 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/>.
#
class AddDefaultSectionsUniqueIndex < ActiveRecord::Migration[5.1]
disable_ddl_transaction!
tag :postdeploy
def up
course_ids_to_fix = CourseSection.active.group(:course_id).where(:default_section => true).
having("COUNT(*) > 1").pluck(:course_id)
course_ids_to_fix.each do |course_id|
CourseSection.where(:course_id => course_id, :default_section => true).
order(:id).offset(1).update_all(:default_section => false)
end
add_index :course_sections, :course_id, :unique => true, :where => "default_section = 't' AND workflow_state <> 'deleted'",
:name => "index_course_sections_unique_default_section"
end
def down
remove_index :course_sections, :name => "index_course_sections_unique_default_section"
end
end