From 4befae9f1e290a869d927828cff2cd71d1c7891e Mon Sep 17 00:00:00 2001 From: MAEDA Go Date: Sun, 23 Aug 2026 15:06:10 +0900 Subject: [PATCH] Delete workflow rules referring to an issue custom field when the custom field is deleted (#44366). --- app/models/issue_custom_field.rb | 7 +++++++ ...ete_orphaned_workflow_rules_of_custom_fields.rb | 14 ++++++++++++++ test/unit/issue_custom_field_test.rb | 9 +++++++++ 3 files changed, 30 insertions(+) create mode 100644 db/migrate/20260823000000_delete_orphaned_workflow_rules_of_custom_fields.rb diff --git a/app/models/issue_custom_field.rb b/app/models/issue_custom_field.rb index ae460e221..b9d067956 100644 --- a/app/models/issue_custom_field.rb +++ b/app/models/issue_custom_field.rb @@ -24,10 +24,17 @@ class IssueCustomField < CustomField safe_attributes 'project_ids', 'tracker_ids' + before_destroy :delete_workflow_rules + def type_name :label_issue_plural end + # Deletes workflow rules that refer to this custom field + def delete_workflow_rules + WorkflowRule.where(:field_name => id.to_s).delete_all + end + def visible_by?(project, user=User.current) super || roles.intersect?(user.roles_for_project(project)) end diff --git a/db/migrate/20260823000000_delete_orphaned_workflow_rules_of_custom_fields.rb b/db/migrate/20260823000000_delete_orphaned_workflow_rules_of_custom_fields.rb new file mode 100644 index 000000000..4148cd35e --- /dev/null +++ b/db/migrate/20260823000000_delete_orphaned_workflow_rules_of_custom_fields.rb @@ -0,0 +1,14 @@ +class DeleteOrphanedWorkflowRulesOfCustomFields < ActiveRecord::Migration[8.1] + def up + field_names = WorkflowRule.where("field_name IS NOT NULL").distinct.pluck(:field_name) + custom_field_ids = field_names.select {|name| name =~ /\A\d+\z/}.map(&:to_i) + orphaned_ids = custom_field_ids - CustomField.where(id: custom_field_ids).pluck(:id) + if orphaned_ids.any? + WorkflowRule.where(field_name: orphaned_ids.map(&:to_s)).delete_all + end + end + + def down + # no-op + end +end diff --git a/test/unit/issue_custom_field_test.rb b/test/unit/issue_custom_field_test.rb index d8c9c790a..dde72efd8 100644 --- a/test/unit/issue_custom_field_test.rb +++ b/test/unit/issue_custom_field_test.rb @@ -43,4 +43,13 @@ class IssueCustomFieldTest < ActiveSupport::TestCase field.save! assert_equal 0, field.roles.count end + + def test_destroy_should_delete_workflow_rules + field = IssueCustomField.create!(:name => 'Field', :field_format => 'string') + WorkflowPermission.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :field_name => field.id.to_s, :rule => 'required') + assert_equal 1, WorkflowRule.where(:field_name => field.id.to_s).count + + field.destroy + assert_equal 0, WorkflowRule.where(:field_name => field.id.to_s).count + end end -- 2.55.0