diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-06 18:13:23 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-06 18:13:23 +0300 |
commit | b333706699e505b2a0a4fa9cc64b9d2358f271a5 (patch) | |
tree | 8d7f450089b32bcf038269c5e2849887b45c212d /app/models/preloaders | |
parent | 65b6ccd12e2e440baafd88851470d032c6ebe2c5 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/models/preloaders')
4 files changed, 62 insertions, 2 deletions
diff --git a/app/models/preloaders/group_policy_preloader.rb b/app/models/preloaders/group_policy_preloader.rb index 44030140ce3..23632a9b6c2 100644 --- a/app/models/preloaders/group_policy_preloader.rb +++ b/app/models/preloaders/group_policy_preloader.rb @@ -17,4 +17,4 @@ module Preloaders end end -Preloaders::GroupPolicyPreloader.prepend_mod_with('Preloaders::GroupPolicyPreloader') +Preloaders::GroupPolicyPreloader.prepend_mod diff --git a/app/models/preloaders/project_policy_preloader.rb b/app/models/preloaders/project_policy_preloader.rb new file mode 100644 index 00000000000..fe9db3464c7 --- /dev/null +++ b/app/models/preloaders/project_policy_preloader.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module Preloaders + class ProjectPolicyPreloader + def initialize(projects, current_user) + @projects = projects + @current_user = current_user + end + + def execute + return if projects.is_a?(ActiveRecord::NullRelation) + + ActiveRecord::Associations::Preloader.new.preload(projects, { group: :route, namespace: :owner }) + ::Preloaders::UserMaxAccessLevelInProjectsPreloader.new(projects, current_user).execute + end + + private + + attr_reader :projects, :current_user + end +end + +Preloaders::ProjectPolicyPreloader.prepend_mod diff --git a/app/models/preloaders/project_root_ancestor_preloader.rb b/app/models/preloaders/project_root_ancestor_preloader.rb new file mode 100644 index 00000000000..8d04e71774c --- /dev/null +++ b/app/models/preloaders/project_root_ancestor_preloader.rb @@ -0,0 +1,37 @@ +# 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 + return if @projects.is_a?(ActiveRecord::NullRelation) + 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) + + ActiveRecord::Associations::Preloader.new.preload(@projects, :namespace) + @projects.each do |project| + project.namespace.root_ancestor = root_ancestors_by_id[project.id]&.first + 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 diff --git a/app/models/preloaders/users_max_access_level_in_projects_preloader.rb b/app/models/preloaders/users_max_access_level_in_projects_preloader.rb index 99a31a620c5..f32184f168d 100644 --- a/app/models/preloaders/users_max_access_level_in_projects_preloader.rb +++ b/app/models/preloaders/users_max_access_level_in_projects_preloader.rb @@ -51,4 +51,4 @@ module Preloaders end end -# Preloaders::UsersMaxAccessLevelInProjectsPreloader.prepend_mod +Preloaders::UsersMaxAccessLevelInProjectsPreloader.prepend_mod |