debian-mirror-gitlab/lib/gitlab/object_hierarchy.rb

176 lines
6 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
module Gitlab
2019-02-13 22:33:31 +05:30
# Retrieving of parent or child objects based on a base ActiveRecord relation.
2017-09-10 17:25:29 +05:30
#
# This class uses recursive CTEs and as a result will only work on PostgreSQL.
2019-02-13 22:33:31 +05:30
class ObjectHierarchy
2017-09-10 17:25:29 +05:30
attr_reader :ancestors_base, :descendants_base, :model
# ancestors_base - An instance of ActiveRecord::Relation for which to
2019-02-13 22:33:31 +05:30
# get parent objects.
2017-09-10 17:25:29 +05:30
# descendants_base - An instance of ActiveRecord::Relation for which to
2019-02-13 22:33:31 +05:30
# get child objects. If omitted, ancestors_base is used.
2017-09-10 17:25:29 +05:30
def initialize(ancestors_base, descendants_base = ancestors_base)
raise ArgumentError.new("Model of ancestors_base does not match model of descendants_base") if ancestors_base.model != descendants_base.model
@ancestors_base = ancestors_base
@descendants_base = descendants_base
@model = ancestors_base.model
end
2018-03-17 18:26:18 +05:30
# Returns the set of descendants of a given relation, but excluding the given
# relation
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
def descendants
base_and_descendants.where.not(id: descendants_base.select(:id))
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
# Returns the set of ancestors of a given relation, but excluding the given
# relation
#
# Passing an `upto` will stop the recursion once the specified parent_id is
# reached. So all ancestors *lower* than the specified ancestor will be
# included.
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2019-02-13 22:33:31 +05:30
def ancestors(upto: nil, hierarchy_order: nil)
base_and_ancestors(upto: upto, hierarchy_order: hierarchy_order).where.not(id: ancestors_base.select(:id))
2018-03-17 18:26:18 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
2019-02-13 22:33:31 +05:30
# Returns a relation that includes the ancestors_base set of objects
2017-09-10 17:25:29 +05:30
# and all their ancestors (recursively).
2018-03-17 18:26:18 +05:30
#
# Passing an `upto` will stop the recursion once the specified parent_id is
# reached. So all ancestors *lower* than the specified acestor will be
# included.
2019-02-13 22:33:31 +05:30
#
# Passing a `hierarchy_order` with either `:asc` or `:desc` will cause the
# recursive query order from most nested object to root or from the root
# ancestor to most nested object respectively. This uses a `depth` column
# where `1` is defined as the depth for the base and increment as we go up
# each parent.
# rubocop: disable CodeReuse/ActiveRecord
def base_and_ancestors(upto: nil, hierarchy_order: nil)
return ancestors_base unless hierarchy_supported?
2017-09-10 17:25:29 +05:30
2019-02-13 22:33:31 +05:30
recursive_query = base_and_ancestors_cte(upto, hierarchy_order).apply_to(model.all)
recursive_query = recursive_query.order(depth: hierarchy_order) if hierarchy_order
read_only(recursive_query)
2017-09-10 17:25:29 +05:30
end
2019-02-13 22:33:31 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
2019-02-13 22:33:31 +05:30
# Returns a relation that includes the descendants_base set of objects
2017-09-10 17:25:29 +05:30
# and all their descendants (recursively).
def base_and_descendants
2019-02-13 22:33:31 +05:30
return descendants_base unless hierarchy_supported?
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
read_only(base_and_descendants_cte.apply_to(model.all))
2017-09-10 17:25:29 +05:30
end
2019-02-13 22:33:31 +05:30
# Returns a relation that includes the base objects, their ancestors,
# and the descendants of the base objects.
2017-09-10 17:25:29 +05:30
#
# The resulting query will roughly look like the following:
#
# WITH RECURSIVE ancestors AS ( ... ),
# descendants AS ( ... )
# SELECT *
# FROM (
# SELECT *
# FROM ancestors namespaces
#
# UNION
#
# SELECT *
# FROM descendants namespaces
# ) groups;
#
# Using this approach allows us to further add criteria to the relation with
# Rails thinking it's selecting data the usual way.
#
2019-02-13 22:33:31 +05:30
# If nested objects are not supported, ancestors_base is returned.
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2019-02-13 22:33:31 +05:30
def all_objects
return ancestors_base unless hierarchy_supported?
2017-09-10 17:25:29 +05:30
ancestors = base_and_ancestors_cte
descendants = base_and_descendants_cte
2019-02-13 22:33:31 +05:30
ancestors_table = ancestors.alias_to(objects_table)
descendants_table = descendants.alias_to(objects_table)
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
relation = model
2017-09-10 17:25:29 +05:30
.unscoped
.with
.recursive(ancestors.to_arel, descendants.to_arel)
2018-12-05 23:21:45 +05:30
.from_union([
model.unscoped.from(ancestors_table),
model.unscoped.from(descendants_table)
])
2018-03-17 18:26:18 +05:30
read_only(relation)
2017-09-10 17:25:29 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
private
2019-02-13 22:33:31 +05:30
def hierarchy_supported?
Gitlab::Database.postgresql?
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2019-02-13 22:33:31 +05:30
def base_and_ancestors_cte(stop_id = nil, hierarchy_order = nil)
2017-09-10 17:25:29 +05:30
cte = SQL::RecursiveCTE.new(:base_and_ancestors)
2019-02-13 22:33:31 +05:30
depth_column = :depth
2017-09-10 17:25:29 +05:30
2019-02-13 22:33:31 +05:30
base_query = ancestors_base.except(:order)
base_query = base_query.select("1 as #{depth_column}", objects_table[Arel.star]) if hierarchy_order
cte << base_query
2017-09-10 17:25:29 +05:30
# Recursively get all the ancestors of the base set.
2018-03-17 18:26:18 +05:30
parent_query = model
2019-02-13 22:33:31 +05:30
.from([objects_table, cte.table])
.where(objects_table[:id].eq(cte.table[:parent_id]))
2017-09-10 17:25:29 +05:30
.except(:order)
2019-02-13 22:33:31 +05:30
parent_query = parent_query.select(cte.table[depth_column] + 1, objects_table[Arel.star]) if hierarchy_order
2018-03-17 18:26:18 +05:30
parent_query = parent_query.where(cte.table[:parent_id].not_eq(stop_id)) if stop_id
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
cte << parent_query
2017-09-10 17:25:29 +05:30
cte
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
def base_and_descendants_cte
cte = SQL::RecursiveCTE.new(:base_and_descendants)
cte << descendants_base.except(:order)
# Recursively get all the descendants of the base set.
cte << model
2019-02-13 22:33:31 +05:30
.from([objects_table, cte.table])
.where(objects_table[:parent_id].eq(cte.table[:id]))
2017-09-10 17:25:29 +05:30
.except(:order)
cte
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
2019-02-13 22:33:31 +05:30
def objects_table
2017-09-10 17:25:29 +05:30
model.arel_table
end
2018-03-17 18:26:18 +05:30
def read_only(relation)
# relations using a CTE are not safe to use with update_all as it will
# throw away the CTE, hence we mark them as read-only.
relation.extend(Gitlab::Database::ReadOnlyRelation)
relation
end
2017-09-10 17:25:29 +05:30
end
end