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 c7b462e5e8c8..39293396afe6 100644 --- a/app/workers/work_packages/automatic_mode/migrate_values_job.rb +++ b/app/workers/work_packages/automatic_mode/migrate_values_job.rb @@ -103,6 +103,7 @@ def set_lags_for_follows_relations # Here is the algorithm: # - Take all follows relations with dates + # - Filter to keep only the closest relation for a same successor # - 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 @@ -111,6 +112,7 @@ def set_lags_for_follows_relations WITH follows_relations_with_dates AS ( SELECT relations.id as id, + relations.from_id as succ_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 @@ -120,6 +122,14 @@ def set_lags_for_follows_relations 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 ), + closest_follows_relations_with_dates AS ( + SELECT DISTINCT ON (succ_id) + id, + pred_date, + succ_date + FROM follows_relations_with_dates + ORDER BY succ_id, pred_date DESC + ), working_dates AS ( SELECT date::date FROM generate_series( @@ -137,8 +147,8 @@ def set_lags_for_follows_relations WHERE date > pred_date AND date < succ_date ) - FROM follows_relations_with_dates - WHERE relations.id = follows_relations_with_dates.id + FROM closest_follows_relations_with_dates + WHERE relations.id = closest_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 81ca39960181..0dcf3ae5209a 100644 --- a/spec/migrations/update_scheduling_mode_and_lags_spec.rb +++ b/spec/migrations/update_scheduling_mode_and_lags_spec.rb @@ -222,4 +222,21 @@ expect(relations.map(&:lag)).to eq([0, 0, 2]) end end + + context "for a work package following multiple work packages" do + shared_let_work_packages(<<~TABLE) + subject | MTWTFSS | properties + predecessor 1 | XX | + predecessor 2 | XX | + predecessor 3 | X | + follower | XX | follows predecessor 1, follows predecessor 2, follows predecessor 3 + TABLE + + it "sets a lag only to the closest relation" do + run_migration + + relations = _table.relations.map(&:reload) + expect(relations.map(&:lag)).to eq([0, 2, 0]) + end + end end