backfill root_account_id for enrollment_dates_overrides

refs FOO-3155
flag = none

test plan:
 • run data fixup migrations
 • should populate any root_account_id columns that are NULL
   for enrollment_dates_overrides table

Change-Id: Idbb8d9f5811cf8da3f7245572cd3f1e1fbf59a76
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/303632
Reviewed-by: Jacob Burroughs <jburroughs@instructure.com>
Migration-Review: Jacob Burroughs <jburroughs@instructure.com>
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
QA-Review: August Thornton <august@instructure.com>
Product-Review: August Thornton <august@instructure.com>
This commit is contained in:
August Thornton 2022-10-19 13:57:20 -06:00
parent 2656f1484a
commit cfa4c53bab
3 changed files with 109 additions and 0 deletions

View File

@ -0,0 +1,30 @@
# frozen_string_literal: true
#
# Copyright (C) 2022 - 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 PopulateRootAccountIdForEnrollmentDatesOverrides < ActiveRecord::Migration[6.1]
tag :postdeploy
def up
DataFixup::PopulateRootAccountIdForEnrollmentDatesOverrides.delay_if_production(
priority: Delayed::LOWER_PRIORITY,
n_strand: ["root_account_id_backfill_strand", Shard.current.database_server.id]
).run
end
end

View File

@ -0,0 +1,38 @@
# frozen_string_literal: true
#
# Copyright (C) 2022 - 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/>.
#
module DataFixup::PopulateRootAccountIdForEnrollmentDatesOverrides
def self.run
if Account.root_accounts.size == 1
EnrollmentDatesOverride
.where(root_account_id: nil)
.in_batches(of: 10_000)
.update_all(root_account_id: Account.root_accounts.take.id)
else
EnrollmentDatesOverride
.where(root_account_id: nil)
.find_each do |enrollment_dates_override|
root_account_id = enrollment_dates_override.context&.resolved_root_account_id ||
enrollment_dates_override.context&.root_account_id
enrollment_dates_override.update!(root_account_id: root_account_id) if root_account_id
end
end
end
end

View File

@ -0,0 +1,41 @@
# frozen_string_literal: true
#
# Copyright (C) 2022 - 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/>.
describe DataFixup::PopulateRootAccountIdForEnrollmentDatesOverrides do
let(:account) { account_model }
describe(".run") do
it "updates the root_account_id" do
course_with_teacher(account: account, active_all: true)
term = @course.enrollment_term
override = term.enrollment_dates_overrides.create!(
enrollment_type: "TeacherEnrollment",
end_at: 1.week.from_now, context: account
)
# callbacks / validations skipped
override.update_column(:root_account_id, nil)
expect(override.reload.root_account_id).to be_nil
DataFixup::PopulateRootAccountIdForEnrollmentDatesOverrides.run
expect(override.reload.root_account_id).to eq account.id
end
end
end