File obs-server-2.10-0130-Remove-unnecessary-includes-in-requests-query.patch of Package obs-server

From f3cbc91b0ea09f871894c6a8458af2d10c6251bf Mon Sep 17 00:00:00 2001
From: Eduardo Navarro <enavarro@suse.com>
Date: Tue, 30 Mar 2021 20:29:04 +0200
Subject: [PATCH] Remove unnecessary `includes` in requests query
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Sorting the requests by the Source, Target or Type columns led to
missing results.

This was caused by a wrong DISTINCT clause, that was taking fields from
two tables (`bs_request_actions` and `requests`) instead of one
(`requests`). Those extra fields in the DISTINCT clause were introduced
by an `includes` finder method.

The problem was fixed by replacing the `includes` in the `.with_action`
scope with a `joins` finder method, so the fields from
`bs_requests_actions` are no longer part of the DISTINCT modifier in the
SQL sentence.

Additionally, the `includes` in the
BsRequest::DataTable::FindForUserOrGroup#requests method was redundant.
We add `preload(:bs_request_actions)` instead.

In order to prevent using LIMIT inside subqueries, we moved the limit
finder method to the last step of the query.

Co-authored-by: Saray Cabrera PadrĂ³n <scabrerapadron@suse.de>
---
 src/api/app/controllers/request_controller.rb                   | 2 +-
 src/api/app/models/bs_request.rb                                | 2 +-
 .../bs_request/data_table/find_for_user_or_group_query.rb       | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/api/app/controllers/request_controller.rb b/src/api/app/controllers/request_controller.rb
index 21a5ac72cd..4a5cd0bd49 100644
--- a/src/api/app/controllers/request_controller.rb
+++ b/src/api/app/controllers/request_controller.rb
@@ -37,9 +37,9 @@ class RequestController < ApplicationController
     params[:ids] = params[:ids].split(',').map(&:to_i) if params[:ids]
 
     rel = BsRequest.find_for(params)
+    rel = BsRequest.where(id: rel.select(:id)).preload([{ bs_request_actions: :bs_request_action_accept_info, reviews: { history_elements: :user } }])
     rel = rel.limit(params[:limit].to_i) if params[:limit].to_i > 0
 
-    rel = BsRequest.where(id: rel.select(:id)).preload([{ bs_request_actions: :bs_request_action_accept_info, reviews: { history_elements: :user } }])
     xml = Nokogiri::XML('<collection/>', &:strict).root
     matches = 0
     rel.each do |r|
diff --git a/src/api/app/models/bs_request.rb b/src/api/app/models/bs_request.rb
index 2579665815..727c4ccb4d 100644
--- a/src/api/app/models/bs_request.rb
+++ b/src/api/app/models/bs_request.rb
@@ -39,7 +39,7 @@ class BsRequest < ApplicationRecord
 
   scope :to_accept_by_time, -> { where(state: ['new', 'review']).where('accept_at < ?', Time.now) }
   # Scopes for collections
-  scope :with_actions, -> { includes(:bs_request_actions).references(:bs_request_actions).distinct.order(priority: :asc, id: :desc) }
+  scope :with_actions, -> { joins(:bs_request_actions).distinct.order(priority: :asc, id: :desc) }
   scope :with_involved_projects, ->(project_ids) { where(bs_request_actions: { target_project_id: project_ids }) }
   scope :with_involved_packages, ->(package_ids) { where(bs_request_actions: { target_package_id: package_ids }) }
 
diff --git a/src/api/app/models/bs_request/data_table/find_for_user_or_group_query.rb b/src/api/app/models/bs_request/data_table/find_for_user_or_group_query.rb
index 5365cd0651..bd69f3d806 100644
--- a/src/api/app/models/bs_request/data_table/find_for_user_or_group_query.rb
+++ b/src/api/app/models/bs_request/data_table/find_for_user_or_group_query.rb
@@ -15,7 +15,7 @@ class BsRequest
           .offset(@params[:offset])
           .limit(@params[:limit])
           .reorder(@params[:sort])
-          .includes(:bs_request_actions)
+          .preload(:bs_request_actions)
       end
 
       def records_total
-- 
2.31.1