Project

General

Profile

Defect #41572 » 41572-v2.patch

Go MAEDA, 2024-10-26 07:55

View differences:

app/models/user.rb
945 945
    Issue.where(['author_id = ?', id]).update_all(['author_id = ?', substitute.id])
946 946
    Issue.where(['assigned_to_id = ?', id]).update_all('assigned_to_id = NULL')
947 947
    Journal.where(['user_id = ?', id]).update_all(['user_id = ?', substitute.id])
948
    Journal.where(['updated_by_id = ?', id]).update_all(['updated_by_id = ?', substitute.id])
948 949
    JournalDetail.
949 950
      where(["property = 'attr' AND prop_key = 'assigned_to_id' AND old_value = ?", id.to_s]).
950 951
      update_all(['old_value = ?', substitute.id.to_s])
db/migrate/20241026031710_update_orphaned_journal_updated_by_id_to_anonymous.rb
1
class UpdateOrphanedJournalUpdatedByIdToAnonymous < ActiveRecord::Migration[7.2]
2
  def up
3
    # Don't use `User.anonymous` here because it creates a new anonymous
4
    # user if one doesn't exist yet.
5
    anonymous_user_id = AnonymousUser.unscoped.pick(:id)
6
    # The absence of an anonymous user implies a fresh installation.
7
    return if anonymous_user_id.nil?
8

  
9
    Journal.joins('LEFT JOIN users ON users.id = journals.updated_by_id')
10
           .where.not(updated_by_id: nil)
11
           .where(users: { id: nil })
12
           .update_all(updated_by_id: anonymous_user_id)
13
  end
14

  
15
  def down
16
    # no-op
17
  end
18
end
test/unit/user_test.rb
308 308
  def test_destroy_should_update_journals
309 309
    issue = Issue.generate!(:project_id => 1, :author_id => 2,
310 310
                          :tracker_id => 1, :subject => 'foo')
311
    # Prepare a journal with both user_id and updated_by_id set to 2
311 312
    issue.init_journal(User.find(2), "update")
312 313
    issue.save!
314
    journal = issue.journals.first
315
    journal.update_columns(updated_by_id: 2)
313 316

  
314 317
    User.find(2).destroy
315 318
    assert_nil User.find_by_id(2)
316
    assert_equal User.anonymous, issue.journals.first.reload.user
319
    journal.reload
320
    assert_equal User.anonymous, journal.user
321
    assert_equal User.anonymous, journal.updated_by
317 322
  end
318 323

  
319 324
  def test_destroy_should_update_journal_details_old_value
(4-4/4)