Skip to content
Snippets Groups Projects
Commit 13d7423d authored by Tianwen Chen's avatar Tianwen Chen :two:
Browse files

Preload project's associations as soon as we know it's a Project

parent a1224cc4
No related branches found
No related tags found
No related merge requests found
This commit is part of merge request !106048. Comments created here will be created in the context of that merge request.
......@@ -89,7 +89,7 @@ def membership
# rubocop: enable CodeReuse/ActiveRecord
def project?
object.is_a?(Project)
object.is_a?(Project) && preload_project_associations
end
def type
......@@ -117,18 +117,14 @@ def last_pipeline
return unless project? && can?(request.current_user, :read_build, object)
BatchLoader.for(object).batch do |projects, loader|
# rubocop: disable CodeReuse/ActiveRecord
# This will load the project_feature association for all the projects for the line after next line
ActiveRecord::Associations::Preloader.new.preload(projects, :project_feature)
builds_enabled_projects = projects.select(&:builds_enabled?)
# Prefill nil for all the projects
projects.each { |project| loader.call(project, nil) }
# This is to load last_pipeline for all the builds-enabled projects in one SQL
Ci::Pipeline # The DISTINCT select statement will work with the below ORDER statement to return the last pipeline
.select("DISTINCT ON (project_id) *")
.where(project_id: builds_enabled_projects.map(&:id)).ci_sources
.order(:project_id, id: :desc)
.select("DISTINCT ON (project_id) id, project_id, status").ci_sources
.where(project_id: builds_enabled_projects.map(&:id)).order(:project_id, id: :desc) # rubocop: disable CodeReuse/ActiveRecord
# `each` is used instead of `find_each` because `find_each` will overwrite the above ORDER statement that we
# need for the last pipelines query to work.
.each do |pipeline|
......@@ -141,9 +137,19 @@ def last_pipeline
text: pipeline.status
})
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
def preload_project_associations
@preload_project_associations ||=
BatchLoader.for(object).batch do |projects, loader|
# This will load the associations for all the projects for the later serialization
preloader = ActiveRecord::Associations::Preloader.new
preloader.preload(projects, :project_feature) # rubocop:disable CodeReuse/ActiveRecord
# NOTE: we just need to fill `nil` to fulfill the loader
projects.each { |project| loader.call(project, nil) }
end && true
end
end
GroupChildEntity.prepend_mod_with('GroupChildEntity')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment