debian-mirror-gitlab/app/models/preloaders/project_root_ancestor_preloader.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

39 lines
1.3 KiB
Ruby
Raw Normal View History

2022-10-11 01:57:18 +05:30
# frozen_string_literal: true
module Preloaders
class ProjectRootAncestorPreloader
def initialize(projects, namespace_sti_name = :namespace, root_ancestor_preloads = [])
@projects = projects
@namespace_sti_name = namespace_sti_name
@root_ancestor_preloads = root_ancestor_preloads
end
def execute
2023-01-13 00:05:48 +05:30
return unless @projects.is_a?(ActiveRecord::Relation)
2022-10-11 01:57:18 +05:30
return unless ::Feature.enabled?(:use_traversal_ids)
root_query = Namespace.joins("INNER JOIN (#{join_sql}) as root_query ON root_query.root_id = namespaces.id")
.select('namespaces.*, root_query.id as source_id')
root_query = root_query.preload(*@root_ancestor_preloads) if @root_ancestor_preloads.any?
root_ancestors_by_id = root_query.group_by(&:source_id)
2023-05-27 22:25:52 +05:30
ActiveRecord::Associations::Preloader.new(records: @projects, associations: :namespace).call
2022-10-11 01:57:18 +05:30
@projects.each do |project|
2022-11-25 23:54:43 +05:30
root_ancestor = root_ancestors_by_id[project.id]&.first
project.namespace.root_ancestor = root_ancestor if root_ancestor.present?
2022-10-11 01:57:18 +05:30
end
end
private
def join_sql
@projects
.joins(@namespace_sti_name)
.select('projects.id, namespaces.traversal_ids[1] as root_id')
.to_sql
end
end
end