Commit 4cb7ff43 authored by Patrick Bair's avatar Patrick Bair

Merge branch '270069_fix_project_db_query_for_graphql_pagination' into 'master'

Fix project db query for GraphQL pagination

See merge request gitlab-org/gitlab!47393
parents 20d0faad a932cd7e
......@@ -165,13 +165,14 @@ module EE
scope :without_repository_size_limit, -> { where(repository_size_limit: nil) }
scope :order_by_total_repository_size_excess_desc, -> (limit) do
excess = ::ProjectStatistics.arel_table[:repository_size] +
excess_arel = ::ProjectStatistics.arel_table[:repository_size] +
::ProjectStatistics.arel_table[:lfs_objects_size] -
::Project.arel_table.coalesce(::Project.arel_table[:repository_size_limit], limit, 0)
arel_table.coalesce(arel_table[:repository_size_limit], limit, 0)
alias_node = Arel::Nodes::SqlLiteral.new('excess_storage')
joins(:statistics).order(
Arel.sql(Arel::Nodes::Descending.new(excess).to_sql)
)
select(*arel.projections, excess_arel.as(alias_node))
.joins(:statistics)
.order(excess_arel.desc)
end
delegate :shared_runners_minutes, :shared_runners_seconds, :shared_runners_seconds_last_reset,
......
# frozen_string_literal: true
module EE
module Gitlab
module Graphql
module Pagination
module Keyset
module OrderInfo
extend ::Gitlab::Utils::Override
private
override :extract_attribute_values
def extract_attribute_values(order_value)
if ordering_by_excess_storage?(order_value)
['excess_storage', order_value.direction, order_value.expr]
else
super
end
end
# determine if ordering using STORAGE
def ordering_by_excess_storage?(order_value)
order_value.expr.is_a?(Arel::Nodes::Grouping) &&
order_value.to_sql.delete('"').include?('(project_statistics.repository_size + project_statistics.lfs_objects_size)')
end
end
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Graphql::Pagination::Keyset::OrderInfo do
describe '#build_order_list' do
let(:order_list) { described_class.build_order_list(relation) }
context 'when ordering by STORAGE' do
let(:relation) { Project.order_by_total_repository_size_excess_desc(10.gigabytes) }
it 'assigns the right attribute name, named function, and direction' do
expect(order_list.count).to eq 1
expect(order_list.first.attribute_name).to eq 'excess_storage'
expect(order_list.first.named_function).to be_kind_of(Arel::Nodes::Grouping)
expect(order_list.first.named_function.to_sql.delete('"')).to include '(project_statistics.repository_size + project_statistics.lfs_objects_size)'
expect(order_list.first.sort_direction).to eq :desc
end
end
end
end
......@@ -127,3 +127,5 @@ module Gitlab
end
end
end
Gitlab::Graphql::Pagination::Keyset::OrderInfo.prepend_if_ee('EE::Gitlab::Graphql::Pagination::Keyset::OrderInfo')
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