fix fk error on user split

fixes CNVS-39625

test plan
 - merge cross shard users
 - do stuff that creates notificaiton poilicy for the new
   communication_channel
 - split users
 - it should work

Change-Id: I2082ee0de1d9407b5dd0a5100d5052ab437b2ea7
Reviewed-on: https://gerrit.instructure.com/128008
Tested-by: Jenkins
Reviewed-by: Cody Cutrer <cody@instructure.com>
Product-Review: Rob Orton <rob@instructure.com>
QA-Review: Rob Orton <rob@instructure.com>
This commit is contained in:
Rob Orton 2017-09-30 12:38:33 -06:00
parent 7705466730
commit fa27d91c54
2 changed files with 13 additions and 2 deletions

View File

@ -98,6 +98,7 @@ class SplitUsers
private
def split_users(user, merge_data)
# user is the active user that was the destination of the user merge
user.shard.activate do
ActiveRecord::Base.transaction do
records = merge_data.user_merge_data_records
@ -115,6 +116,8 @@ class SplitUsers
end
end
# source_user is the destination user of the user merge
# user is the old user that is being restored
def move_records_to_old_user(source_user, user, records)
fix_communication_channels(source_user, user, records.where(context_type: 'CommunicationChannel'))
move_user_observers(source_user, user, records.where(context_type: 'UserObserver', previous_user_id: user))
@ -140,11 +143,16 @@ class SplitUsers
restore_worklow_states_from_records(records)
end
# source_user is the destination user of the user merge
# user is the old user that is being restored
def fix_communication_channels(source_user, user, cc_records)
if source_user.shard != user.shard
user.shard.activate do
source_user.shard.activate do
# remove communication channels that didn't exist prior to the merge
CommunicationChannel.where(id: cc_records.where(previous_workflow_state: 'non_existent').pluck(:context_id)).delete_all
ccs = CommunicationChannel.where(id: cc_records.where(previous_workflow_state: 'non_existent').pluck(:context_id))
DelayedMessage.where(communication_channel_id: ccs).delete_all
NotificationPolicy.where(communication_channel: ccs).delete_all
ccs.delete_all
end
end
# move moved communication channels back

View File

@ -393,6 +393,9 @@ describe SplitUsers do
@shard1.activate do
UserMerge.from(user1).into(@user2)
cc = @user2.reload.communication_channels.where(path: 'user1@example.com').take
n = Notification.create!(name: 'Assignment Createds', subject: 'Tests', category: 'TestNevers')
NotificationPolicy.create(notification: n, communication_channel: cc, frequency: 'immediately')
SplitUsers.split_db_users(@user2)
end