diff --git a/app/services/user_merger.rb b/app/services/user_merger.rb index c322d5f3638..1284dd655ef 100644 --- a/app/services/user_merger.rb +++ b/app/services/user_merger.rb @@ -363,7 +363,9 @@ class UserMerger update_user_id(:user_custom_fields, conditions: "x.name = y.name") - update_user_id(:user_emails, conditions: "x.email = y.email OR y.primary = false", updates: '"primary" = false') + if @target_user.human? + update_user_id(:user_emails, conditions: "x.email = y.email OR y.primary = false", updates: '"primary" = false') + end UserExport.where(user_id: @source_user.id).update_all(user_id: @target_user.id) diff --git a/spec/services/user_merger_spec.rb b/spec/services/user_merger_spec.rb index 55996ac52f0..06d3a51931c 100644 --- a/spec/services/user_merger_spec.rb +++ b/spec/services/user_merger_spec.rb @@ -848,6 +848,15 @@ describe UserMerger do expect(UserEmail.where(user_id: source_user.id).count).to eq(0) end + it "skips merging email addresses when target user is not human" do + target_user = Discourse.system_user + merge_users!(source_user, target_user) + + emails = UserEmail.where(user_id: target_user.id).pluck(:email, :primary) + expect(emails).to contain_exactly([target_user.email, true]) + expect(UserEmail.exists?(user_id: source_user.id)).to eq(false) + end + it "updates exports" do UserExport.create(file_name: "user-archive-alice1-190218-003249", user_id: source_user.id)