Commit 6be56057 authored by Thong Kuah's avatar Thong Kuah

Merge branch 'sh-fix-protected-environments-nplusone' into 'master'

Fix N+1 SQL queries with protected environments

Closes #55346

See merge request gitlab-org/gitlab!22101
parents ec72fdc2 9ed56bb0
...@@ -589,7 +589,11 @@ module EE ...@@ -589,7 +589,11 @@ module EE
def protected_environment_by_name(environment_name) def protected_environment_by_name(environment_name)
return unless protected_environments_feature_available? return unless protected_environments_feature_available?
protected_environments.find_by(name: environment_name) key = "protected_environment_by_name:#{id}:#{environment_name}"
::Gitlab::SafeRequestStore.fetch(key) do
protected_environments.find_by(name: environment_name)
end
end end
override :after_import override :after_import
......
---
title: Fix N+1 SQL queries with protected environments
merge_request: 22101
author:
type: performance
...@@ -1448,7 +1448,7 @@ describe Project do ...@@ -1448,7 +1448,7 @@ describe Project do
end end
describe '#protected_environment_by_name' do describe '#protected_environment_by_name' do
let(:project) { create(:project) } let_it_be(:project) { create(:project) }
subject { project.protected_environment_by_name('production') } subject { project.protected_environment_by_name('production') }
...@@ -1465,19 +1465,27 @@ describe Project do ...@@ -1465,19 +1465,27 @@ describe Project do
context 'when Protected Environments feature is available on the project' do context 'when Protected Environments feature is available on the project' do
let(:feature_available) { true } let(:feature_available) { true }
let(:environment) { create(:environment, name: 'production') }
let(:protected_environment) { create(:protected_environment, name: environment.name, project: project) } context 'when the project environment does not exists' do
it { is_expected.to be_nil }
end
context 'when the project environment exists' do context 'when the project environment exists' do
before do let_it_be(:environment) { create(:environment, name: 'production') }
protected_environment let_it_be(:protected_environment) { create(:protected_environment, name: environment.name, project: project) }
end
it { is_expected.to eq(protected_environment) } it { is_expected.to eq(protected_environment) }
end
context 'when the project environment does not exists' do it 'caches environment name', :request_store do
it { is_expected.to be_nil } control_count = ActiveRecord::QueryRecorder.new { project.protected_environment_by_name(protected_environment.name) }
expect do
2.times { project.protected_environment_by_name(protected_environment.name) }
end.not_to exceed_query_limit(control_count)
expect(project.protected_environment_by_name('non-existent-env')).to be_nil
expect(project.protected_environment_by_name(protected_environment.name)).to eq(protected_environment)
end
end end
end end
end end
......
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