Commit c0616ba8 authored by Stan Hu's avatar Stan Hu

Merge branch 'mk/fix-n-plus-1-queries-in-uploads-check-rake-task' into 'master'

Fix N+1 queries in uploads check rake task

Closes #47924

See merge request gitlab-org/gitlab-ce!20012
parents e40aa397 ba56b344
...@@ -28,6 +28,7 @@ By default, QueryRecorder will ignore cached queries in the count. However, it m ...@@ -28,6 +28,7 @@ By default, QueryRecorder will ignore cached queries in the count. However, it m
all queries to avoid introducing an N+1 query that may be masked by the statement cache. To do this, all queries to avoid introducing an N+1 query that may be masked by the statement cache. To do this,
pass the `skip_cached` variable to `QueryRecorder` and use the `exceed_all_query_limit` matcher: pass the `skip_cached` variable to `QueryRecorder` and use the `exceed_all_query_limit` matcher:
```
it "avoids N+1 database queries" do it "avoids N+1 database queries" do
control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) { visit_some_page }.count control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) { visit_some_page }.count
create_list(:issue, 5) create_list(:issue, 5)
......
...@@ -12,7 +12,7 @@ module Gitlab ...@@ -12,7 +12,7 @@ module Gitlab
private private
def all_relation def all_relation
Upload.all Upload.all.preload(:model)
end end
def local?(upload) def local?(upload)
......
...@@ -47,6 +47,10 @@ describe Gitlab::Verify::Uploads do ...@@ -47,6 +47,10 @@ describe Gitlab::Verify::Uploads do
before do before do
stub_uploads_object_storage(AvatarUploader) stub_uploads_object_storage(AvatarUploader)
upload.update!(store: ObjectStorage::Store::REMOTE) upload.update!(store: ObjectStorage::Store::REMOTE)
end
describe 'returned hash object' do
before do
expect(CarrierWave::Storage::Fog::File).to receive(:new).and_return(file) expect(CarrierWave::Storage::Fog::File).to receive(:new).and_return(file)
end end
...@@ -63,5 +67,30 @@ describe Gitlab::Verify::Uploads do ...@@ -63,5 +67,30 @@ describe Gitlab::Verify::Uploads do
expect(failure).to include('Remote object does not exist') expect(failure).to include('Remote object does not exist')
end end
end end
describe 'performance' do
before do
allow(file).to receive(:exists?)
allow(CarrierWave::Storage::Fog::File).to receive(:new).and_return(file)
end
it "avoids N+1 queries" do
control_count = ActiveRecord::QueryRecorder.new { perform_task }
# Create additional uploads in object storage
projects = create_list(:project, 3, :with_avatar)
uploads = projects.flat_map(&:uploads)
uploads.each do |upload|
upload.update!(store: ObjectStorage::Store::REMOTE)
end
expect { perform_task }.not_to exceed_query_limit(control_count)
end
def perform_task
described_class.new(batch_size: 100).run_batches { }
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