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

fix[Op#57516]: add missing primary key to custom fields projects

Kabiru Mwenjac8ccf131cc21
diff --git a/app/services/custom_fields/custom_field_projects/delete_service.rb b/app/services/custom_fields/custom_field_projects/delete_service.rb
index 66223d8f80c..41069ffa527 100644
--- a/app/services/custom_fields/custom_field_projects/delete_service.rb
+++ b/app/services/custom_fields/custom_field_projects/delete_service.rb
@@ -29,22 +29,6 @@
module CustomFields
module CustomFieldProjects
class DeleteService < ::BaseServices::Delete
- def destroy(custom_field_project)
- delete_result = delete(custom_field_id: custom_field_project.custom_field_id,
- project_id: custom_field_project.project_id)
- ActiveRecord::Type::Boolean.new.cast(delete_result)
- end
-
- # `custom_fields_projects` table has no `id` column, hence no primary key. #destroy method would not work
- # Note: `delete_all` goes straight to the database and does not trigger callbacks
- #
- # @return [Integer] number of rows deleted
- def delete(custom_field_id:, project_id:)
- CustomFieldsProject.transaction do
- CustomFieldsProject.where(custom_field_id:, project_id:).delete_all
- end
- end
-
# Mappings have custom deletion rules that are similar to the update rules all derived from the base contract
# Reuse the update contract to ensure that the deletion rules are consistent with the update rules
def default_contract_class = CustomFields::CustomFieldProjects::UpdateContract
diff --git a/db/migrate/20240917105829_add_primary_key_to_custom_fields_projects.rb b/db/migrate/20240917105829_add_primary_key_to_custom_fields_projects.rb
new file mode 100644
index 00000000000..40a37ec0b2b
--- /dev/null
+++ b/db/migrate/20240917105829_add_primary_key_to_custom_fields_projects.rb
@@ -0,0 +1,50 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+class AddPrimaryKeyToCustomFieldsProjects < ActiveRecord::Migration[7.1]
+ def change
+ add_column :custom_fields_projects, :id, :primary_key # rubocop:disable Rails/DangerousColumnNames
+
+ reversible do |dir|
+ dir.up do
+ # Backfill the id column for existing rows
+ execute <<-SQL.squish
+ WITH cte AS (
+ SELECT row_number() OVER () AS row_num, project_id, custom_field_id
+ FROM custom_fields_projects
+ )
+ UPDATE custom_fields_projects
+ SET id = cte.row_num
+ FROM cte
+ WHERE custom_fields_projects.project_id = cte.project_id
+ AND custom_fields_projects.custom_field_id = cte.custom_field_id;
+ SQL
+ end
+ end
+ end
+end
diff --git a/spec/services/custom_fields/custom_field_projects/delete_service_spec.rb b/spec/services/custom_fields/custom_field_projects/delete_service_spec.rb
index 42504c4391b..a3e68133bc9 100644
--- a/spec/services/custom_fields/custom_field_projects/delete_service_spec.rb
+++ b/spec/services/custom_fields/custom_field_projects/delete_service_spec.rb
@@ -36,20 +36,5 @@ RSpec.describe CustomFields::CustomFieldProjects::DeleteService do
let(:contract_class) do
"#{namespace}::UpdateContract".constantize
end
-
- before do
- allow(CustomFieldsProject).to receive(:where).and_return(instance_double(ActiveRecord::Relation,
- delete_all: model_destroy_result))
- end
-
- context "with transaction rollback" do
- before do
- allow(CustomFieldsProject).to receive(:where).and_raise(ActiveRecord::Rollback)
- end
-
- it "is unsuccessful" do
- expect(subject).to be_failure
- end
- end
end
end