From 8a9470aebf0b784f1d5fc3470a7c1e2840805430 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). Also adds a migration that deletes workflow rules already orphaned by custom fields deleted before this fix. Co-Authored-By: Claude Fable 5 --- 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/20260827002435_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..146f0925f 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 + WorkflowPermission.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/20260827002435_delete_orphaned_workflow_rules_of_custom_fields.rb b/db/migrate/20260827002435_delete_orphaned_workflow_rules_of_custom_fields.rb new file mode 100644 index 000000000..9d99ae7d9 --- /dev/null +++ b/db/migrate/20260827002435_delete_orphaned_workflow_rules_of_custom_fields.rb @@ -0,0 +1,14 @@ +class DeleteOrphanedWorkflowRulesOfCustomFields < ActiveRecord::Migration[8.1] + def up + field_names = WorkflowPermission.where.not(field_name: nil).distinct.pluck(:field_name) + custom_field_ids = field_names.grep(/\A\d+\z/).map(&:to_i) + orphaned_ids = custom_field_ids - CustomField.where(id: custom_field_ids).pluck(:id) + if orphaned_ids.any? + WorkflowPermission.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..b1b7ea752 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, WorkflowPermission.where(:field_name => field.id.to_s).count + + field.destroy + assert_equal 0, WorkflowPermission.where(:field_name => field.id.to_s).count + end end -- 2.55.0