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
[42388] Migrate scheduling mode and lags
Christophe Bliard06f596cdf305
diff --git a/app/workers/work_packages/automatic_mode/migrate_values_job.rb b/app/workers/work_packages/automatic_mode/migrate_values_job.rbnew file mode 100644index 00000000000..88765340935--- /dev/null+++ b/app/workers/work_packages/automatic_mode/migrate_values_job.rb@@ -0,0 +1,109 @@+#-- 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 WorkPackages::AutomaticMode::MigrateValuesJob < ApplicationJob+ def perform+ with_temporary_table do+ change_scheduling_mode_to_manual_mode+ copy_values_to_work_packages_and_update_journals+ end+ end++ private++ def with_temporary_table+ WorkPackage.transaction do+ create_temporary_table+ yield+ ensure+ drop_temporary_table+ end+ end++ def create_temporary_table+ execute(<<~SQL.squish)+ CREATE UNLOGGED TABLE temp_wp_values+ AS SELECT+ id,+ start_date,+ due_date,+ schedule_manually+ FROM work_packages+ SQL+ end++ def drop_temporary_table+ execute(<<~SQL.squish)+ DROP TABLE temp_wp_values+ SQL+ end++ def change_scheduling_mode_to_manual_mode+ execute(<<~SQL.squish)+ UPDATE temp_wp_values+ SET schedule_manually = true+ SQL+ end++ def copy_values_to_work_packages_and_update_journals+ updated_work_package_ids = copy_values_to_work_packages+ create_journals_for_updated_work_packages(updated_work_package_ids)+ end++ def copy_values_to_work_packages+ results = execute(<<~SQL.squish)+ UPDATE work_packages+ SET schedule_manually = temp_wp_values.schedule_manually,+ lock_version = lock_version + 1,+ updated_at = NOW()+ FROM temp_wp_values+ WHERE work_packages.id = temp_wp_values.id+ AND work_packages.schedule_manually IS DISTINCT FROM temp_wp_values.schedule_manually+ RETURNING work_packages.id+ SQL+ results.column_values(0)+ end++ def create_journals_for_updated_work_packages(updated_work_package_ids)+ cause = { type: "system_update", feature: "scheduling_mode_adjusted" }+ WorkPackage.where(id: updated_work_package_ids).find_each do |work_package|+ Journals::CreateService+ .new(work_package, system_user)+ .call(cause:)+ end+ end++ # Executes an sql statement, shorter.+ def execute(sql)+ ActiveRecord::Base.connection.execute(sql)+ end++ def system_user+ @system_user ||= User.system+ end+enddiff --git a/config/locales/en.yml b/config/locales/en.ymlindex dbeeaa34aa2..2b73395fd52 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -2207,6 +2207,8 @@ en:Progress calculation automatically <a href="%{href}" target="_blank">set to work-based mode and adjusted with version update</a>.progress_calculation_adjusted: >-Progress calculation automatically <a href="%{href}" target="_blank">adjusted with version update</a>.+ scheduling_mode_adjusted: >-+ Scheduling mode automatically adjusted with version update.totals_removed_from_childless_work_packages: >-Work and progress totals automatically removed for non-parent work packages with <a href="%{href}" target="_blank">version update</a>.This is a maintenance task and can be safely ignored.diff --git a/db/migrate/20241120095318_update_scheduling_mode_and_lags.rb b/db/migrate/20241120095318_update_scheduling_mode_and_lags.rbnew file mode 100644index 00000000000..bd3d0ae9453--- /dev/null+++ b/db/migrate/20241120095318_update_scheduling_mode_and_lags.rb@@ -0,0 +1,10 @@+class UpdateSchedulingModeAndLags < ActiveRecord::Migration[7.1]+ def up+ migration_job = WorkPackages::AutomaticMode::MigrateValuesJob+ if Rails.env.development?+ migration_job.perform_now+ else+ migration_job.perform_later+ end+ end+enddiff --git a/spec/migrations/update_scheduling_mode_and_lags_spec.rb b/spec/migrations/update_scheduling_mode_and_lags_spec.rbnew file mode 100644index 00000000000..89da634d80b--- /dev/null+++ b/spec/migrations/update_scheduling_mode_and_lags_spec.rb@@ -0,0 +1,117 @@+#-- 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/20241120095318_update_scheduling_mode_and_lags.rb")++RSpec.describe UpdateSchedulingModeAndLags, type: :model do+ # Silencing migration logs, since we are not interested in that during testing+ subject(:run_migration) do+ perform_enqueued_jobs do+ ActiveRecord::Migration.suppress_messages { described_class.new.up }+ end+ end++ shared_let(:author) { create(:user) }+ shared_let(:priority) { create(:priority, name: "Normal") }+ shared_let(:project) { create(:project, name: "Main project") }+ shared_let(:status_new) { create(:status, name: "New") }++ before_all do+ set_factory_default(:user, author)+ set_factory_default(:priority, priority)+ set_factory_default(:project, project)+ set_factory_default(:project_with_types, project)+ set_factory_default(:status, status_new)+ end++ describe "journal creation" do+ context "when scheduling mode is changed by the migration" do+ let_work_packages(<<~TABLE)+ subject | schedule manually+ wp already manual | true+ wp automatic | false+ TABLE++ before do+ run_migration+ end++ it "creates a journal entry only for the changed work packages" do+ expect(wp_already_manual.journals.count).to eq(1)+ expect(wp_automatic.journals.count).to eq(2)++ expect(wp_automatic.last_journal.get_changes)+ .to include("schedule_manually" => [false, true],+ "cause" => [nil, { "feature" => "scheduling_mode_adjusted", "type" => "system_update" }])++ aggregate_failures "the journal author is the system user" do+ journal = wp_automatic.last_journal+ expect(journal.user).to eq(User.system)+ end++ aggregate_failures "the lock_version of the work package is incremented" do+ previous_lock_version = wp_automatic.lock_version+ wp_automatic.reload+ expect(wp_automatic.lock_version).to be > previous_lock_version+ end++ aggregate_failures "changes the updated_at of the work package" do+ expect(wp_automatic.updated_at).not_to eq(wp_automatic.created_at)+ expect(wp_automatic.updated_at).to be > wp_automatic.created_at++ first_journal, last_journal = wp_automatic.journals+ expect(wp_automatic.updated_at).not_to eq(first_journal.updated_at)+ expect(wp_automatic.updated_at).to eq(last_journal.updated_at)+ end+ end+ end+ end++ # spec from #42388, "Migration from an earlier version" section+ context "for work packages with no predecessors" do+ let_work_packages(<<~TABLE)+ subject | start date | due date | schedule manually+ wp automatic 1 | 2024-11-20 | 2024-11-21 | false+ wp automatic 2 | | 2024-11-21 | false+ wp automatic 3 | 2024-11-20 | | false+ wp automatic 4 | | | false+ wp manual 1 | 2024-11-20 | 2024-11-21 | true+ wp manual 2 | | 2024-11-21 | true+ wp manual 3 | 2024-11-20 | | true+ wp manual 4 | | | true+ TABLE++ it "switches to manual scheduling" do+ run_migration++ table_work_packages.map(&:reload)+ expect(table_work_packages).to all(be_schedule_manually)+ end+ end+end