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

chore[Op#57516]: remove unnecessary manual backfilling

Kabiru Mwenja922bd158ef38
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
index 40a37ec0b2b..0e07746b175 100644
--- a/db/migrate/20240917105829_add_primary_key_to_custom_fields_projects.rb
+++ b/db/migrate/20240917105829_add_primary_key_to_custom_fields_projects.rb
@@ -28,23 +28,8 @@
class AddPrimaryKeyToCustomFieldsProjects < ActiveRecord::Migration[7.1]
def change
+ # NOTE: Manually backfilling the `id` column is not necessary as the BIGSERIAL primary key will auto-populate
+ # the existing records with auto-incrementing values
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/migrations/add_primary_key_to_custom_fields_projects_spec.rb b/spec/migrations/add_primary_key_to_custom_fields_projects_spec.rb
new file mode 100644
index 00000000000..62411f854e9
--- /dev/null
+++ b/spec/migrations/add_primary_key_to_custom_fields_projects_spec.rb
@@ -0,0 +1,59 @@
+#-- 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.
+#++
+
+require "spec_helper"
+require Rails.root.join("db/migrate/20240917105829_add_primary_key_to_custom_fields_projects.rb")
+
+RSpec.describe AddPrimaryKeyToCustomFieldsProjects, type: :model do
+ shared_association_default(:project) { create(:project) }
+
+ it "adds an `id` primary key column with backfilled values" do
+ ActiveRecord::Migration.suppress_messages { described_class.migrate(:down) }
+
+ create_list(:custom_fields_project, 5)
+
+ aggregate_failures "no primary key column" do
+ expect(CustomFieldsProject.column_names).not_to include("id")
+ expect(CustomFieldsProject.count).to eq 5
+ end
+
+ ActiveRecord::Migration.suppress_messages { described_class.migrate(:up) }
+ CustomFieldsProject.reset_column_information
+ CustomFieldsProject.reset_primary_key
+
+ aggregate_failures "primary key column added" do
+ expect(CustomFieldsProject.column_names).to include("id")
+ expect(CustomFieldsProject.last.id).to eq 5
+ end
+
+ aggregate_failures "next record increments the primary key" do
+ expect { create(:custom_fields_project) }.to change(CustomFieldsProject, :count).by(1)
+ expect(CustomFieldsProject.last.id).to eq 6
+ end
+ end
+end