-
Notifications
You must be signed in to change notification settings - Fork 900
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18909 from kbrock/fix_order
Allow order fields to be subqueries with order
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# This monkey patches rails to support proper joins | ||
# current PR: https://github.com/rails/rails/pull/36531 | ||
module ActiveRecord | ||
module ConnectionAdapters | ||
module PostgreSQL | ||
module SchemaStatements | ||
def columns_for_distinct(columns, orders) #:nodoc: | ||
order_columns = orders.reject(&:blank?).map { |s| | ||
# Convert Arel node to string | ||
unless s.is_a?(String) | ||
if s.kind_of?(Arel::Nodes::Ordering) | ||
s = s.expr | ||
keep_order = true | ||
end | ||
if s.respond_to?(:to_sql) | ||
s = s.to_sql | ||
else # for Arel::Nodes::Attribute | ||
engine = Arel::Table.engine | ||
collector = Arel::Collectors::SQLString.new | ||
collector = engine.connection.visitor.accept s, collector | ||
s = collector.value | ||
end | ||
end | ||
# If we haven't already removed the order clause, | ||
# Remove any ASC/DESC modifiers | ||
if keep_order | ||
s | ||
else | ||
s.gsub(/\s+(?:ASC|DESC)\b/i, "") | ||
.gsub(/\s+NULLS\s+(?:FIRST|LAST)\b/i, "") | ||
end | ||
}.reject(&:blank?).map.with_index { |column, i| "#{column} AS alias_#{i}" } | ||
|
||
(order_columns << super).join(", ") | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
describe "ar_order extension" do | ||
# includes a has_many AND references the has many | ||
# this introduces a DISTINCT to the query. | ||
# rails uses limited_ids_for (which calls columns_for_distinct) to run a quick query | ||
# | ||
# when we use a virtual attribute in the sort (and the attribute has a descending order) | ||
# the string munging gives us issues. | ||
it "supports order when distinct is present for has_many virtual column" do | ||
expect do | ||
VmOrTemplate.includes(:disks).references(:disks).order(:last_compliance_status).first | ||
end.not_to raise_error | ||
end | ||
|
||
it "supports order when distinct is present for basic column" do | ||
expect do | ||
VmOrTemplate.includes(:disks).references(:disks).order(:id).first | ||
end.not_to raise_error | ||
end | ||
end |