diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index cef8ad76b07df9a021ab4e40f28a1559ec3b8cf1..d12283ab0a28cdf5d6b571db22794488af25bd2d 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -155,8 +155,10 @@ class MergeRequest < ActiveRecord::Base # # Returns an ActiveRecord::Relation. def self.in_projects(relation) - source = where(source_project_id: relation).select(:id) - target = where(target_project_id: relation).select(:id) + # unscoping unnecessary conditions that'll be applied + # when executing `where("merge_requests.id IN (#{union.to_sql})")` + source = unscoped.where(source_project_id: relation).select(:id) + target = unscoped.where(target_project_id: relation).select(:id) union = Gitlab::SQL::Union.new([source, target]) where("merge_requests.id IN (#{union.to_sql})") diff --git a/app/models/project.rb b/app/models/project.rb index 17cf8226bcc924b935b3ddbfcc0633b1956e5c5d..a08a832d11be95bf4f35834232f10d24ff18eecd 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -313,20 +313,15 @@ class Project < ActiveRecord::Base ntable = Namespace.arel_table pattern = "%#{query}%" - projects = select(:id).where( + # unscoping unnecessary conditions that'll be applied + # when executing `where("projects.id IN (#{union.to_sql})")` + projects = unscoped.select(:id).where( ptable[:path].matches(pattern). or(ptable[:name].matches(pattern)). or(ptable[:description].matches(pattern)) ) - # We explicitly remove any eager loading clauses as they're: - # - # 1. Not needed by this query - # 2. Combined with .joins(:namespace) lead to all columns from the - # projects & namespaces tables being selected, leading to a SQL error - # due to the columns of all UNION'd queries no longer being the same. - namespaces = select(:id). - except(:includes). + namespaces = unscoped.select(:id). joins(:namespace). where(ntable[:name].matches(pattern))