Skip to content

Commit

Permalink
Merge pull request #50079 from tttffff/mysql_null_first_last_consistency
Browse files Browse the repository at this point in the history
Arel nulls first/last implementation Mysql
  • Loading branch information
tenderlove committed Dec 8, 2023
2 parents 5621c93 + 059caa2 commit ab8f5c9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
Expand Up @@ -106,4 +106,8 @@

*Jason Meller*

* Add `nulls_last` and working `desc.nulls_first` for MySQL.

*Tristan Fellows*

Please check [7-1-stable](https://github.com/rails/rails/blob/7-1-stable/activerecord/CHANGELOG.md) for previous changes.
9 changes: 7 additions & 2 deletions activerecord/lib/arel/visitors/mysql.rb
Expand Up @@ -59,9 +59,14 @@ def visit_Arel_Nodes_NotRegexp(o, collector)
infix_value o, collector, " NOT REGEXP "
end

# no-op
def visit_Arel_Nodes_NullsFirst(o, collector)
visit o.expr, collector
visit(o.expr.expr, collector) << " IS NOT NULL, "
visit(o.expr, collector)
end

def visit_Arel_Nodes_NullsLast(o, collector)
visit(o.expr.expr, collector) << " IS NULL, "
visit(o.expr, collector)
end

def visit_Arel_Nodes_Cte(o, collector)
Expand Down
25 changes: 23 additions & 2 deletions activerecord/test/cases/arel/visitors/mysql_test.rb
Expand Up @@ -154,10 +154,31 @@ def compile(node)
end

describe "Nodes::Ordering" do
it "should no-op ascending nulls first" do
it "should handle nulls first" do
test = Table.new(:users)[:first_name].asc.nulls_first
_(compile(test)).must_be_like %{
"users"."first_name" ASC
"users"."first_name" IS NOT NULL, "users"."first_name" ASC
}
end

it "should handle nulls last" do
test = Table.new(:users)[:first_name].asc.nulls_last
_(compile(test)).must_be_like %{
"users"."first_name" IS NULL, "users"."first_name" ASC
}
end

it "should handle nulls first reversed" do
test = Table.new(:users)[:first_name].asc.nulls_first.reverse
_(compile(test)).must_be_like %{
"users"."first_name" IS NULL, "users"."first_name" DESC
}
end

it "should handle nulls last reversed" do
test = Table.new(:users)[:first_name].asc.nulls_last.reverse
_(compile(test)).must_be_like %{
"users"."first_name" IS NOT NULL, "users"."first_name" DESC
}
end
end
Expand Down

0 comments on commit ab8f5c9

Please sign in to comment.