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
[#59184] simplify by using subqueries in favor of CTE
Tobias Dillmanna7f7e9fb9d20
diff --git a/app/models/queries/orders/base.rb b/app/models/queries/orders/base.rbindex af925e94cd3..9cda8338ab3 100644--- a/app/models/queries/orders/base.rb+++ b/app/models/queries/orders/base.rb@@ -1,5 +1,3 @@-# frozen_string_literal: true-#-- copyright# OpenProject is an open source project management software.# Copyright (C) the OpenProject GmbH@@ -55,7 +53,6 @@ module Queriesdef apply_to(query_scope)query_scope = order(query_scope)- query_scope = query_scope.with(cte_name => cte_statement) if cte_name && cte_statementquery_scope = query_scope.joins(joins) if joinsquery_scope = query_scope.left_outer_joins(left_outer_joins) if left_outer_joinsquery_scope@@ -83,14 +80,6 @@ module Queriesnilend- def cte_name- nil- end-- def cte_statement- nil- end-def with_raise_on_invalidif VALID_DIRECTIONS.include?(direction)yielddiff --git a/app/models/queries/projects/orders/life_cycle_step_order.rb b/app/models/queries/projects/orders/life_cycle_step_order.rbindex 11899c5a1bb..7090b448bf6 100644--- a/app/models/queries/projects/orders/life_cycle_step_order.rb+++ b/app/models/queries/projects/orders/life_cycle_step_order.rb@@ -55,24 +55,27 @@ class Queries::Projects::Orders::LifeCycleStepOrder < Queries::Orders::Basedef joins<<~SQL.squish- LEFT JOIN #{cte_name} ON #{cte_name}.project_id = projects.id+ LEFT JOIN (+ SELECT steps.*, def.name, def.id as def_id+ FROM project_life_cycle_steps steps+ LEFT JOIN project_life_cycle_step_definitions def+ ON steps.definition_id = def.id+ WHERE+ steps.active = true+ AND def.id = #{life_cycle_step_definition.id}+ ) #{subquery_table_name} ON #{subquery_table_name}.project_id = projects.idSQLend- def cte_name+ def subquery_table_name# Since we can combine multiple queries with their respective ORDER BY clauses, we need to make sure- # that the name of our CTE is unique. To achieve this, we use the definition_id as part of the CTE name.+ # that the name of our tables is unique. It suffices to include the definition id into the name as there can only+ # ever be one order statement per definition.definition_id = life_cycle_step_definition.id:"life_cycle_steps_cte_#{definition_id}"end- def cte_statement- Project::LifeCycleStep.select("*, def.id as def_id")- .joins("LEFT JOIN project_life_cycle_step_definitions def ON definition_id = def.id")- .where(active: true, definition_id: life_cycle_step_definition.id)- end-def order(scope)with_raise_on_invalid doscope.where(order_condition)@@ -83,7 +86,7 @@ class Queries::Projects::Orders::LifeCycleStepOrder < Queries::Orders::Basedef order_condition# To avoid SQL injection warnings, we use Arel to build the condition.# Note that this SQL query uses the CTE defined in `cte_statement`.- steps_cte = Arel::Table.new(cte_name.to_s)+ steps_cte = Arel::Table.new(subquery_table_name.to_s)# WHERE cte_name.def_id = life_cycle_step_definition.id OR cte_name.def_id IS NULLsteps_cte[:def_id]@@ -92,7 +95,7 @@ class Queries::Projects::Orders::LifeCycleStepOrder < Queries::Orders::Baseenddef order_by_start_and_end_date- steps_table = Arel::Table.new(cte_name.to_s)+ steps_table = Arel::Table.new(subquery_table_name.to_s)# Even though a gate does not define an end_date, this code still works.[