Commit 07e829fc authored by Thong Kuah's avatar Thong Kuah

Environment scope should not reorder whole query

Rather, only reverse the ORDER clause for this relation
parent 99ac56ac
...@@ -23,6 +23,8 @@ module HasEnvironmentScope ...@@ -23,6 +23,8 @@ module HasEnvironmentScope
# This is equivalent to using `#last` from SQL standpoint. # This is equivalent to using `#last` from SQL standpoint.
# #
scope :on_environment, -> (environment_name, relevant_only: false) do scope :on_environment, -> (environment_name, relevant_only: false) do
order_direction = relevant_only ? 'DESC' : 'ASC'
where = <<~SQL where = <<~SQL
environment_scope IN (:wildcard, :environment_name) OR environment_scope IN (:wildcard, :environment_name) OR
:environment_name LIKE :environment_name LIKE
...@@ -34,7 +36,7 @@ module HasEnvironmentScope ...@@ -34,7 +36,7 @@ module HasEnvironmentScope
WHEN %{wildcard} THEN 0 WHEN %{wildcard} THEN 0
WHEN %{environment_name} THEN 2 WHEN %{environment_name} THEN 2
ELSE 1 ELSE 1
END END #{order_direction}
SQL SQL
values = { values = {
...@@ -69,7 +71,7 @@ module HasEnvironmentScope ...@@ -69,7 +71,7 @@ module HasEnvironmentScope
relation = where(where, values) relation = where(where, values)
.order(order % quoted_values) # `order` cannot escape for us! .order(order % quoted_values) # `order` cannot escape for us!
relation = relation.reverse_order.limit(1) if relevant_only relation = relation.limit(1) if relevant_only
relation relation
end end
......
---
title: Fix on_environment scope to not re-order whole query
merge_request: 14481
author:
type: fixed
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