diff options
author | Krasimir Angelov <kangelov@gitlab.com> | 2019-07-27 16:12:04 +0300 |
---|---|---|
committer | Krasimir Angelov <kangelov@gitlab.com> | 2019-07-27 16:12:04 +0300 |
commit | 19a10333ddf89fa05867d78e366675db4fa68606 (patch) | |
tree | a27ccc71ee01f651ac1ba85a9ad31d7309cafb33 | |
parent | f52e9d55ded0e392bfea5d9bf963615766d5a1a3 (diff) | |
download | gitlab-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.rb | 18 |
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 |