@ -119,7 +119,7 @@ module WorkPackages::Scopes
def paths_sql ( work_packages )
values = work_packages . map { | wp | " ( #{ wp . id } , false) " } . join ( ', ' )
<< ~ SQL
<< ~ SQL . squish
to_schedule ( id , manually ) AS (
SELECT * FROM ( VALUES #{values}) AS t(id, manually)
@ -131,7 +131,7 @@ module WorkPackages::Scopes
THEN relations . from_id
ELSE relations . to_id
END id ,
( work_packages . schedule_manually OR COALESCE ( descendants . schedule_manually , false ) ) manually
( related_ work_packages. schedule_manually OR COALESCE ( descendants . schedule_manually , false ) ) manually
FROM
to_schedule
JOIN
@ -141,13 +141,13 @@ module WorkPackages::Scopes
AND
( ( relations . to_id = to_schedule . id )
OR ( relations . from_id = to_schedule . id AND relations . follows = 0 ) )
LEFT JOIN work_packages
LEFT JOIN work_packages related_ work_packages
ON ( CASE
WHEN relations . to_id = to_schedule . id
THEN relations . from_id
ELSE relations . to_id
END ) = work_packages . id
LEFT JOIN (
END ) = related_ work_packages. id
LEFT JOIN LATERAL (
SELECT
relations . from_id ,
bool_and ( COALESCE ( work_packages . schedule_manually , false ) ) schedule_manually
@ -155,15 +155,16 @@ module WorkPackages::Scopes
JOIN work_packages
ON
work_packages . id = relations . to_id
AND related_work_packages . id = relations . from_id
AND relations . follows = 0 AND #{relations_condition_sql(transitive: true)}
GROUP BY relations . from_id
) descendants ON work_packages . id = descendants . from_id
) descendants ON related_ work_packages. id = descendants . from_id
)
SQL
end
def relations_condition_sql ( transitive : false )
<< ~ SQL
<< ~ SQL . squish
" relations " . " relates " = 0 AND " relations " . " duplicates " = 0 AND " relations " . " blocks " = 0 AND " relations " . " includes " = 0 AND " relations " . " requires " = 0
AND ( relations . hierarchy + relations . relates + relations . duplicates + relations . follows + relations . blocks + relations . includes + relations . requires #{transitive ? '>' : ''}= 1)
SQL