Commit 878b844f authored by Dylan Griffith's avatar Dylan Griffith

Merge branch 'cat-fix-nplus1-solo-owned-groups' into 'master'

Fix N+1 when looking up user's solo owned groups

See merge request gitlab-org/gitlab!48340
parents c4a7dac3 aad0af54
......@@ -1245,7 +1245,7 @@ class User < ApplicationRecord
end
def solo_owned_groups
@solo_owned_groups ||= owned_groups.select do |group|
@solo_owned_groups ||= owned_groups.includes(:owners).select do |group|
group.owners == [self]
end
end
......
---
title: Fix N+1 when looking up user's solo owned groups
merge_request: 48340
author:
type: performance
......@@ -2965,6 +2965,49 @@ RSpec.describe User do
end
end
describe '#solo_owned_groups' do
let_it_be_with_refind(:user) { create(:user) }
subject(:solo_owned_groups) { user.solo_owned_groups }
context 'no owned groups' do
it { is_expected.to be_empty }
end
context 'has owned groups' do
let_it_be(:group) { create(:group) }
before do
group.add_owner(user)
end
context 'not solo owner' do
let_it_be(:user2) { create(:user) }
before do
group.add_owner(user2)
end
it { is_expected.to be_empty }
end
context 'solo owner' do
it { is_expected.to include(group) }
it 'avoids N+1 queries' do
fresh_user = User.find(user.id)
control_count = ActiveRecord::QueryRecorder.new do
fresh_user.solo_owned_groups
end.count
create(:group).add_owner(user)
expect { solo_owned_groups }.not_to exceed_query_limit(control_count)
end
end
end
end
describe "#recent_push" do
let(:user) { build(:user) }
let(:project) { build(: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