Set up replica identity index for AccessToken

refs FOO-1171
flag=none

also don't backfill nulls if the column is already non-nullable

test plan:
 - migration works up and down
 - tests pass

Change-Id: I5009a2f31249643738f69a6b0d765daf89276165
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/260519
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
Reviewed-by: Simon Williams <simon@instructure.com>
QA-Review: Simon Williams <simon@instructure.com>
Product-Review: Simon Williams <simon@instructure.com>
This commit is contained in:
Michael Ziwisky 2021-03-11 22:25:12 -08:00
parent c5988b8cc5
commit ca41fd8a4f
3 changed files with 46 additions and 31 deletions

View File

@ -1511,9 +1511,16 @@ ActiveRecord::ConnectionAdapters::SchemaStatements.class_eval do
execute schema_creation.accept(at)
end
def add_replica_identity(table_string, column_name, default_value)
klass = table_string.constantize
DataFixup::BackfillNulls.run(klass, column_name, default_value: default_value)
def add_replica_identity(model_name, column_name, default_value)
klass = model_name.constantize
# when a batch of migrations includes both adding the `column_name` and
# calling this method (e.g. when migrating a newly created db), the cached
# column information can be stale, so we have to explicitly reset it here
# to avoid a NoMethodError
klass.reset_column_information
if klass.columns_hash[column_name.to_s].null
DataFixup::BackfillNulls.run(klass, column_name, default_value: default_value)
end
change_column_null klass.table_name, column_name, false
primary_column = klass.primary_key
index_name = "index_#{klass.table_name}_replica_identity"
@ -1521,8 +1528,8 @@ ActiveRecord::ConnectionAdapters::SchemaStatements.class_eval do
execute(%[ALTER TABLE #{klass.quoted_table_name} REPLICA IDENTITY USING INDEX #{index_name}])
end
def remove_replica_identity(table_string)
klass = table_string.constantize
def remove_replica_identity(model_name)
klass = model_name.constantize
execute(%[ALTER TABLE #{klass.quoted_table_name} REPLICA IDENTITY DEFAULT])
remove_index klass.table_name, name: "index_#{klass.table_name}_replica_identity", if_exists: true
end
@ -1814,4 +1821,4 @@ module MaxRuntimeConnectionPool
old_connections.each(&:disconnect!)
end
end
ActiveRecord::ConnectionAdapters::ConnectionPool.prepend(MaxRuntimeConnectionPool)
ActiveRecord::ConnectionAdapters::ConnectionPool.prepend(MaxRuntimeConnectionPool)

View File

@ -0,0 +1,33 @@
# 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 AddReplicaIdentityForAccessTokens < ActiveRecord::Migration[6.0]
tag :postdeploy
disable_ddl_transaction!
def up
add_replica_identity 'AccessToken', :root_account_id, 0
remove_index :access_tokens, column: :root_account_id, if_exists: true
end
def down
add_index :access_tokens, :root_account_id, algorithm: :concurrently, if_not_exists: true
remove_replica_identity 'AccessToken'
change_column_null :access_tokens, :root_account_id, true
end
end

View File

@ -80,31 +80,6 @@ describe DataFixup::PopulateRootAccountIdOnModels do
end
end
context 'with AccessToken' do
it_behaves_like 'a datafixup that populates root_account_id' do
let(:record) do
dk = DeveloperKey.create!(account: @course.account)
at = dk.access_tokens.create!(user: user_model)
end
let(:reference_record) { @course }
end
context 'with_sharding' do
specs_require_sharding
context 'with DeveloperKey with root_account_id' do
it_behaves_like 'a datafixup that populates root_account_id' do
let(:account) { @shard1.activate { account_model } }
let(:record) do
dk = DeveloperKey.create!(account: account)
at = dk.access_tokens.create!(user: user_model)
end
let(:reference_record) { account }
end
end
end
end
it 'should populate root_account_id on AssessmentQuestion' do
aq = assessment_question_model(bank: AssessmentQuestionBank.create!(context: @course))
aq.update_columns(root_account_id: nil)