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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[#55898] Add uniqueness index to project_custom_field_project_mappings (#15964)
judithroth6655403ca4dd
diff --git a/app/services/project_custom_field_project_mappings/bulk_create_service.rb b/app/services/project_custom_field_project_mappings/bulk_create_service.rbindex 356c2c3594c..1ea84387f1c 100644--- a/app/services/project_custom_field_project_mappings/bulk_create_service.rb+++ b/app/services/project_custom_field_project_mappings/bulk_create_service.rb@@ -75,7 +75,8 @@ module ProjectCustomFieldProjectMappingsdef perform_bulk_create(service_call)ProjectCustomFieldProjectMapping.insert_all(- service_call.result.map { |model| model.attributes.slice("project_id", "custom_field_id") }+ service_call.result.map { |model| model.attributes.slice("project_id", "custom_field_id") },+ unique_by: %i[project_id custom_field_id])service_calldiff --git a/app/services/project_custom_field_project_mappings/bulk_update_service.rb b/app/services/project_custom_field_project_mappings/bulk_update_service.rbindex 8dd4b69f044..3957134f5a6 100644--- a/app/services/project_custom_field_project_mappings/bulk_update_service.rb+++ b/app/services/project_custom_field_project_mappings/bulk_update_service.rb@@ -101,7 +101,10 @@ module ProjectCustomFieldProjectMappingsnew_mappings = custom_field_ids.map do |id|{ project_id: @project.id, custom_field_id: id }end- ProjectCustomFieldProjectMapping.insert_all(new_mappings)+ ProjectCustomFieldProjectMapping.insert_all(+ new_mappings,+ unique_by: %i[project_id custom_field_id]+ )endendenddiff --git a/db/migrate/20240625075139_add_uniqueness_index_to_project_custom_field_project_mappings.rb b/db/migrate/20240625075139_add_uniqueness_index_to_project_custom_field_project_mappings.rbnew file mode 100644index 00000000000..c5b47e67fb0--- /dev/null+++ b/db/migrate/20240625075139_add_uniqueness_index_to_project_custom_field_project_mappings.rb@@ -0,0 +1,19 @@+class AddUniquenessIndexToProjectCustomFieldProjectMappings < ActiveRecord::Migration[7.1]+ def up+ execute <<~SQL.squish+ DELETE FROM project_custom_field_project_mappings AS pcfpm_1+ USING project_custom_field_project_mappings AS pcfpm_2+ WHERE pcfpm_1.project_id = pcfpm_2.project_id+ AND pcfpm_1.custom_field_id = pcfpm_2.custom_field_id+ AND pcfpm_1.id > pcfpm_2.id;+ SQL++ add_index :project_custom_field_project_mappings, %i[project_id custom_field_id],+ unique: true,+ name: "index_project_custom_field_project_mappings_unique"+ end++ def down+ remove_index :project_custom_field_project_mappings, name: "index_project_custom_field_project_mappings_unique"+ end+enddiff --git a/spec/services/project_custom_field_project_mappings/bulk_create_service_spec.rb b/spec/services/project_custom_field_project_mappings/bulk_create_service_spec.rbindex 8bb137c4ae6..64343f2c399 100644--- a/spec/services/project_custom_field_project_mappings/bulk_create_service_spec.rb+++ b/spec/services/project_custom_field_project_mappings/bulk_create_service_spec.rb@@ -82,6 +82,20 @@ RSpec.describe ProjectCustomFieldProjectMappings::BulkCreateService doendendend++ context "with duplicates" do+ let(:project) { create(:project) }+ let(:instance) { described_class.new(user:, projects: [project, project], project_custom_field:) }++ it "creates the mappings only once" do+ expect { instance.call }.to change(ProjectCustomFieldProjectMapping, :count).by(1)++ aggregate_failures "creates the mapping for the correct project and custom field" do+ expect(ProjectCustomFieldProjectMapping.last.project).to eq(project)+ expect(ProjectCustomFieldProjectMapping.last.project_custom_field).to eq(project_custom_field)+ end+ end+ endendcontext "with non-admin but sufficient permissions" do