Commit 4ae25fdb authored by Oswaldo Ferreira's avatar Oswaldo Ferreira

Improve performance and simplify queries on IssuesLink::ListService

parent 356635fa
...@@ -24,16 +24,18 @@ module IssueLinks ...@@ -24,16 +24,18 @@ module IssueLinks
private private
def issues def issues
referenced_issues = @issue.referenced_issues.select('issues.*', 'issue_links.id AS issue_links_id') authorized_issues = Issue
referred_by_issues = @issue.referred_by_issues.select('issues.*', 'issue_links.id AS issue_links_id') .not_restricted_by_confidentiality(@current_user)
authorized_issues = IssuesFinder.new(@current_user).execute .where(project_id: @current_user.authorized_projects.select(:id))
.reorder(nil)
union = Gitlab::SQL::Union.new([referenced_issues, referred_by_issues])
Issue.from("(SELECT issues.*, issue_links.id AS issue_links_id
Issue.from("(#{union.to_sql}) #{Issue.table_name}") FROM issue_links, issues
.where(id: authorized_issues.select(:id)) WHERE (issue_links.source_id = issues.id AND issue_links.target_id = #{@issue.id})
.preload(project: :namespace) OR (issue_links.target_id = issues.id AND issue_links.source_id = #{@issue.id})) #{Issue.table_name}")
.reorder('issue_links_id') .where(id: authorized_issues.select(:id))
.preload(project: :namespace)
.reorder(:issue_links_id)
end end
def destroy_relation_path(issue) def destroy_relation_path(issue)
......
...@@ -38,7 +38,7 @@ describe IssueLinks::ListService, service: true do ...@@ -38,7 +38,7 @@ describe IssueLinks::ListService, service: true do
it 'verifies number of queries' do it 'verifies number of queries' do
recorded = ActiveRecord::QueryRecorder.new { subject } recorded = ActiveRecord::QueryRecorder.new { subject }
expect(recorded.count).to be_within(1).of(37) expect(recorded.count).to be_within(1).of(35)
end end
it 'returns related issues JSON' do it 'returns related issues JSON' do
......
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