Project

General

Profile

Defect #44366 » 0001-Delete-workflow-rules-referring-to-an-issue-custom-f.patch

Go MAEDA, 2026-08-23 08:13

View differences:

app/models/issue_custom_field.rb
24 24
  safe_attributes 'project_ids',
25 25
                  'tracker_ids'
26
  before_destroy :delete_workflow_rules
27

  
26 28
  def type_name
27 29
    :label_issue_plural
28 30
  end
31
  # Deletes workflow rules that refer to this custom field
32
  def delete_workflow_rules
33
    WorkflowRule.where(:field_name => id.to_s).delete_all
34
  end
35

  
29 36
  def visible_by?(project, user=User.current)
30 37
    super || roles.intersect?(user.roles_for_project(project))
31 38
  end
db/migrate/20260823000000_delete_orphaned_workflow_rules_of_custom_fields.rb
1
class DeleteOrphanedWorkflowRulesOfCustomFields < ActiveRecord::Migration[8.1]
2
  def up
3
    field_names = WorkflowRule.where("field_name IS NOT NULL").distinct.pluck(:field_name)
4
    custom_field_ids = field_names.select {|name| name =~ /\A\d+\z/}.map(&:to_i)
5
    orphaned_ids = custom_field_ids - CustomField.where(id: custom_field_ids).pluck(:id)
6
    if orphaned_ids.any?
7
      WorkflowRule.where(field_name: orphaned_ids.map(&:to_s)).delete_all
8
    end
9
  end
10

  
11
  def down
12
    # no-op
13
  end
14
end
test/unit/issue_custom_field_test.rb
43 43
    field.save!
44 44
    assert_equal 0, field.roles.count
45 45
  end
46

  
47
  def test_destroy_should_delete_workflow_rules
48
    field = IssueCustomField.create!(:name => 'Field', :field_format => 'string')
49
    WorkflowPermission.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :field_name => field.id.to_s, :rule => 'required')
50
    assert_equal 1, WorkflowRule.where(:field_name => field.id.to_s).count
51

  
52
    field.destroy
53
    assert_equal 0, WorkflowRule.where(:field_name => field.id.to_s).count
54
  end
46 55
end
(1-1/2)