Commit 3a790c86 authored by Dmytro Zaporozhets (DZ)'s avatar Dmytro Zaporozhets (DZ)

Merge branch '249245-fix-sorting-by-case-in-graphql' into 'master'

Fix problem with sorting by case in GraphQL

See merge request gitlab-org/gitlab!43861
parents 46fd7bce bdba0946
...@@ -94,6 +94,8 @@ module Gitlab ...@@ -94,6 +94,8 @@ module Gitlab
[order_value.expr.expressions[0].name.to_s, order_value.direction, order_value.expr] [order_value.expr.expressions[0].name.to_s, order_value.direction, order_value.expr]
elsif ordering_by_similarity?(order_value) elsif ordering_by_similarity?(order_value)
['similarity', order_value.direction, order_value.expr] ['similarity', order_value.direction, order_value.expr]
elsif ordering_by_case?(order_value)
[order_value.expr.case.name.to_s, order_value.direction, order_value.expr]
else else
[order_value.expr.name, order_value.direction, nil] [order_value.expr.name, order_value.direction, nil]
end end
...@@ -108,6 +110,11 @@ module Gitlab ...@@ -108,6 +110,11 @@ module Gitlab
def ordering_by_similarity?(order_value) def ordering_by_similarity?(order_value)
Gitlab::Database::SimilarityScore.order_by_similarity?(order_value) Gitlab::Database::SimilarityScore.order_by_similarity?(order_value)
end end
# determine if ordering using CASE
def ordering_by_case?(order_value)
order_value.expr.is_a?(Arel::Nodes::Case)
end
end end
end end
end end
......
...@@ -63,6 +63,17 @@ RSpec.describe Gitlab::Graphql::Pagination::Keyset::OrderInfo do ...@@ -63,6 +63,17 @@ RSpec.describe Gitlab::Graphql::Pagination::Keyset::OrderInfo do
expect(order_list.first.sort_direction).to eq :desc expect(order_list.first.sort_direction).to eq :desc
end end
end end
context 'when ordering by CASE', :aggregate_failuers do
let(:relation) { Project.order(Arel::Nodes::Case.new(Project.arel_table[:pending_delete]).when(true).then(100).else(1000).asc) }
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 'pending_delete'
expect(order_list.first.named_function).to be_kind_of(Arel::Nodes::Case)
expect(order_list.first.sort_direction).to eq :asc
end
end
end end
describe '#validate_ordering' do describe '#validate_ordering' 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