Project

General

Profile

Defect #32977 » 0001-removes-references-to-deleted-users-in-custom-field-.patch

updated patch including migration - Jens Krämer, 2020-02-11 06:29

View differences:

app/models/user.rb
916 916
    Watcher.where('user_id = ?', id).delete_all
917 917
    WikiContent.where(['author_id = ?', id]).update_all(['author_id = ?', substitute.id])
918 918
    WikiContent::Version.where(['author_id = ?', id]).update_all(['author_id = ?', substitute.id])
919
    user_custom_field_ids = CustomField.where(field_format: 'user').pluck(:id)
920
    if user_custom_field_ids.any?
921
      CustomValue.where(custom_field_id: user_custom_field_ids, value: self.id.to_s).delete_all
922
    end
919 923
  end
920 924

  
921 925
  # Singleton class method is public
db/migrate/20200211051351_remove_orphaned_user_custom_values.rb
1
class RemoveOrphanedUserCustomValues < ActiveRecord::Migration[5.2]
2
  def up
3
    user_custom_field_ids = CustomField.where(field_format: 'user').pluck(:id)
4
    if user_custom_field_ids.any?
5
      user_ids = Principal.pluck(:id)
6
      CustomValue.
7
        where(custom_field_id: user_custom_field_ids).
8
        where.not(value: [nil, ''] + user_ids).
9
        delete_all
10
    end
11
  end
12
end
test/unit/user_test.rb
1295 1295
    assert_equal [], User.find(2).bookmarked_project_ids
1296 1296
  end
1297 1297

  
1298
  def test_remove_custom_field_references_upon_destroy
1299
    cf1 = IssueCustomField.create(field_format: 'user', name: 'user cf', is_for_all: true, tracker_ids: Tracker.pluck(:id))
1300
    cf2 = IssueCustomField.create(field_format: 'user', name: 'users cf', is_for_all: true, multiple: true, tracker_ids: Tracker.pluck(:id))
1301

  
1302
    issue = Issue.first
1303
    issue.init_journal(@admin)
1304
    assert_difference ->{cf1.custom_values.count} do
1305
      assert_difference ->{cf2.custom_values.count}, 2 do
1306
        issue.update_attributes(custom_field_values: {
1307
          cf1.id => @jsmith.id,
1308
          cf2.id => [@dlopper.id, @jsmith.id]
1309
        })
1310
      end
1311
    end
1312
    assert cv1 = cf1.custom_values.where(customized_id: issue.id).last
1313
    assert_equal @jsmith.id.to_s, cv1.value
1314

  
1315
    assert cv2 = cf2.custom_values.where(customized_id: issue.id)
1316
    assert_equal 2, cv2.size
1317
    assert cv2a = cv2.detect{|cv|cv.value == @dlopper.id.to_s}
1318
    assert cv2b = cv2.detect{|cv|cv.value == @jsmith.id.to_s}
1319

  
1320
    assert_difference ->{CustomValue.count}, -2 do
1321
      @jsmith.destroy
1322
    end
1323

  
1324
    assert_raise(ActiveRecord::RecordNotFound){cv1.reload}
1325
    assert_raise(ActiveRecord::RecordNotFound){cv2b.reload}
1326

  
1327
    cv2a.reload
1328
    assert_equal @dlopper.id.to_s, cv2a.value
1329
  end
1330

  
1298 1331
  if Object.const_defined?(:OpenID)
1299 1332
    def test_setting_identity_url
1300 1333
      normalized_open_id_url = 'http://example.com/'
(2-2/2)