Skip to content

Commit

Permalink
change procedure to add not null constraint to large scores table
Browse files Browse the repository at this point in the history
There is concern that using change_column to add a NOT NULL constraint
on a large table will lock the table for too long. This uses a
procedure that locks the table for a short period of time to add the
constraint, and then kicks off a validation that will run outside of
the lock.

closes GRADE-668

test plan:
 - The migration runs
 - Check that the NOT NULL constraint is on the table.

Change-Id: Idc11f170768cd4af9820e73950c5cfcc6a3354de
Reviewed-on: https://gerrit.instructure.com/133002
Reviewed-by: Rob Orton <[email protected]>
Reviewed-by: Shahbaz Javeed <[email protected]>
Tested-by: Jenkins
QA-Review: Jeremy Neander <[email protected]>
Product-Review: Keith T. Garner <[email protected]>
  • Loading branch information
ktgeek committed Nov 16, 2017
1 parent 8be528b commit eb9200d
Showing 1 changed file with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,22 @@

class AddNotNullConstraintToScoresCourseScore < ActiveRecord::Migration[5.0]
tag :postdeploy
disable_ddl_transaction!

def change
change_column_null :scores, :course_score, false
def up
self.connection.execute(<<SQL)
ALTER TABLE #{Score.quoted_table_name}
ADD CONSTRAINT course_score_not_null CHECK (course_score IS NOT NULL) NOT VALID;
SQL

self.connection.execute(<<SQL)
ALTER TABLE #{Score.quoted_table_name} VALIDATE CONSTRAINT course_score_not_null;
SQL
end

def down
self.connection.execute(<<SQL)
ALTER TABLE #{Score.quoted_table_name} DROP CONSTRAINT course_score_not_null;
SQL
end
end

0 comments on commit eb9200d

Please sign in to comment.