From cb83a35db59059d1d514e28dda671ef8d90dc164 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20G=C3=BCnther?= Date: Tue, 20 Nov 2018 21:15:11 +0100 Subject: [PATCH 1/4] Cherry-pick hierarchy paths change --- app/models/relation/hierarchy_paths.rb | 58 +++++++++++++++++++------- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/app/models/relation/hierarchy_paths.rb b/app/models/relation/hierarchy_paths.rb index 463c1ef58d..991b311894 100644 --- a/app/models/relation/hierarchy_paths.rb +++ b/app/models/relation/hierarchy_paths.rb @@ -86,21 +86,49 @@ module Relation::HierarchyPaths end def self.add_hierarchy_path_sql(id = nil) - <<-SQL - INSERT INTO - #{hierarchy_table_name} - (work_package_id, path) - SELECT - to_id, #{add_hierarchy_agg_function} AS path - FROM - (SELECT to_id, from_id, hierarchy - FROM relations - WHERE hierarchy > 0 AND relates = 0 AND blocks = 0 AND duplicates = 0 AND includes = 0 AND requires = 0 AND follows = 0 - #{add_hierarchy_id_constraint(id)} - ) ordered_by_hierarchy + # Added a slighly optimized sql statement, which works without subselects. STEFFEN + if id.nil? + stmt = <<-SQL + INSERT INTO + #{hierarchy_table_name} + (work_package_id, path) + SELECT + to_id, #{add_hierarchy_agg_function} AS path + FROM + (SELECT to_id, from_id, hierarchy + FROM relations + WHERE hierarchy > 0 AND relates = 0 AND blocks = 0 AND duplicates = 0 AND includes = 0 AND requires = 0 AND follows = 0 + ) ordered_by_hierarchy + GROUP BY to_id + #{add_hierarchy_conflict_statement} + SQL + else + stmt = <<-SQL + INSERT INTO + #{hierarchy_table_name} + (work_package_id, path) + SELECT + to_id, #{add_hierarchy_agg_function} AS path + FROM ( + SELECT to_id, from_id, hierarchy + FROM relations + WHERE to_id = #{id} AND + hierarchy > 0 AND relates = 0 AND blocks = 0 AND duplicates = 0 AND includes = 0 AND requires = 0 AND follows = 0 + UNION SELECT to_id,from_id,hierarchy + FROM relations + WHERE from_id=#{id} AND + hierarchy > 0 AND relates = 0 AND blocks = 0 AND duplicates = 0 AND includes = 0 AND requires = 0 AND follows = 0 + UNION SELECT b.to_id, b.from_id, b.hierarchy FROM relations a + JOIN relations b ON b.to_id = a.to_id + WHERE a.from_id = #{id} AND + a.hierarchy > 0 AND a.relates = 0 AND a.blocks = 0 AND a.duplicates = 0 AND a.includes = 0 AND a.requires = 0 AND a.follows = 0 AND + b.hierarchy > 0 AND b.relates = 0 AND b.blocks = 0 AND b.duplicates = 0 AND b.includes = 0 AND b.requires = 0 AND b.follows = 0 + ) AS Foo GROUP BY to_id - #{add_hierarchy_conflict_statement} - SQL + #{add_hierarchy_conflict_statement} + SQL + end + stmt end def self.remove_hierarchy_path_sql(id = nil) @@ -111,7 +139,7 @@ module Relation::HierarchyPaths <<-SQL DELETE FROM #{hierarchy_table_name} - #{id_constraint} + #{id_constraint} SQL end From d87075b07efacd915849f2c86752b9201df092a6 Mon Sep 17 00:00:00 2001 From: Steffen Kirschke Date: Thu, 22 Nov 2018 07:00:28 +0100 Subject: [PATCH 2/4] Reviewed SQL statement and removed commet. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit geändert: app/models/relation/hierarchy_paths.rb --- app/models/relation/hierarchy_paths.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/models/relation/hierarchy_paths.rb b/app/models/relation/hierarchy_paths.rb index 991b311894..7022866cf4 100644 --- a/app/models/relation/hierarchy_paths.rb +++ b/app/models/relation/hierarchy_paths.rb @@ -86,7 +86,6 @@ module Relation::HierarchyPaths end def self.add_hierarchy_path_sql(id = nil) - # Added a slighly optimized sql statement, which works without subselects. STEFFEN if id.nil? stmt = <<-SQL INSERT INTO From ea01c8ef157216c44cd0faaa3291024a2d14e887 Mon Sep 17 00:00:00 2001 From: Steffen Kirschke Date: Thu, 22 Nov 2018 07:28:23 +0100 Subject: [PATCH 3/4] Minor style changes. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit geändert: app/models/relation/hierarchy_paths.rb --- app/models/relation/hierarchy_paths.rb | 65 +++++++++++--------------- 1 file changed, 28 insertions(+), 37 deletions(-) diff --git a/app/models/relation/hierarchy_paths.rb b/app/models/relation/hierarchy_paths.rb index 7022866cf4..2f42e856aa 100644 --- a/app/models/relation/hierarchy_paths.rb +++ b/app/models/relation/hierarchy_paths.rb @@ -86,48 +86,39 @@ module Relation::HierarchyPaths end def self.add_hierarchy_path_sql(id = nil) + <<-SQL + INSERT INTO + #{hierarchy_table_name} + (work_package_id, path) + SELECT + to_id, #{add_hierarchy_agg_function} AS path + FROM + (SELECT to_id, from_id, hierarchy FROM relations #{add_conditions_and_union id}) as rel + GROUP BY to_id + #{add_hierarchy_conflict_statement} + SQL + end + + def self.add_conditions_and_union(id) if id.nil? - stmt = <<-SQL - INSERT INTO - #{hierarchy_table_name} - (work_package_id, path) - SELECT - to_id, #{add_hierarchy_agg_function} AS path - FROM - (SELECT to_id, from_id, hierarchy - FROM relations - WHERE hierarchy > 0 AND relates = 0 AND blocks = 0 AND duplicates = 0 AND includes = 0 AND requires = 0 AND follows = 0 - ) ordered_by_hierarchy - GROUP BY to_id - #{add_hierarchy_conflict_statement} + <<-SQL + WHERE hierarchy > 0 AND relates = 0 AND blocks = 0 AND duplicates = 0 AND includes = 0 AND requires = 0 AND follows = 0 SQL else - stmt = <<-SQL - INSERT INTO - #{hierarchy_table_name} - (work_package_id, path) - SELECT - to_id, #{add_hierarchy_agg_function} AS path - FROM ( - SELECT to_id, from_id, hierarchy - FROM relations - WHERE to_id = #{id} AND - hierarchy > 0 AND relates = 0 AND blocks = 0 AND duplicates = 0 AND includes = 0 AND requires = 0 AND follows = 0 - UNION SELECT to_id,from_id,hierarchy - FROM relations - WHERE from_id=#{id} AND - hierarchy > 0 AND relates = 0 AND blocks = 0 AND duplicates = 0 AND includes = 0 AND requires = 0 AND follows = 0 - UNION SELECT b.to_id, b.from_id, b.hierarchy FROM relations a - JOIN relations b ON b.to_id = a.to_id - WHERE a.from_id = #{id} AND - a.hierarchy > 0 AND a.relates = 0 AND a.blocks = 0 AND a.duplicates = 0 AND a.includes = 0 AND a.requires = 0 AND a.follows = 0 AND - b.hierarchy > 0 AND b.relates = 0 AND b.blocks = 0 AND b.duplicates = 0 AND b.includes = 0 AND b.requires = 0 AND b.follows = 0 - ) AS Foo - GROUP BY to_id - #{add_hierarchy_conflict_statement} + <<-SQL + WHERE to_id = #{id} AND + hierarchy > 0 AND relates = 0 AND blocks = 0 AND duplicates = 0 AND includes = 0 AND requires = 0 AND follows = 0 + UNION SELECT to_id,from_id,hierarchy + FROM relations + WHERE from_id=#{id} AND + hierarchy > 0 AND relates = 0 AND blocks = 0 AND duplicates = 0 AND includes = 0 AND requires = 0 AND follows = 0 + UNION SELECT b.to_id, b.from_id, b.hierarchy FROM relations a + JOIN relations b ON b.to_id = a.to_id + WHERE a.from_id = #{id} AND + a.hierarchy > 0 AND a.relates = 0 AND a.blocks = 0 AND a.duplicates = 0 AND a.includes = 0 AND a.requires = 0 AND a.follows = 0 AND + b.hierarchy > 0 AND b.relates = 0 AND b.blocks = 0 AND b.duplicates = 0 AND b.includes = 0 AND b.requires = 0 AND b.follows = 0 SQL end - stmt end def self.remove_hierarchy_path_sql(id = nil) From 78db98a61b71c3d94d640b62d762321b1cf171bb Mon Sep 17 00:00:00 2001 From: Steffen Kirschke Date: Thu, 22 Nov 2018 08:05:31 +0100 Subject: [PATCH 4/4] Removed a bad whitespace MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit geändert: app/models/relation/hierarchy_paths.rb --- app/models/relation/hierarchy_paths.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/relation/hierarchy_paths.rb b/app/models/relation/hierarchy_paths.rb index 2f42e856aa..01569a4aee 100644 --- a/app/models/relation/hierarchy_paths.rb +++ b/app/models/relation/hierarchy_paths.rb @@ -101,7 +101,7 @@ module Relation::HierarchyPaths def self.add_conditions_and_union(id) if id.nil? - <<-SQL + <<-SQL WHERE hierarchy > 0 AND relates = 0 AND blocks = 0 AND duplicates = 0 AND includes = 0 AND requires = 0 AND follows = 0 SQL else