Commit 819ecd5f authored by Sean McGivern's avatar Sean McGivern

Fix N+1 for notification recipients in subscribers

parent eac20b74
...@@ -31,9 +31,11 @@ module Subscribable ...@@ -31,9 +31,11 @@ module Subscribable
end end
def subscribers(project) def subscribers(project)
subscriptions_available(project) relation = subscriptions_available(project)
.where(subscribed: true) .where(subscribed: true)
.map(&:user) .select(:user_id)
User.where(id: relation)
end end
def toggle_subscription(user, project = nil) def toggle_subscription(user, project = nil)
......
...@@ -10,27 +10,50 @@ describe NotificationRecipientService do ...@@ -10,27 +10,50 @@ describe NotificationRecipientService do
let(:issue) { create(:issue, project: project, assignees: [assignee]) } let(:issue) { create(:issue, project: project, assignees: [assignee]) }
let(:note) { create(:note_on_issue, noteable: issue, project_id: issue.project_id) } let(:note) { create(:note_on_issue, noteable: issue, project_id: issue.project_id) }
def create_watcher context 'when there are multiple watchers' do
watcher = create(:user) def create_watcher
create(:notification_setting, source: project, user: watcher, level: :watch) watcher = create(:user)
create(:notification_setting, source: project, user: watcher, level: :watch)
other_projects.each do |other_project|
create(:notification_setting, source: other_project, user: watcher, level: :watch)
end
end
it 'avoids N+1 queries', :request_store do
create_watcher
service.build_new_note_recipients(note)
control_count = ActiveRecord::QueryRecorder.new do
service.build_new_note_recipients(note)
end
other_projects.each do |other_project| create_watcher
create(:notification_setting, source: other_project, user: watcher, level: :watch)
expect { service.build_new_note_recipients(note) }.not_to exceed_query_limit(control_count)
end end
end end
it 'avoids N+1 queries', :request_store do context 'when there are multiple subscribers' do
create_watcher def create_subscriber
subscriber = create(:user)
issue.subscriptions.create(user: subscriber, project: project, subscribed: true)
end
service.build_new_note_recipients(note) it 'avoids N+1 queries' do
create_subscriber
control_count = ActiveRecord::QueryRecorder.new do
service.build_new_note_recipients(note) service.build_new_note_recipients(note)
end
create_watcher control_count = ActiveRecord::QueryRecorder.new do
service.build_new_note_recipients(note)
end
expect { service.build_new_note_recipients(note) }.not_to exceed_query_limit(control_count) create_subscriber
expect { service.build_new_note_recipients(note) }.not_to exceed_query_limit(control_count)
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