Remove foreign key constraints from work_item_type_id columns
What does this MR do and why?
To allow storing both system-defined work item type IDs (1-9) and custom work item type IDs (1000+) in the same work_item_type_id column, we need to remove foreign key constraints from work_item_type_id columns.
Changes
Database changes
This MR removes foreign key constraints from the following tables using post-deployment migrations:
issueswork_item_type_custom_fieldswork_item_custom_status_mappingswork_item_type_custom_lifecycleswork_item_type_user_preferences
Application-level validation
To maintain data integrity after removing foreign key constraints, this MR adds centralized application-level validation:
- Creates
WorkItems::TypesFramework::Validationconcern that provides consistent validation across all affected models. - Uses
WorkItems::TypesFramework::Providerpattern for centralized work item type validation logic. - Ensure validation runs regardless of
work_item_system_defined_typefeature flag state, preventing invalidwork_item_type_idvalues during the transition period.
This ensures that invalid work item type references are caught at the application level, replacing the database-level foreign key constraint protection while providing stronger data integrity guarantees.
Safety measures during transition
- Adds
prevent_deletion_during_transitioncallback toWorkItems::Typemodel that prevents deletion of work item types when thework_item_system_defined_typefeature flag is disabled. - This serves as a safety net against accidental deletions via Rails console during the transition period, preventing orphaned records that would reference non-existent work item types.
Considerations
- This MR can only be merged after the
work_item_system_defined_typefeature flag has been enabled and tested. Keeping it in draft for now. - Once the FF flag is enabled, we will be removing the legacy work item types stored in the DB.
- The foreign key also needs to be dropped from
work_item_widget_definitions. Since thework_item_widget_definitionstable will be deleted once thework_item_system_defined_typefeature flag is enabled (likely within the next couple of weeks), we decided it isn't necessary to remove the constraint now, as it will be removed together with the table itself. - Deletion of custom work item types that reference existing records will be handled by the custom FK validation changes from !223997 (merged).
References
How to set up and validate locally
- Enable the
work_item_system_defined_typeFF. -
issues- Navigate to a project and create a new work item, for example an issue or a task. Creating work items should behave exactly the same as before.
- Via the Rails console, verify the work item type ID of the newly created work item. The work item type should reference a system-defined one.
WorkItem.last.work_item_typeIssue.last.work_item_type
-
work_item_type_custom_lifecycles- Navigate to the work item settings at the root group, for example:
http://127.0.0.1:3000/groups/public-group-1/-/settings/work_items. - Create a new work item status lifecycle.
- Apply the lifecycle to a work item type, such as 'Issue' or 'Task'.
- Via the Rails console, verify the work item type associated with the lifecycle. It should reference a system-defined one.
WorkItems::Statuses::Custom::Lifecycle.last.work_item_types.lastWorkItems::TypeCustomLifecycle.last.work_item_type
- Navigate to the work item settings at the root group, for example:
-
work_item_custom_status_mappings- Remove one of the statuses within the lifecycle, for example 'In progress', and provide a status mapping.
- Via the Rails console, verify the work item type. It should reference a system-defined one.
WorkItems::Statuses::Custom::Mapping.last.work_item_type
-
work_item_type_custom_fields- Navigate to the work item settings at the root group, for example:
http://127.0.0.1:3000/groups/public-group-1/-/settings/work_items. - Create a new custom field.
- Via the Rails console, verify the work item type. It should reference a system-defined one.
WorkItems::TypeCustomField.last.work_item_type
- Navigate to the work item settings at the root group, for example:
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.