use helper methods for check constraints on old migrations

not that we really need to change, but it serves as an example for future
migrations

Change-Id: Ie1a1975311c81b0c145c17af46e57e604a01eca5
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/327026
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
Reviewed-by: Jackson Howe <jackson.howe@instructure.com>
QA-Review: Cody Cutrer <cody@instructure.com>
Product-Review: Cody Cutrer <cody@instructure.com>
Build-Review: Cody Cutrer <cody@instructure.com>
Migration-Review: Cody Cutrer <cody@instructure.com>
This commit is contained in:
Cody Cutrer 2023-09-06 10:16:18 -06:00
parent b75528cc31
commit 2039924c66
10 changed files with 50 additions and 58 deletions

View File

@ -339,9 +339,9 @@ GEM
activerecord (7.0.7.2)
activemodel (= 7.0.7.2)
activesupport (= 7.0.7.2)
activerecord-pg-extensions (0.4.4)
activerecord (>= 6.0, < 7.1)
railties (>= 6.0, < 7.1)
activerecord-pg-extensions (0.5.2)
activerecord (~> 7.0.0)
railties (~> 7.0.0)
activestorage (7.0.7.2)
actionpack (= 7.0.7.2)
activejob (= 7.0.7.2)
@ -613,9 +613,9 @@ GEM
json-jwt (~> 1.7)
rexml
simple_oauth (~> 0.3.1)
inst-jobs (3.1.11)
inst-jobs (3.1.12)
activerecord (>= 6.0)
activerecord-pg-extensions (~> 0.4.4)
activerecord-pg-extensions (~> 0.4)
activesupport (>= 6.0)
after_transaction_commit (>= 1.0, < 3)
debug_inspector (~> 1.0)

View File

@ -18,24 +18,16 @@
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
class AddNotNullConstraintToScoresCourseScore < ActiveRecord::Migration[5.0]
class AddNotNullConstraintToScoresCourseScore < ActiveRecord::Migration[7.0]
tag :postdeploy
disable_ddl_transaction!
def up
connection.execute(<<SQL.squish)
ALTER TABLE #{Score.quoted_table_name}
ADD CONSTRAINT course_score_not_null CHECK (course_score IS NOT NULL) NOT VALID;
SQL
connection.execute(<<SQL.squish)
ALTER TABLE #{Score.quoted_table_name} VALIDATE CONSTRAINT course_score_not_null;
SQL
end
def down
connection.execute(<<SQL.squish)
ALTER TABLE #{Score.quoted_table_name} DROP CONSTRAINT course_score_not_null;
SQL
def change
add_check_constraint(:scores,
"course_score IS NOT NULL",
name: "course_score_not_null",
validate: false,
if_not_exists: true)
validate_constraint(:scores, :course_score_not_null)
end
end

View File

@ -17,7 +17,7 @@
# 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 CleanUpAssignmentOverrides < ActiveRecord::Migration[5.1]
class CleanUpAssignmentOverrides < ActiveRecord::Migration[7.0]
tag :predeploy
disable_ddl_transaction!
@ -27,19 +27,15 @@ class CleanUpAssignmentOverrides < ActiveRecord::Migration[5.1]
# this fix is fast enough to run synchronously, without requiring a multi-deploy rollout of the check constraint
DataFixup::RemoveInvalidAssignmentOverrides.run
# we will break the constraint creation and validation into separate queries to reduce time spent in ex-lock
execute(<<~SQL.squish)
ALTER TABLE #{AssignmentOverride.quoted_table_name}
ADD CONSTRAINT require_quiz_or_assignment
CHECK (workflow_state='deleted' OR quiz_id IS NOT NULL OR assignment_id IS NOT NULL)
NOT VALID
SQL
execute("ALTER TABLE #{AssignmentOverride.quoted_table_name} VALIDATE CONSTRAINT require_quiz_or_assignment")
add_check_constraint(:assignment_overrides,
"workflow_state='deleted' OR quiz_id IS NOT NULL OR assignment_id IS NOT NULL",
name: "require_quiz_or_assignment",
if_not_exists: true,
validate: false)
validate_constraint(:assignment_overrides, :require_quiz_or_assignment)
end
def self.down
execute(<<~SQL.squish)
ALTER TABLE #{AssignmentOverride.quoted_table_name}
DROP CONSTRAINT IF EXISTS require_quiz_or_assignment
SQL
remove_check_constraint(:assignment_overrides, name: "require_quiz_or_assignment", if_exists: true)
end
end

View File

@ -27,7 +27,8 @@ class ChangeRequireQuizOrAssignmentConstraint < ActiveRecord::Migration[7.0]
# Split constraint creation and validation to reduce exclusive locking time
add_check_constraint(:assignment_overrides,
"workflow_state='deleted' OR quiz_id IS NOT NULL OR assignment_id IS NOT NULL OR context_module_id IS NOT NULL",
name: :require_quiz_or_assignment_or_module,
name: "require_quiz_or_assignment_or_module",
if_not_exists: true,
validate: false)
validate_constraint(:assignment_overrides, :require_quiz_or_assignment_or_module)
end
@ -36,7 +37,8 @@ class ChangeRequireQuizOrAssignmentConstraint < ActiveRecord::Migration[7.0]
remove_check_constraint(:assignment_overrides, name: "require_quiz_or_assignment_or_module", if_exists: true)
add_check_constraint(:assignment_overrides,
"workflow_state='deleted' OR quiz_id IS NOT NULL OR assignment_id IS NOT NULL",
name: :require_quiz_or_assignment,
name: "require_quiz_or_assignment",
if_not_exists: true,
validate: false)
validate_constraint(:assignment_overrides, :require_quiz_or_assignment)
end

View File

@ -27,9 +27,9 @@ GEM
activerecord (7.0.7.2)
activemodel (= 7.0.7.2)
activesupport (= 7.0.7.2)
activerecord-pg-extensions (0.4.4)
activerecord (>= 6.0, < 7.1)
railties (>= 6.0, < 7.1)
activerecord-pg-extensions (0.5.2)
activerecord (~> 7.0.0)
railties (~> 7.0.0)
activesupport (7.0.7.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
@ -57,9 +57,9 @@ GEM
raabro (~> 1.4)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
inst-jobs (3.1.11)
inst-jobs (3.1.12)
activerecord (>= 6.0)
activerecord-pg-extensions (~> 0.4.4)
activerecord-pg-extensions (~> 0.4)
activesupport (>= 6.0)
after_transaction_commit (>= 1.0, < 3)
debug_inspector (~> 1.0)

View File

@ -27,9 +27,9 @@ GEM
activerecord (7.0.7.2)
activemodel (= 7.0.7.2)
activesupport (= 7.0.7.2)
activerecord-pg-extensions (0.4.4)
activerecord (>= 6.0, < 7.1)
railties (>= 6.0, < 7.1)
activerecord-pg-extensions (0.5.2)
activerecord (~> 7.0.0)
railties (~> 7.0.0)
activesupport (7.0.7.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)

View File

@ -53,9 +53,9 @@ GEM
activerecord (7.0.7.2)
activemodel (= 7.0.7.2)
activesupport (= 7.0.7.2)
activerecord-pg-extensions (0.4.4)
activerecord (>= 6.0, < 7.1)
railties (>= 6.0, < 7.1)
activerecord-pg-extensions (0.5.2)
activerecord (~> 7.0.0)
railties (~> 7.0.0)
activesupport (7.0.7.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
@ -103,9 +103,9 @@ GEM
railties (>= 6.1, < 7.1)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
inst-jobs (3.1.11)
inst-jobs (3.1.12)
activerecord (>= 6.0)
activerecord-pg-extensions (~> 0.4.4)
activerecord-pg-extensions (~> 0.4)
activesupport (>= 6.0)
after_transaction_commit (>= 1.0, < 3)
debug_inspector (~> 1.0)

View File

@ -64,9 +64,9 @@ GEM
activerecord (7.0.7.2)
activemodel (= 7.0.7.2)
activesupport (= 7.0.7.2)
activerecord-pg-extensions (0.4.4)
activerecord (>= 6.0, < 7.1)
railties (>= 6.0, < 7.1)
activerecord-pg-extensions (0.5.2)
activerecord (~> 7.0.0)
railties (~> 7.0.0)
activesupport (7.0.7.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
@ -119,9 +119,9 @@ GEM
i18n (1.14.1)
concurrent-ruby (~> 1.0)
idn-ruby (0.1.5)
inst-jobs (3.1.11)
inst-jobs (3.1.12)
activerecord (>= 6.0)
activerecord-pg-extensions (~> 0.4.4)
activerecord-pg-extensions (~> 0.4)
activesupport (>= 6.0)
after_transaction_commit (>= 1.0, < 3)
debug_inspector (~> 1.0)

View File

@ -57,9 +57,9 @@ GEM
activerecord (7.0.7.2)
activemodel (= 7.0.7.2)
activesupport (= 7.0.7.2)
activerecord-pg-extensions (0.4.4)
activerecord (>= 6.0, < 7.1)
railties (>= 6.0, < 7.1)
activerecord-pg-extensions (0.5.2)
activerecord (~> 7.0.0)
railties (~> 7.0.0)
activesupport (7.0.7.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
@ -107,9 +107,9 @@ GEM
railties (>= 6.1, < 7.1)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
inst-jobs (3.1.11)
inst-jobs (3.1.12)
activerecord (>= 6.0)
activerecord-pg-extensions (~> 0.4.4)
activerecord-pg-extensions (~> 0.4)
activesupport (>= 6.0)
after_transaction_commit (>= 1.0, < 3)
debug_inspector (~> 1.0)

View File

@ -44,5 +44,7 @@ describe "CleanUpAssignmentOverrides" do
# ensure the check constraint prevents detaching AssignmentOverrides from an assignment or quiz
override3 = @assignment.assignment_overrides.create! set_type: "ADHOC"
expect { override3.update_attribute(:assignment_id, nil) }.to raise_error(ActiveRecord::StatementInvalid)
ensure
CleanUpAssignmentOverrides.up
end
end