support both old and new postgres versions for new submission column

this migration was originally written with a backfill, but the
submissions table was large enough that the backfill was very slow. so
we changed to create the column with a default, taking advantage of
PG11+ ability to do this quickly rather than lock the table. But some
operators have not upgraded postgres versions yet, so let's support both
methods for now, while we work out a clear upgrade timeline.

Change-Id: I7e745c16ba162ab2740e9387e3138a7fca74df8c
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/261779
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
Reviewed-by: Cody Cutrer <cody@instructure.com>
QA-Review: Cody Cutrer <cody@instructure.com>
Product-Review: Cody Cutrer <cody@instructure.com>
This commit is contained in:
Simon Williams 2021-03-29 13:17:27 -05:00
parent aff62d5264
commit 6e877868be
2 changed files with 47 additions and 3 deletions

View File

@ -21,11 +21,17 @@ class AddRedoRequestToSubmissions < ActiveRecord::Migration[5.2]
tag :predeploy
def up
remove_column :submissions, :redo_request, if_exists: true
add_column :submissions, :redo_request, :boolean, default: false, null: false
if connection.postgresql_version >= 110000
remove_column :submissions, :redo_request, if_exists: true
add_column :submissions, :redo_request, :boolean, default: false, null: false
else
# backfill and default will come in a postdeploy
add_column :submissions, :redo_request, :boolean
change_column_default(:submissions, :redo_request, false)
end
end
def down
change_column_null(:submissions, :redo_request, true)
remove_column :submissions, :redo_request, if_exists: true
end
end

View File

@ -0,0 +1,38 @@
# frozen_string_literal: true
#
# Copyright (C) 2021 - 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 BackfillSubmissionsRedoRequest < ActiveRecord::Migration[5.2]
tag :postdeploy
disable_ddl_transaction!
def self.runnable?
connection.postgresql_version < 110000
end
def up
if Submission.columns.detect{|c| c.name == "redo_request"}&.null
DataFixup::BackfillNulls.run(Submission, :redo_request, default_value: false)
change_column_null(:submissions, :redo_request, false)
end
end
def down
change_column_null(:submissions, :redo_request, true)
end
end