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

avoid generating a series of days unnecessarily

ulferts9b039d6cfbe0
diff --git a/app/models/project/phases/scopes/covering_dates_and_days_of_week.rb b/app/models/project/phases/scopes/covering_dates_and_days_of_week.rb
index 74c7e1a0e10..0bee8641bd6 100644
--- a/app/models/project/phases/scopes/covering_dates_and_days_of_week.rb
+++ b/app/models/project/phases/scopes/covering_dates_and_days_of_week.rb
@@ -57,29 +57,31 @@ module Project::Phases::Scopes::CoveringDatesAndDaysOfWeek
WHERE
start_date IS NOT NULL
OR finish_date IS NOT NULL
- ),
-
- -- generate a row for every date between start_date and finish_date
- covered_dates AS (
- SELECT
- id,
- generate_series(start_date, finish_date, '1 day') AS date
- FROM phases_with_dates
- ),
-
- -- add day of the week column
- covered_dates_and_wday AS (
- SELECT
- id,
- date,
- EXTRACT(isodow FROM date) AS dow
- FROM covered_dates
)
- -- select id of phases covering the given days
- SELECT id
- FROM covered_dates_and_wday
- WHERE dow IN (:days_of_week) OR date IN (:dates)
+ SELECT
+ id
+ FROM
+ phases_with_dates
+ WHERE
+ -- Check if the range covers any of the provided days of week. It is
+ -- done by comparing number of days from start_date to first target
+ -- day of week with number of days in range. If number is less or
+ -- eqal, then the range covers the target day of week
+ EXISTS (
+ SELECT 1
+ FROM unnest(Array[:days_of_week]::INT4[]) AS target_dow
+ WHERE (
+ MOD((target_dow + 7 - EXTRACT(isodow FROM start_date)) % 7, 7) <= (finish_date - start_date)
+ )
+ )
+ OR
+ -- Check if the range covers any of the provided dates
+ EXISTS (
+ SELECT 1
+ FROM unnest(Array[:dates]::DATE[]) AS target_date
+ WHERE target_date BETWEEN start_date AND finish_date
+ )
SQL
where("id IN (#{ids_sql})")