summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKrasimir Angelov <kangelov@gitlab.com>2019-07-27 16:12:04 +0300
committerKrasimir Angelov <kangelov@gitlab.com>2019-07-27 16:12:04 +0300
commit19a10333ddf89fa05867d78e366675db4fa68606 (patch)
treea27ccc71ee01f651ac1ba85a9ad31d7309cafb33
parentf52e9d55ded0e392bfea5d9bf963615766d5a1a3 (diff)
downloadgitlab-ce-all-projects-recursive-cte.tar.gz
Refactor Group#all_projects to use recursive CTEall-projects-recursive-cte
Use recursive CTE to find all projects under group instead of querieng routes with LIKE.
-rw-r--r--app/models/namespace.rb18
1 files changed, 17 insertions, 1 deletions
diff --git a/app/models/namespace.rb b/app/models/namespace.rb
index 058350b16ce..d6e2d87c70d 100644
--- a/app/models/namespace.rb
+++ b/app/models/namespace.rb
@@ -232,7 +232,23 @@ class Namespace < ApplicationRecord
# Includes projects from this namespace and projects from all subgroups
# that belongs to this namespace
def all_projects
- Project.inside_path(full_path)
+ return Project.none unless id
+
+ cte = <<~SQL
+ (WITH RECURSIVE groups AS
+ (SELECT id,
+ parent_id
+ FROM namespaces
+ WHERE id = #{id}
+ UNION SELECT namespaces.id,
+ namespaces.parent_id
+ FROM namespaces
+ INNER JOIN groups ON groups.id = namespaces.parent_id) SELECT projects.*
+ FROM groups
+ JOIN projects ON projects.namespace_id = groups.id)
+ SQL
+
+ Project.from("#{cte} AS projects")
end
# Includes pipelines from this namespace and pipelines from all subgroups