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
Migrate progress values for status-based mode
Christophe Bliard4313fd3042be
diff --git a/app/workers/work_packages/update_progress_job.rb b/app/workers/work_packages/update_progress_job.rbindex 37d4b692cfc..664e62009bc 100644--- a/app/workers/work_packages/update_progress_job.rb+++ b/app/workers/work_packages/update_progress_job.rb@@ -30,32 +30,47 @@class WorkPackages::UpdateProgressJob < ApplicationJobqueue_with_priority :default- def perform(previous_mode: nil)- create_temporary_progress_table- if previous_mode == "disabled"- unset_all_percent_complete_values+ def perform(current_mode:, previous_mode:)+ with_temporary_progress_table do+ if previous_mode == "disabled"+ unset_all_percent_complete_values+ end+ if current_mode == "field"+ fix_remaining_work_set_with_100p_complete+ fix_remaining_work_exceeding_work+ fix_only_work_being_set+ fix_only_remaining_work_being_set+ derive_unset_remaining_work_from_work_and_p_complete+ derive_unset_work_from_remaining_work_and_p_complete+ derive_p_complete_from_work_and_remaining_work+ end+ if current_mode == "status"+ set_p_complete_from_status+ fix_remaining_work_set_with_100p_complete+ derive_unset_work_from_remaining_work_and_p_complete+ derive_remaining_work_from_work_and_p_complete+ end++ updated_work_package_ids = copy_progress_values_to_work_packages+ create_journals_for_updated_work_packages(updated_work_package_ids)end- fix_remaining_work_set_with_100p_complete- fix_remaining_work_exceeding_work- fix_only_work_being_set- fix_only_remaining_work_being_set- derive_unset_remaining_work_from_work_and_p_complete- derive_unset_work_from_remaining_work_and_p_complete- derive_p_complete_from_work_and_remaining_work-- updated_work_package_ids = copy_progress_values_to_work_packages- create_journals_for_updated_work_packages(updated_work_package_ids)- ensure- drop_temporary_progress_tableendprivate+ def with_temporary_progress_table+ create_temporary_progress_table+ yield+ ensure+ drop_temporary_progress_table+ end+def create_temporary_progress_tableexecute(<<~SQL)CREATE UNLOGGED TABLE temp_wp_progress_valuesAS SELECTid,+ status_id,estimated_hours,remaining_hours,done_ratio@@ -83,7 +98,7 @@ class WorkPackages::UpdateProgressJob < ApplicationJobSET estimated_hours = remaining_hours,remaining_hours = 0WHERE estimated_hours IS NULL- AND remaining_hours > 0+ AND remaining_hours IS NOT NULLAND done_ratio = 100SQLend@@ -129,6 +144,15 @@ class WorkPackages::UpdateProgressJob < ApplicationJobSQLend+ def derive_remaining_work_from_work_and_p_complete+ execute(<<~SQL.squish)+ UPDATE temp_wp_progress_values+ SET remaining_hours = ROUND((estimated_hours - (estimated_hours * done_ratio / 100.0))::numeric, 2)+ WHERE estimated_hours IS NOT NULL+ AND done_ratio IS NOT NULL+ SQL+ end+def derive_unset_work_from_remaining_work_and_p_completeexecute(<<~SQL.squish)UPDATE temp_wp_progress_values@@ -151,6 +175,15 @@ class WorkPackages::UpdateProgressJob < ApplicationJobSQLend+ def set_p_complete_from_status+ execute(<<~SQL.squish)+ UPDATE temp_wp_progress_values+ SET done_ratio = statuses.default_done_ratio+ FROM statuses+ WHERE temp_wp_progress_values.status_id = statuses.id+ SQL+ end+def copy_progress_values_to_work_packagesresults = execute(<<~SQL)UPDATE work_packagesdiff --git a/db/migrate/20240402072213_update_progress_calculation.rb b/db/migrate/20240402072213_update_progress_calculation.rbindex b3a96802b96..ee8743dbb88 100644--- a/db/migrate/20240402072213_update_progress_calculation.rb+++ b/db/migrate/20240402072213_update_progress_calculation.rb@@ -1,13 +1,15 @@class UpdateProgressCalculation < ActiveRecord::Migration[7.1]# See https://community.openproject.org/wp/40749 for migration detailsdef up- if progress_calculation_mode == "disabled"+ current_mode = progress_calculation_mode+ if current_mode == "disabled"set_progress_calculation_mode_to_work_basedprevious_mode = "disabled"+ current_mode = "field"endperform_method = Rails.env.production? ? :perform_later : :perform_now- WorkPackages::UpdateProgressJob.public_send(perform_method, previous_mode:)+ WorkPackages::UpdateProgressJob.public_send(perform_method, current_mode:, previous_mode:)enddef progress_calculation_modediff --git a/spec/factories/work_package_factory.rb b/spec/factories/work_package_factory.rbindex 18fb56276ef..8eeb8df5322 100644--- a/spec/factories/work_package_factory.rb+++ b/spec/factories/work_package_factory.rb@@ -146,5 +146,15 @@ FactoryBot.define docallback(:after_build, &set_done_ratios)callback(:after_stub, &set_done_ratios)++ # force done_ratio in status-based mode if given done_ratio is different from status default+ callback(:after_create) do |work_package, evaluator|+ next unless WorkPackage.use_status_for_done_ratio?+ next unless evaluator.__override_names__.include?(:done_ratio)++ if work_package.read_attribute(:done_ratio) != evaluator.done_ratio+ work_package.update_column(:done_ratio, evaluator.done_ratio)+ end+ endendenddiff --git a/spec/migrations/update_progress_calculation_spec.rb b/spec/migrations/update_progress_calculation_spec.rbindex fc35b4efd60..4fe07015c18 100644--- a/spec/migrations/update_progress_calculation_spec.rb+++ b/spec/migrations/update_progress_calculation_spec.rb@@ -512,4 +512,140 @@ RSpec.describe UpdateProgressCalculation, type: :model doendendend++ ###++ context "when in status mode" do+ shared_let(:status_0p_todo) { create(:status, name: "To do (0%)", default_done_ratio: 0) }+ shared_let(:status_30p_doing) { create(:status, name: "Doing (30%)", default_done_ratio: 30) }+ shared_let(:status_100p_done) { create(:status, name: "Done (100%)", default_done_ratio: 100) }++ before do+ Setting.work_package_done_ratio = "status"+ end++ context "when only % Complete is set, and is the same as its status" do+ it "does nothing, the % Complete value is kept, work and remaining work are kept unset, and no journal is created" do+ work_packages = expect_migrates(+ from: <<~TABLE,+ subject | status | work | remaining work | % complete+ wp 0% | To do (0%) | | | 0%+ wp 30% | Doing (30%) | | | 30%+ wp 100% | Done (100%) | | | 100%+ TABLE+ to: <<~TABLE+ subject | status | work | remaining work | % complete+ wp 0% | To do (0%) | | | 0%+ wp 30% | Doing (30%) | | | 30%+ wp 100% | Done (100%) | | | 100%+ TABLE+ )++ work_packages.each do |wp|+ # no new journals as nothing changed+ expect(wp.journals.count).to eq(1)+ end+ end+ end++ context "when % Complete is different from its status value (set or unset)" do+ it "updates % Complete value to the status value, and a journal entry is created" do+ work_packages = expect_migrates(+ from: <<~TABLE,+ subject | status | work | remaining work | % complete+ wp | Doing (30%) | | |+ wp 0% | To do (0%) | | | 55%+ wp 30% | Doing (30%) | | | 55%+ wp 100% | Done (100%) | | | 55%+ TABLE+ to: <<~TABLE+ subject | status | work | remaining work | % complete+ wp | Doing (30%) | | | 30%+ wp 0% | To do (0%) | | | 0%+ wp 30% | Doing (30%) | | | 30%+ wp 100% | Done (100%) | | | 100%+ TABLE+ )++ wp = work_packages.first+ # one new journal as % complete was changed+ expect(wp.journals.count).to eq(2)+ end+ end++ context "when only Work is set" do+ it "sets % Complete value to the status value, and derives Remaining work" do+ expect_migrates(+ from: <<~TABLE,+ subject | status | work | remaining work | % complete+ wp w 0% | To do (0%) | 10h | |+ wp w 30% | Doing (30%) | 10h | |+ wp w 100% | Done (100%) | 10h | |+ wp w 0% 0h | To do (0%) | 0h | |+ wp w 100% 0h| Done (100%) | 0h | |+ TABLE+ to: <<~TABLE+ subject | status | work | remaining work | % complete+ wp w 0% | To do (0%) | 10h | 10h | 0%+ wp w 30% | Doing (30%) | 10h | 7h | 30%+ wp w 100% | Done (100%) | 10h | 0h | 100%+ wp w 0% 0h | To do (0%) | 0h | 0h | 0%+ wp w 100% 0h| Done (100%) | 0h | 0h | 100%+ TABLE+ )+ end+ end++ context "when only Remaining work is set" do+ it "sets % Complete value to the status value, and derives Work" do+ expect_migrates(+ from: <<~TABLE,+ subject | status | work | remaining work | % complete+ rw 0% | To do (0%) | | 10h |+ rw 30% | Doing (30%) | | 7h |+ rw 100% 5h | Done (100%) | | 5h |+ rw 0% 0h | To do (0%) | | 0h |+ rw 100% 0h | Done (100%) | | 0h |+ TABLE+ to: <<~TABLE+ subject | status | work | remaining work | % complete+ rw 0% | To do (0%) | 10h | 10h | 0%+ rw 30% | Doing (30%) | 10h | 7h | 30%+ rw 100% 5h | Done (100%) | 5h | 0h | 100%+ rw 0% 0h | To do (0%) | 0h | 0h | 0%+ rw 100% 0h | Done (100%) | 0h | 0h | 100%+ TABLE+ )+ end+ end++ context "when both Work and Remaining work are set" do+ it "sets % Complete value to the status value, and derives Remaining work" do+ expect_migrates(+ from: <<~TABLE,+ subject | status | work | remaining work | % complete+ rw 0% | To do (0%) | 10h | 10h |+ rw 30% 0h | Doing (30%) | 10h | 0h |+ rw 30% 99h | Doing (30%) | 10h | 99h |+ rw 30% 5h | Doing (30%) | 10h | 5h |+ rw 30% 10h | Doing (30%) | 10h | 10h |+ rw 100% | Done (100%) | 10h | 0h |+ rw 0% 0h | To do (0%) | 0h | 0h |+ rw 100% 0h | Done (100%) | 0h | 0h |+ TABLE+ to: <<~TABLE+ subject | status | work | remaining work | % complete+ rw 0% | To do (0%) | 10h | 10h | 0%+ rw 30% 0h | Doing (30%) | 10h | 7h | 30%+ rw 30% 99h | Doing (30%) | 10h | 7h | 30%+ rw 30% 5h | Doing (30%) | 10h | 7h | 30%+ rw 30% 10h | Doing (30%) | 10h | 7h | 30%+ rw 100% | Done (100%) | 10h | 0h | 100%+ rw 0% 0h | To do (0%) | 0h | 0h | 0%+ rw 100% 0h | Done (100%) | 0h | 0h | 100%+ TABLE+ )+ end+ end+ endend