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
[59539] Correctly reschedule predecessors needing relations rescheduling
Christophe Bliardb4d2f5dc9c2e
diff --git a/app/models/work_packages/scopes/covering_dates_and_days_of_week.rb b/app/models/work_packages/scopes/covering_dates_and_days_of_week.rbindex 87fb3bdda65..7ccb5a27df5 100644--- a/app/models/work_packages/scopes/covering_dates_and_days_of_week.rb+++ b/app/models/work_packages/scopes/covering_dates_and_days_of_week.rb@@ -39,19 +39,61 @@ module WorkPackages::Scopes::CoveringDatesAndDaysOfWeek# @param days_of_week number[] An array of the ISO days of the week to# consider. 1 is Monday, 7 is Sunday.def covering_dates_and_days_of_week(days_of_week: [], dates: [])+ work_packages_periods_cte = work_packages_periods_cte_for_covering_work_packages+ where_covers_periods(work_packages_periods_cte, days_of_week, dates)+ end++ def predecessors_needing_relations_rescheduling(days_of_week: [], dates: [])+ work_packages_periods_cte = work_packages_periods_cte_for_predecessors_needing_relations_rescheduling+ where_covers_periods(work_packages_periods_cte, days_of_week, dates)+ end++ private++ def where_covers_periods(work_packages_periods_cte, days_of_week, dates)days_of_week = Array(days_of_week)dates = Array(dates)return none if days_of_week.empty? && dates.empty?- where("id IN (#{query(days_of_week, dates)})")- end+ covering_work_packages_query_sql = <<~SQL.squish+ -- select work packages dates+ WITH+ -- cte returning a table with work package id, period start_date and end_date+ #{work_packages_periods_cte},- private+ -- All days between the start date of a work package and its due date+ covered_dates AS (+ SELECT+ id,+ generate_series(work_packages_periods.start_date,+ work_packages_periods.end_date,+ '1 day') AS date+ FROM work_packages_periods+ ),++ -- All days between the start date of a work package and its due date including the day of the week for each date+ covered_dates_and_wday AS (+ SELECT+ id,+ date,+ EXTRACT(isodow FROM date) dow+ FROM covered_dates+ )++ -- select id of work packages covering the given days+ SELECT id FROM covered_dates_and_wday+ WHERE dow IN (:days_of_week) OR date IN (:dates)+ SQL++ covering_work_packages_query_sql = sanitize_sql([covering_work_packages_query_sql, { days_of_week:, dates: }])++ where("id IN (#{covering_work_packages_query_sql})")+ end- def query(days_of_week, dates)- sql = <<~SQL.squish- -- select work packages dates with their followers dates- WITH work_packages_with_dates AS (+ def work_packages_periods_cte_for_covering_work_packages+ <<~SQL.squish+ -- select work packages dates+ work_packages_with_dates AS (SELECT work_packages.id,work_packages.start_date AS work_package_start_date,work_packages.due_date AS work_package_due_date@@ -68,30 +110,48 @@ module WorkPackages::Scopes::CoveringDatesAndDaysOfWeekLEAST(work_package_start_date, work_package_due_date) AS start_date,GREATEST(work_package_start_date, work_package_due_date) AS end_dateFROM work_packages_with_dates+ )+ SQL+ end++ def work_packages_periods_cte_for_predecessors_needing_relations_rescheduling+ <<~SQL.squish+ follows_relations+ AS (SELECT+ relations.id as id,+ relations.to_id as pred_id,+ relations.from_id as succ_id,+ COALESCE(wp_pred.due_date, wp_pred.start_date) + INTERVAL '1 DAY' as pred_date,+ COALESCE(wp_succ.start_date, wp_succ.due_date) - INTERVAL '1 DAY' as succ_date,+ wp_succ.schedule_manually as succ_schedule_manually+ 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'),- -- All days between the start date of a work package and its due date- covered_dates AS (- SELECT- id,- generate_series(work_packages_periods.start_date,- work_packages_periods.end_date,- '1 day') AS date- FROM work_packages_periods+ -- select automatic follows relations. A relation is automatic if the+ -- successor is scheduled automatically and both successor and+ -- predecessor have dates+ -- also excluded relations that have no duration (predecessor and successor "touch" each other)+ automatic_follows_relations AS (+ SELECT *+ FROM follows_relations+ WHERE succ_schedule_manually = false+ AND pred_date IS NOT NULL+ AND succ_date IS NOT NULL+ AND pred_date <= succ_date),- -- All days between the start date of a work package and its due date including the day of the week for each date- covered_dates_and_wday AS (- SELECT- id,- date,- EXTRACT(isodow FROM date) dow- FROM covered_dates+ -- keep only the longest relation for each successor+ -- get the predecessor id and the relation period for each relation+ work_packages_periods AS (+ SELECT DISTINCT ON (succ_id)+ pred_id as id,+ pred_date as start_date,+ succ_date as end_date+ FROM automatic_follows_relations+ ORDER BY succ_id, pred_date ASC)- -- select id of work packages covering the given days- SELECT id FROM covered_dates_and_wday- WHERE dow IN (:days_of_week) OR date IN (:dates)SQL-- sanitize_sql([sql, { days_of_week:, dates: }])endendenddiff --git a/app/workers/work_packages/apply_working_days_change_job.rb b/app/workers/work_packages/apply_working_days_change_job.rbindex 64352f32b79..5c393924fa2 100644--- a/app/workers/work_packages/apply_working_days_change_job.rb+++ b/app/workers/work_packages/apply_working_days_change_job.rb@@ -106,8 +106,11 @@ class WorkPackages::ApplyWorkingDaysChangeJob < ApplicationJobenddef applicable_predecessors+ days_of_week = changed_days.keys+ dates = changed_non_working_dates.keys+WorkPackage- .where(id: Relation.follows_with_lag.select(:to_id))+ .predecessors_needing_relations_rescheduling(days_of_week:, dates:).where.not(id: already_processed_work_package_ids)enddiff --git a/spec/models/work_packages/scopes/covering_dates_and_days_of_week_spec.rb b/spec/models/work_packages/scopes/covering_dates_and_days_of_week_spec.rbindex 36341d63e85..5a27d503bbe 100644--- a/spec/models/work_packages/scopes/covering_dates_and_days_of_week_spec.rb+++ b/spec/models/work_packages/scopes/covering_dates_and_days_of_week_spec.rb@@ -31,162 +31,242 @@ require "rails_helper"RSpec.describe WorkPackages::Scopes::CoveringDatesAndDaysOfWeek docreate_shared_association_defaults_for_work_package_factory- shared_let(:next_monday) { Date.current.next_occurring(:monday) }-- shared_examples_for "covering days" do- # Construct the keyword arguments for the `#covering_dates_and_days_of_week` method.- # It builds the expected day values based on the argument type provided (`:dates`, `:days_of_week`)- # and based on the day values provided.- # By providing the `expected_days`, the specific tetscase can decide which days are expected.- let(:day_args) do- ->(*expected_days) do- days.keys.reduce(Hash.new { |hsh, key| hsh[key] = [] }) do |day_args, dt|- Array(expected_days).each do |day|- day_args[dt] << days[dt][day] if days[dt][day]- end- day_args+ # Constructs the keyword arguments for the `#covering_dates_and_days_of_week` method.+ # It's a `{ days_of_week:, dates: }` hash for given days of week built differently+ # depending on the `days_args_strategy` value.+ #+ # `days_args_strategy` can take the following values:+ # - `:days_of_week_only`: returns `{ days_of_week: ..., dates: [] }` so it+ # only contains days of week+ # - `:dates_only`: returns `{ days_of_week: [], dates: ... }` so it only+ # contains specific dates for the specified days of weeks over the next 2+ # weeks+ # - `:mixed`: returns `{ days_of_week: ..., dates: ... }` so it contains a mix of days+ # of week and specific dates: Monday, Wednesday, Friday and Sunday are days of week, and+ # Tuesday, Thursday and Saturday are dates+ def day_args(*days_of_week_as_symbols)+ next_monday = Date.current.next_occurring(:monday)+ values = days_of_week_as_symbols.map { |dow| next_monday.next_occurring(dow.to_sym) }+ .flat_map { |day| [day, day + 7.days] }++ case days_args_strategy+ when :days_of_week_only+ days_of_week = values.map(&:cwday).uniq+ dates = []+ when :dates_only+ days_of_week = []+ dates = values+ when :mixed+ # Monday, Wednesday, Friday and Sunday as days of week+ # Tuesday, Thursday and Saturday as dates+ days_of_week = values.map(&:cwday).uniq.filter(&:odd?)+ dates = values.filter { |day| day.cwday.even? }+ end+ { days_of_week:, dates: }+ end++ shared_context "with the days of week" do+ let(:days_args_strategy) { :days_of_week_only }+ end++ shared_context "with specific dates" do+ let(:days_args_strategy) { :dates_only }+ end++ shared_context "with days of week and specific dates mixed" do+ let(:days_args_strategy) { :mixed }+ end++ for_each_context "with the days of week",+ "with specific dates",+ "with days of week and specific dates mixed" do+ describe "#covering_dates_and_days_of_week" do+ it "returns work packages having start date or due date being in the given days of week" do+ table =+ create_table(<<~TABLE)+ subject | MTWTFSS |+ covered1 | XX |+ covered2 | XX |+ covered3 | X |+ covered4 | [ |+ covered5 | ] |+ not_covered1 | X |+ not_covered2 | X |+ not_covered3 | XX |+ not_covered4 | |+ TABLE++ expect(WorkPackage.covering_dates_and_days_of_week(**day_args(:tuesday)))+ .to contain_exactly(+ table.work_package("covered1"),+ table.work_package("covered2"),+ table.work_package("covered3"),+ table.work_package("covered4"),+ table.work_package("covered5")+ )+ end++ it "returns work packages having days between start date and due date being in the given days of week" do+ table =+ create_table(<<~TABLE)+ subject | MTWTFSS |+ covered1 | XXXX |+ covered2 | XXX |+ not_covered1 | XX |+ not_covered2 | X |+ TABLE++ expect(WorkPackage.covering_dates_and_days_of_week(**day_args(:tuesday, :wednesday)))+ .to contain_exactly(+ table.work_package("covered1"),+ table.work_package("covered2")+ )+ end++ context "if work package ignores non working days" do+ it "does not returns it" do+ create_table(<<~TABLE)+ subject | MTWTFSS | days counting+ not_covered | XXXXXXX | all days+ TABLE++ expect(WorkPackage.covering_dates_and_days_of_week(**day_args(:wednesday)))+ .to eq([])endend- end- it "returns work packages having start date or due date being in the given days of week" do- table =+ it "does not return work packages having follows relation covering the given days of week" docreate_table(<<~TABLE)- subject | MTWTFSS |- covered1 | XX |- covered2 | XX |- covered3 | X |- covered4 | [ |- covered5 | ] |+ subject | MTWTFSS | predecessorsnot_covered1 | X |- not_covered2 | X |- not_covered3 | XX |- not_covered4 | |+ follower1 | X | not_covered1+ not_covered2 | X |+ follower2 | X | not_covered2TABLE- expect(WorkPackage.covering_dates_and_days_of_week(**day_args[:tuesday]))- .to contain_exactly(- table.work_package("covered1"),- table.work_package("covered2"),- table.work_package("covered3"),- table.work_package("covered4"),- table.work_package("covered5")- )- end+ expect(WorkPackage.covering_dates_and_days_of_week(**day_args(:tuesday, :thursday)))+ .to eq([])+ end- it "returns work packages having days between start date and due date being in the given days of week" do- table =+ it "does not return work packages having follows relation with lag covering the given days of week" docreate_table(<<~TABLE)- subject | MTWTFSS |- covered1 | XXXX |- covered2 | XXX |- not_covered1 | XX |+ subject | MTWTFSS | predecessors+ not_covered1 | X |+ follower1 | X | not_covered1 with lag 3not_covered2 | X |+ follower2 | X | not_covered2 with lag 1TABLE- expect(WorkPackage.covering_dates_and_days_of_week(**day_args[:tuesday, :wednesday]))- .to contain_exactly(- table.work_package("covered1"),- table.work_package("covered2")- )+ expect(WorkPackage.covering_dates_and_days_of_week(**day_args(:tuesday, :thursday)))+ .to eq([])+ end++ it "accepts a single day of week or an array of days" do+ table =+ create_table(<<~TABLE)+ subject | MTWTFSS |+ covered | X |+ not_covered | X |+ TABLE++ single_value = day_args(:tuesday).transform_values { |v| Array(v).first }++ expect(WorkPackage.covering_dates_and_days_of_week(**single_value))+ .to eq([table.work_package("covered")])+ expect(WorkPackage.covering_dates_and_days_of_week(**day_args(:tuesday)))+ .to eq([table.work_package("covered")])+ expect(WorkPackage.covering_dates_and_days_of_week(**day_args(:tuesday, :wednesday)))+ .to eq([table.work_package("covered")])+ endend- context "if work package ignores non working days" do- it "does not returns it" do+ describe "#predecessors_needing_relations_rescheduling" do+ it "returns nothing if no days of week or dates are provided" docreate_table(<<~TABLE)- subject | MTWTFSS | days counting- not_covered | XXXXXXX | all days+ subject | MTWTFSS | scheduling mode | predecessors+ covered1 | XX | manual |+ succ1 | XX | automatic | covered1TABLE- expect(WorkPackage.covering_dates_and_days_of_week(**day_args[:wednesday]))+ expect(WorkPackage.predecessors_needing_relations_rescheduling(**day_args).pluck(:subject)).to eq([])end- end- it "does not return work packages having follows relation covering the given days of week" do- create_table(<<~TABLE)- subject | MTWTFSS | predecessors- not_covered1 | X |- follower1 | X | follows not_covered1- not_covered2 | X |- follower2 | X | follows not_covered2- TABLE-- expect(WorkPackage.covering_dates_and_days_of_week(**day_args[:tuesday, :thursday]))- .to eq([])- end+ it "returns work packages being predecessors in a relation covering the given days" do+ create_table(<<~TABLE)+ subject | MTWTFSS | scheduling mode | predecessors+ covered1 | XX ░ | manual |+ succ1 | ░ XX | automatic | covered1- it "does not return work packages having follows relation with lag covering the given days of week" do- create_table(<<~TABLE)- subject | MTWTFSS | predecessors- not_covered1 | X |- follower1 | X | follows not_covered1 with lag 3- not_covered2 | X |- follower2 | X | follows not_covered2 with lag 1- TABLE-- expect(WorkPackage.covering_dates_and_days_of_week(**day_args[:tuesday, :thursday]))- .to eq([])- end+ covered2 | XX░ | manual |+ succ2 | ░XX | automatic | covered2- it "accepts a single day of week or an array of days" do- table =- create_table(<<~TABLE)- subject | MTWTFSS |- covered | X |- not_covered | X |+ not_covered3 | XX░ | manual |+ succ3 | XX | automatic | not_covered3++ not_covered4 | XX | manual |+ succ4 | ░XX | automatic | not_covered4++ not_covered5 | XX | manual |+ succ5 | ░ XX | automatic | not_covered5++ not_covered6 | XX ░ | manual |+ succ6 | XX | automatic | not_covered6TABLE- single_value = day_args[:tuesday].transform_values { |v| Array(v).first }+ expect(WorkPackage.predecessors_needing_relations_rescheduling(**day_args(:wednesday)).pluck(:subject))+ .to contain_exactly(+ "covered1",+ "covered2"+ )+ end- expect(WorkPackage.covering_dates_and_days_of_week(**single_value))- .to eq([table.work_package("covered")])- expect(WorkPackage.covering_dates_and_days_of_week(**day_args[:tuesday]))- .to eq([table.work_package("covered")])- expect(WorkPackage.covering_dates_and_days_of_week(**day_args[:tuesday, :wednesday]))- .to eq([table.work_package("covered")])- end- end+ it "does not return non-impacting predecessors from a chain of successors" do+ create_table(<<~TABLE)+ subject | MTWTFSS | scheduling mode | predecessors+ pred | X ░ | manual |+ succ1 | X ░ | automatic | pred+ succ2 | X░ | automatic | succ1+ succ3 | ░X | automatic | succ2+ succ4 | ░ XX | automatic | succ3+ TABLE- context "with the days of week" do- let(:days) do- {- days_of_week: {- tuesday: 2,- wednesday: 3,- thursday: 4- }- }- end+ expect(WorkPackage.predecessors_needing_relations_rescheduling(**day_args(:thursday)).pluck(:subject))+ .to contain_exactly(+ "succ2"+ )+ end- it_behaves_like "covering days"- end+ it "returns each impacting predecessor in a chain of successors" do+ create_table(<<~TABLE)+ subject | MTWTFSSmtwtfss | scheduling mode | predecessors+ pred | X ░ ░ | manual |+ succ1 | X ░ ░ | automatic | pred+ succ2 | ░ X ░ | automatic | succ1+ succ3 | ░ X ░ | automatic | succ2+ succ4 | ░ ░ X | automatic | succ3+ TABLE- context "with specific dates" do- let(:days) do- {- dates: {- tuesday: next_monday.next_occurring(:tuesday),- wednesday: next_monday.next_occurring(:wednesday),- thursday: next_monday.next_occurring(:thursday)- }- }- end+ expect(WorkPackage.predecessors_needing_relations_rescheduling(**day_args(:friday)).pluck(:subject))+ .to contain_exactly(+ "succ1",+ "succ3"+ )+ end- it_behaves_like "covering days"- end+ it "when there are multiple follows relations to the same successor, " \+ "it returns only the farthest predecessor for each successor" do+ create_table(<<~TABLE)+ subject | MTWTFSSmtwtfss | scheduling mode | predecessors+ pred1 | X ░ ░ | manual |+ pred2 | X ░ ░ | manual |+ pred3 | ░ X ░ | manual |+ succ | ░ X ░ | automatic | pred1, pred2, pred3+ TABLE- context "with days of week and specific dates mixed" do- let(:days) do- {- days_of_week: { wednesday: 3 },- dates: {- tuesday: next_monday.next_occurring(:tuesday),- thursday: next_monday.next_occurring(:thursday)- }- }+ expect(WorkPackage.predecessors_needing_relations_rescheduling(**day_args(:friday)).pluck(:subject))+ .to contain_exactly("pred2")+ endend-- it_behaves_like "covering days"endenddiff --git a/spec/workers/work_packages/apply_working_days_change_job_spec.rb b/spec/workers/work_packages/apply_working_days_change_job_spec.rbindex bd5df32c6de..90ea4680aed 100644--- a/spec/workers/work_packages/apply_working_days_change_job_spec.rb+++ b/spec/workers/work_packages/apply_working_days_change_job_spec.rb@@ -45,7 +45,7 @@ RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob doshared_examples_for "journal updates with cause" dolet(:changed_work_packages) { [] }let(:unchanged_work_packages) { [] }- let(:changed_days) { raise "need to specify note" }+ let(:changed_days) { raise "need to specify `let(:changed_days)`" }it "adds journal entries to changed work packages" dosubject@@ -334,8 +334,7 @@ RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob doendend- context "when a follower has a predecessor with a non-working day between them that is now a working day",- skip: "TODO!!!" do+ context "when a follower has a predecessor with a non-working day between them that is now a working day" dolet_work_packages(<<~TABLE)subject | MTWTFSS | scheduling mode | predecessorspredecessor | XX░ ░░ | manual |@@ -363,6 +362,12 @@ RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob dolet(:unchanged_work_packages) do[predecessor]end+ let(:changed_days) do+ {+ "working_days" => { "3" => true },+ "non_working_days" => {}+ }+ endendend@@ -887,8 +892,7 @@ RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob doendend- context "when a follower has a predecessor with a non-working day between them that is now a working day",- skip: "TODO!!!" do+ context "when a follower has a predecessor with a non-working day between them that is now a working day" dolet_work_packages(<<~TABLE)subject | MTWTFSS | scheduling mode | predecessorspredecessor | XX░ ░░ | manual |@@ -904,7 +908,7 @@ RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob doset_working_days(non_working_day.date)end- it "does not move the follower" do+ it "moves the follower backwards" dosubjectexpect(WorkPackage.all).to match_table(<<~TABLE)subject | MTWTFSS |@@ -920,6 +924,12 @@ RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob dolet(:unchanged_work_packages) do[predecessor]end+ let(:changed_days) do+ {+ "working_days" => {},+ "non_working_days" => { non_working_day.date.iso8601 => true }+ }+ endendend@@ -1503,18 +1513,27 @@ RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob doset_working_days(non_working_day.date)end- it "does not move the follower" do+ it "moves the follower backwards" dosubjectexpect(WorkPackage.all).to match_table(<<~TABLE)subject | MTWTFSS |predecessor | XX |- follower | XX░░ |+ follower | XX ░░ |TABLEendit_behaves_like "journal updates with cause" dolet(:unchanged_work_packages) do- [predecessor, follower]+ [predecessor]+ end+ let(:changed_work_packages) do+ [follower]+ end+ let(:changed_days) do+ {+ "working_days" => {},+ "non_working_days" => { non_working_day.date.iso8601 => true }+ }endendend