Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Update db/migrate/20240208100316_enable_required_project_custom_fields_in_all_projects.rb

jjabari-op855e1b396c9e
diff --git a/db/migrate/20240208100316_enable_required_project_custom_fields_in_all_projects.rb b/db/migrate/20240208100316_enable_required_project_custom_fields_in_all_projects.rb
index 9da66c00d58..9168d770fb8 100644
--- a/db/migrate/20240208100316_enable_required_project_custom_fields_in_all_projects.rb
+++ b/db/migrate/20240208100316_enable_required_project_custom_fields_in_all_projects.rb
@@ -1,14 +1,26 @@
class EnableRequiredProjectCustomFieldsInAllProjects < ActiveRecord::Migration[7.1]
def up
- required_project_custom_fields = ProjectCustomField.required.find_each.to_a
+ required_custom_field_ids = ProjectCustomField.required.ids
- Project.includes(:project_custom_field_project_mappings).find_each do |project|
- required_project_custom_fields.each do |pcf|
- if project.project_custom_field_project_mappings.pluck(:custom_field_id).exclude?(pcf.id)
- ProjectCustomFieldProjectMapping.create!(project_id: project.id, custom_field_id: pcf.id)
+ # Gather the custom_field_ids for every project, then add a new mapping
+ # of {project_id:, custom_field_id:} for every project that does not have
+ # the required required_custom_field_ids activated.
+ missing_custom_field_attributes =
+ Project
+ .includes(:project_custom_field_project_mappings)
+ .pluck(:id, 'project_custom_field_project_mappings.custom_field_id')
+ .group_by(&:first)
+ .transform_values { |values| values.map(&:last) }
+ .reduce([]) do |acc, (project_id, custom_field_ids)|
+
+ missing_custom_field_ids = required_custom_field_ids - custom_field_ids
+
+ acc + missing_custom_field_ids.map do |custom_field_id|
+ { project_id: , custom_field_id: }
end
end
- end
+
+ ProjectCustomFieldProjectMapping.insert_all!(missing_custom_field_attributes)
end
def down