Commit 38717aa3 authored by Toon Claes's avatar Toon Claes

Merge branch 'tchu/fix-explore-projects-controller-n+1' into 'master'

Fix explore projects controller n+1 bullet report

See merge request gitlab-org/gitlab!36874
parents 383841db a51a599a
......@@ -80,7 +80,7 @@ class Explore::ProjectsController < Explore::ApplicationController
# rubocop: disable CodeReuse/ActiveRecord
def preload_associations(projects)
projects.includes(:route, :creator, :group, namespace: [:route, :owner])
projects.includes(:route, :creator, :group, :project_feature, namespace: [:route, :owner])
end
# rubocop: enable CodeReuse/ActiveRecord
......
---
title: Fix N+1 issue in Explore Projects controller.
merge_request: 36874
author:
type: performance
......@@ -138,6 +138,33 @@ RSpec.describe Explore::ProjectsController do
end
end
shared_examples 'avoids N+1 queries' do
[:index, :trending, :starred].each do |endpoint|
describe "GET #{endpoint}" do
render_views
# some N+1 queries still exist
it 'avoids N+1 queries' do
projects = create_list(:project, 3, :repository, :public)
projects.each do |project|
pipeline = create(:ci_pipeline, :success, project: project, sha: project.commit.id)
create(:commit_status, :success, pipeline: pipeline, ref: pipeline.ref)
end
control = ActiveRecord::QueryRecorder.new { get endpoint }
new_projects = create_list(:project, 2, :repository, :public)
new_projects.each do |project|
pipeline = create(:ci_pipeline, :success, project: project, sha: project.commit.id)
create(:commit_status, :success, pipeline: pipeline, ref: pipeline.ref)
end
expect { get endpoint }.not_to exceed_query_limit(control).with_threshold(8)
end
end
end
end
context 'when user is signed in' do
let(:user) { create(:user) }
......@@ -147,6 +174,7 @@ RSpec.describe Explore::ProjectsController do
include_examples 'explore projects'
include_examples "blocks high page numbers"
include_examples 'avoids N+1 queries'
context 'user preference sorting' do
let(:project) { create(:project) }
......@@ -160,6 +188,7 @@ RSpec.describe Explore::ProjectsController do
context 'when user is not signed in' do
include_examples 'explore projects'
include_examples "blocks high page numbers"
include_examples 'avoids N+1 queries'
context 'user preference sorting' do
let(:project) { create(:project) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment