Project

General

Profile

Defect #41572 » 41572-v2.patch

Go MAEDA, 2024-10-26 07:55

View differences:

app/models/user.rb
Issue.where(['author_id = ?', id]).update_all(['author_id = ?', substitute.id])
Issue.where(['assigned_to_id = ?', id]).update_all('assigned_to_id = NULL')
Journal.where(['user_id = ?', id]).update_all(['user_id = ?', substitute.id])
Journal.where(['updated_by_id = ?', id]).update_all(['updated_by_id = ?', substitute.id])
JournalDetail.
where(["property = 'attr' AND prop_key = 'assigned_to_id' AND old_value = ?", id.to_s]).
update_all(['old_value = ?', substitute.id.to_s])
db/migrate/20241026031710_update_orphaned_journal_updated_by_id_to_anonymous.rb
class UpdateOrphanedJournalUpdatedByIdToAnonymous < ActiveRecord::Migration[7.2]
def up
# Don't use `User.anonymous` here because it creates a new anonymous
# user if one doesn't exist yet.
anonymous_user_id = AnonymousUser.unscoped.pick(:id)
# The absence of an anonymous user implies a fresh installation.
return if anonymous_user_id.nil?
Journal.joins('LEFT JOIN users ON users.id = journals.updated_by_id')
.where.not(updated_by_id: nil)
.where(users: { id: nil })
.update_all(updated_by_id: anonymous_user_id)
end
def down
# no-op
end
end
test/unit/user_test.rb
def test_destroy_should_update_journals
issue = Issue.generate!(:project_id => 1, :author_id => 2,
:tracker_id => 1, :subject => 'foo')
# Prepare a journal with both user_id and updated_by_id set to 2
issue.init_journal(User.find(2), "update")
issue.save!
journal = issue.journals.first
journal.update_columns(updated_by_id: 2)
User.find(2).destroy
assert_nil User.find_by_id(2)
assert_equal User.anonymous, issue.journals.first.reload.user
journal.reload
assert_equal User.anonymous, journal.user
assert_equal User.anonymous, journal.updated_by
end
def test_destroy_should_update_journal_details_old_value
(4-4/4)