Commit 57bb697c authored by Brett Walker's avatar Brett Walker

Enable snippets to use reference filter cache

parent a012be9c
...@@ -11,10 +11,14 @@ module Banzai ...@@ -11,10 +11,14 @@ module Banzai
self.reference_type = :snippet self.reference_type = :snippet
self.object_class = Snippet self.object_class = Snippet
def find_object(project, id) def parent_records(project, ids)
return unless project.is_a?(Project) return unless project.is_a?(Project)
project.snippets.find_by(id: id) project.snippets.where(id: ids.to_a)
end
def find_object(project, id)
reference_cache.records_per_parent[project][id]
end end
def url_for_object(snippet, project) def url_for_object(snippet, project)
......
...@@ -58,6 +58,7 @@ RSpec.describe Banzai::Filter::References::ReferenceCache do ...@@ -58,6 +58,7 @@ RSpec.describe Banzai::Filter::References::ReferenceCache do
# Since this is an issue filter that is not batching issue queries # Since this is an issue filter that is not batching issue queries
# across projects, we have to account for that. # across projects, we have to account for that.
# 1 for both projects, 1 for issues in each project == 3 # 1 for both projects, 1 for issues in each project == 3
# TODO: https://gitlab.com/gitlab-org/gitlab/-/issues/330359
max_count = control_count + 1 max_count = control_count + 1
expect do expect do
......
...@@ -219,4 +219,31 @@ RSpec.describe Banzai::Filter::References::SnippetReferenceFilter do ...@@ -219,4 +219,31 @@ RSpec.describe Banzai::Filter::References::SnippetReferenceFilter do
expect(reference_filter(act, project: nil, group: create(:group)).to_html).to eq exp expect(reference_filter(act, project: nil, group: create(:group)).to_html).to eq exp
end end
end end
context 'checking N+1' do
let(:namespace2) { create(:namespace) }
let(:project2) { create(:project, :public, namespace: namespace2) }
let(:snippet2) { create(:project_snippet, project: project2) }
let(:reference2) { "#{project2.full_path}$#{snippet2.id}" }
it 'does not have N+1 per multiple references per project', :use_sql_query_cache do
markdown = "#{reference} $9999990"
control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do
reference_filter(markdown)
end.count
markdown = "#{reference} $9999990 $9999991 $9999992 $9999993 #{reference2} something/cool$12"
# Since we're not batching snippet queries across projects,
# we have to account for that.
# 1 for both projects, 1 for snippets in each project == 3
# TODO: https://gitlab.com/gitlab-org/gitlab/-/issues/330359
max_count = control_count + 1
expect do
reference_filter(markdown)
end.not_to exceed_all_query_limit(max_count)
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