diff --git a/app/models/user.rb b/app/models/user.rb index e27b0be18..3081b0f95 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -214,6 +214,7 @@ class User < ApplicationRecord end def merge(old_user) + raise "Merging same user, no way" if old_user.id == self.id transaction do old_user.dossiers.update_all(user_id: id) old_user.invites.update_all(user_id: id) diff --git a/db/migrate/20221013142432_clean_pending_user_with_requested_merge_into_id_similar_to_their_id.rb b/db/migrate/20221013142432_clean_pending_user_with_requested_merge_into_id_similar_to_their_id.rb new file mode 100644 index 000000000..597442bd2 --- /dev/null +++ b/db/migrate/20221013142432_clean_pending_user_with_requested_merge_into_id_similar_to_their_id.rb @@ -0,0 +1,5 @@ +class CleanPendingUserWithRequestedMergeIntoIdSimilarToTheirId < ActiveRecord::Migration[6.1] + def change + User.where('users.id = users.requested_merge_into_id').update_all(requested_merge_into_id: nil) + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index a9715c397..75447ae94 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -423,7 +423,11 @@ describe User, type: :model do let(:targeted_user) { create(:user) } subject { targeted_user.merge(old_user) } - + context 'merge myself' do + it 'fails' do + expect { old_user.merge(old_user) }.to raise_error 'Merging same user, no way' + end + end context 'and the old account has some stuff' do let!(:dossier) { create(:dossier, user: old_user) } let!(:hidden_dossier) { create(:dossier, user: old_user, hidden_by_user_at: 1.hour.ago) }