Skip to content

Commit e1608be

Browse files
committed
Bugfix: email reassignment failure when deleting a person
If the person to be deleted had existing posts, updates happened in the wrong order, causing foreign key violations.
1 parent 9afd0f4 commit e1608be

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

app/models/person.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,19 @@ def attach_alias!(alias_record, user: nil)
9898
def cleanup_orphaned_person(person)
9999
return if person.user.present?
100100
return if Alias.where(person_id: person.id).exists?
101+
reassign_authored_records(person)
101102
person.destroy!
102103
end
103104

105+
def reassign_authored_records(old_person)
106+
Topic.where(creator_person_id: old_person.id).update_all(creator_person_id: id)
107+
Topic.where(last_sender_person_id: old_person.id).update_all(last_sender_person_id: id)
108+
Message.where(sender_person_id: old_person.id).update_all(sender_person_id: id)
109+
Mention.where(person_id: old_person.id).update_all(person_id: id)
110+
TopicParticipant.where(person_id: old_person.id).where.not(topic_id: TopicParticipant.where(person_id: id).select(:topic_id)).update_all(person_id: id)
111+
TopicParticipant.where(person_id: old_person.id).delete_all
112+
end
113+
104114
def merge_contributor_memberships_from(other_person)
105115
other_person.contributor_memberships.find_each do |membership|
106116
ContributorMembership.find_or_create_by!(person_id: id, contributor_type: membership.contributor_type) do |record|

0 commit comments

Comments
 (0)