Skip to content

Commit

Permalink
[59539] Only set lag for the closest relation in the migration
Browse files Browse the repository at this point in the history
  • Loading branch information
cbliard committed Nov 27, 2024
1 parent 0ff7554 commit d1e40d2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
14 changes: 12 additions & 2 deletions app/workers/work_packages/automatic_mode/migrate_values_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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

Expand Down
17 changes: 17 additions & 0 deletions spec/migrations/update_scheduling_mode_and_lags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d1e40d2

Please sign in to comment.