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

[59539] Respect non-working days when computing lags in migration

Christophe Bliard04d3f6651b73
diff --git a/app/workers/work_packages/automatic_mode/migrate_values_job.rb b/app/workers/work_packages/automatic_mode/migrate_values_job.rb
index 3b6b03c32ad..c7b462e5e8c 100644
--- a/app/workers/work_packages/automatic_mode/migrate_values_job.rb
+++ b/app/workers/work_packages/automatic_mode/migrate_values_job.rb
@@ -99,15 +99,46 @@ class WorkPackages::AutomaticMode::MigrateValuesJob < ApplicationJob
end
def set_lags_for_follows_relations
+ working_days = Setting.working_days
+
+ # Here is the algorithm:
+ # - Take all follows relations with dates
+ # - Generate a series of dates between the min date and the max date and
+ # filter for working days
+ # - Use both information to count the number of working days between
+ # predecessor and successor dates and update the lag with it
execute(<<~SQL.squish)
+ WITH follows_relations_with_dates AS (
+ SELECT
+ relations.id as id,
+ COALESCE(wp_pred.due_date, wp_pred.start_date) as pred_date,
+ COALESCE(wp_succ.start_date, wp_succ.due_date) as succ_date
+ FROM relations
+ LEFT JOIN work_packages wp_pred ON relations.to_id = wp_pred.id
+ LEFT JOIN work_packages wp_succ ON relations.from_id = wp_succ.id
+ WHERE relation_type = 'follows'
+ AND COALESCE(wp_pred.due_date, wp_pred.start_date) IS NOT NULL
+ AND COALESCE(wp_succ.start_date, wp_succ.due_date) IS NOT NULL
+ ),
+ working_dates AS (
+ SELECT date::date
+ FROM generate_series(
+ (SELECT MIN(pred_date) FROM follows_relations_with_dates),
+ (SELECT MAX(succ_date) FROM follows_relations_with_dates),
+ '1 day'::interval
+ ) AS date
+ WHERE EXTRACT(ISODOW FROM date)::integer IN (#{working_days.join(',')})
+ AND NOT date IN (SELECT date FROM non_working_days)
+ )
UPDATE relations
- SET lag = COALESCE(wp_succ.start_date, wp_succ.due_date) - COALESCE(wp_pred.due_date, wp_pred.start_date) - 1
- FROM work_packages wp_pred, work_packages wp_succ
- WHERE relations.relation_type = 'follows'
- AND relations.to_id = wp_pred.id
- AND relations.from_id = wp_succ.id
- AND COALESCE(wp_succ.start_date, wp_succ.due_date) IS NOT NULL
- AND COALESCE(wp_pred.due_date, wp_pred.start_date) IS NOT NULL
+ SET lag = (
+ SELECT COUNT(*)
+ FROM working_dates
+ WHERE date > pred_date
+ AND date < succ_date
+ )
+ FROM follows_relations_with_dates
+ WHERE relations.id = follows_relations_with_dates.id
SQL
end
diff --git a/spec/migrations/update_scheduling_mode_and_lags_spec.rb b/spec/migrations/update_scheduling_mode_and_lags_spec.rb
index a66904e2d06..81ca3996018 100644
--- a/spec/migrations/update_scheduling_mode_and_lags_spec.rb
+++ b/spec/migrations/update_scheduling_mode_and_lags_spec.rb
@@ -156,18 +156,19 @@ RSpec.describe UpdateSchedulingModeAndLags, type: :model do
end
context "for 2 work packages following each other with distant dates" do
- let_work_packages(<<~TABLE)
+ shared_let_work_packages(<<~TABLE)
subject | MTWTFSS | properties
predecessor 1 | XX |
- follower 1 | XX | follows predecessor 1
+ follower 1 | XX | follows predecessor 1
# only start dates
predecessor 2 | [ |
- follower 2 | [ | follows predecessor 2
+ follower 2 | [ | follows predecessor 2
# only due dates
+ # if lag is already set, it's overwritten
predecessor 3 | ] |
- follower 3 | ] | follows predecessor 3 with lag 1
+ follower 3 | ] | follows predecessor 3 with lag 2
TABLE
it "sets a lag to the relation to ensure the distance is kept" do
@@ -175,7 +176,25 @@ RSpec.describe UpdateSchedulingModeAndLags, type: :model do
expect(follower1).to be_schedule_automatically
relations = _table.relations.map(&:reload)
- expect(relations.map(&:lag)).to all(eq(2))
+ expect(relations.map(&:lag)).to all(eq(3))
+ end
+
+ context "when there are non-working days between the dates" do
+ before do
+ # Wednesday is a recurring non-working day
+ set_non_working_week_days("wednesday")
+ # Thursday is a fixed non-working day
+ thursday = Date.current.next_occurring(:monday) + 3.days
+ create(:non_working_day, date: thursday)
+ end
+
+ it "computes the lag correctly by excluding non-working days" do
+ run_migration
+
+ expect(follower1).to be_schedule_automatically
+ relations = _table.relations.map(&:reload)
+ expect(relations.map(&:lag)).to all(eq(1))
+ end
end
end
@@ -188,11 +207,11 @@ RSpec.describe UpdateSchedulingModeAndLags, type: :model do
# only successor has dates
predecessor 2 | |
- follower 2 | XX | follows predecessor 2
+ follower 2 | XX | follows predecessor 2
# none have dates
predecessor 3 | |
- follower 3 | | follows predecessor 3 with lag 1
+ follower 3 | | follows predecessor 3 with lag 2
TABLE
it "does not change the existing lag" do
@@ -200,7 +219,7 @@ RSpec.describe UpdateSchedulingModeAndLags, type: :model do
expect(follower1).to be_schedule_automatically
relations = _table.relations.map(&:reload)
- expect(relations.map(&:lag)).to eq([0, 0, 1])
+ expect(relations.map(&:lag)).to eq([0, 0, 2])
end
end
end