From 42bffa8b21e249e8eb494de2813eaa5f8dd5e4f5 Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Mon, 8 Nov 2021 16:23:30 -0700 Subject: [PATCH] cdc: improve reliability of fixture creation Change-Id: I4c458ab1a79ffba18eaec2bf732727f125650d62 Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/277645 Reviewed-by: Jacob Burroughs Tested-by: Service Cloud Jenkins QA-Review: Cody Cutrer Product-Review: Cody Cutrer --- .../fixtures/assignment_override.rb | 11 +++--- .../fixtures/blackout_date.rb | 15 ++++---- .../delayed/backend/active_record/job.rb | 38 ------------------- lib/cdc_migration_testing/fixtures/profile.rb | 2 +- .../fixtures/web_conference.rb | 29 ++++++++------ .../fixtures/wiki_page.rb | 15 ++++---- 6 files changed, 38 insertions(+), 72 deletions(-) delete mode 100644 lib/cdc_migration_testing/fixtures/delayed/backend/active_record/job.rb diff --git a/lib/cdc_migration_testing/fixtures/assignment_override.rb b/lib/cdc_migration_testing/fixtures/assignment_override.rb index deb8128186d..74f820623bb 100644 --- a/lib/cdc_migration_testing/fixtures/assignment_override.rb +++ b/lib/cdc_migration_testing/fixtures/assignment_override.rb @@ -26,11 +26,10 @@ module CdcFixtures assignment = Assignment.first raise CdcFixtures::ForeignKeyNotSatisfied unless assignment - AssignmentOverride.new({ - workflow_state: 'default', - title: 'default', - set_id: 1, - assignment_id: assignment.id - }) + AssignmentOverride.new(id: 1, + workflow_state: 'default', + title: 'default', + set_id: 1, + assignment_id: assignment.id) end end diff --git a/lib/cdc_migration_testing/fixtures/blackout_date.rb b/lib/cdc_migration_testing/fixtures/blackout_date.rb index c8793e1f9b7..2502726f51f 100644 --- a/lib/cdc_migration_testing/fixtures/blackout_date.rb +++ b/lib/cdc_migration_testing/fixtures/blackout_date.rb @@ -20,13 +20,12 @@ module CdcFixtures def self.create_blackout_date - BlackoutDate.new({ - context_type: 'Account', - context_id: 1, - start_date: Time.zone.today, - end_date: Time.zone.today, - event_title: 'default', - root_account_id: 1 - }) + BlackoutDate.new(id: 1, + context_type: 'Account', + context_id: 1, + start_date: Time.zone.today, + end_date: Time.zone.today, + event_title: 'default', + root_account_id: 1) end end diff --git a/lib/cdc_migration_testing/fixtures/delayed/backend/active_record/job.rb b/lib/cdc_migration_testing/fixtures/delayed/backend/active_record/job.rb deleted file mode 100644 index ade8b26ea68..00000000000 --- a/lib/cdc_migration_testing/fixtures/delayed/backend/active_record/job.rb +++ /dev/null @@ -1,38 +0,0 @@ -# frozen_string_literal: true - -# -# Copyright (C) 2020 - 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 . -# - -module CdcFixtures - def self.create_job - # The model-generator can successfully auto-create a delayed job model. - # However, this causes a problem when a PluginSetting model is created. - # When a PluginSetting is created, it also creates a delayed job (the - # inst-jobs gem does this automatically), and that job gets an id of 1 - # for some reason, even though a job with id=1 already exists. The new job - # id doesn't get auto-incremented to 2. - # I couldn't figure out why that was happening, so this is a workaround. - # Manually create a delayed job here, with an id of 2, so that the - # other job created along with the PluginSetting can use id 1. - return Delayed::Backend::ActiveRecord::Job.new( - id: 2, - run_at: Time.zone.now, - queue: 'test', - ) - end -end diff --git a/lib/cdc_migration_testing/fixtures/profile.rb b/lib/cdc_migration_testing/fixtures/profile.rb index fe0143dfb3c..726b136b6e3 100644 --- a/lib/cdc_migration_testing/fixtures/profile.rb +++ b/lib/cdc_migration_testing/fixtures/profile.rb @@ -21,6 +21,6 @@ module CdcFixtures def self.create_profile Course.find_or_create_by!(id: 1) - Profile.new(context_id: 1, context_type: 'Course', root_account_id: 1) + Profile.new(id: 1, context_id: 1, context_type: 'Course', root_account_id: 1) end end diff --git a/lib/cdc_migration_testing/fixtures/web_conference.rb b/lib/cdc_migration_testing/fixtures/web_conference.rb index 1d011304bba..f3c2220d23d 100644 --- a/lib/cdc_migration_testing/fixtures/web_conference.rb +++ b/lib/cdc_migration_testing/fixtures/web_conference.rb @@ -20,18 +20,23 @@ module CdcFixtures def self.create_web_conference - ps = PluginSetting.find_or_create_by(name: 'adobe_connect') - ps.settings = {} - ps.disabled = false - ps.save! + # the model-generator may have created a record with id 1 already, but + # bypassing the sequence, so it'll try to create with id 1 and fail. + # just let it try twice + PluginSetting.unique_constraint_retry do + ps = PluginSetting.where(name: 'adobe_connect').first_or_initialize + ps.settings = {} + ps.disabled = false + ps.save! if ps.changed? + end - return WebConference.new({ - title: 'default', - conference_type: 'AdobeConnect', - context_id: 1, - context_type: 'Course', - user_id: 1, - root_account_id: 1, - }) + WebConference.new( + title: 'default', + conference_type: 'AdobeConnect', + context_id: 1, + context_type: 'Course', + user_id: 1, + root_account_id: 1, + ) end end diff --git a/lib/cdc_migration_testing/fixtures/wiki_page.rb b/lib/cdc_migration_testing/fixtures/wiki_page.rb index 8d0c026ea9a..7ebf5009a9d 100644 --- a/lib/cdc_migration_testing/fixtures/wiki_page.rb +++ b/lib/cdc_migration_testing/fixtures/wiki_page.rb @@ -20,12 +20,13 @@ module CdcFixtures def self.create_wiki_page - Wiki.new.save!(validate: false) - WikiPage.new({ - wiki_id: 1, - workflow_state: 'default', - context_id: 1, - context_type: 'Course' - }) + wiki = Wiki.where(id: 1).first_or_initialize + wiki.save!(validate: false) if wiki.new_record? + + WikiPage.new(id: 1, + wiki_id: 1, + workflow_state: 'default', + context_id: 1, + context_type: 'Course') end end