File tree 2 files changed +28
-1
lines changed
2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -177,7 +177,14 @@ def attribute_names
177
177
# "#{table_name}.#{column_name} #{direction}"
178
178
# "#{column_name}"
179
179
# "#{column_name} #{direction}"
180
- COLUMN_NAME_ORDER_WHITELIST = /\A (?:\w +\. )?\w +(?:\s +asc|\s +desc)?\z /i
180
+ COLUMN_NAME_ORDER_WHITELIST = /
181
+ \A
182
+ (?:\w +\. )?
183
+ \w +
184
+ (?:\s +asc|\s +desc)?
185
+ (?:\s +nulls\s +(?:first|last))?
186
+ \z
187
+ /ix
181
188
182
189
def enforce_raw_sql_whitelist ( args , whitelist : COLUMN_NAME_WHITELIST ) # :nodoc:
183
190
unexpected = args . reject do |arg |
Original file line number Diff line number Diff line change @@ -107,6 +107,26 @@ class UnsafeRawSqlTest < ActiveRecord::TestCase
107
107
assert_equal ids_expected , ids_disabled
108
108
end
109
109
110
+ test "order: allows NULLS FIRST and NULLS LAST too" do
111
+ raise "precondition failed" if Post . count < 2
112
+
113
+ # Ensure there are NULL and non-NULL post types.
114
+ Post . first . update_column ( :type , nil )
115
+ Post . last . update_column ( :type , "Programming" )
116
+
117
+ [ "asc" , "desc" , "" ] . each do |direction |
118
+ %w( first last ) . each do |position |
119
+ ids_expected = Post . order ( Arel . sql ( "type #{ direction } nulls #{ position } " ) ) . pluck ( :id )
120
+
121
+ ids_depr = with_unsafe_raw_sql_deprecated { Post . order ( "type #{ direction } nulls #{ position } " ) . pluck ( :id ) }
122
+ ids_disabled = with_unsafe_raw_sql_disabled { Post . order ( "type #{ direction } nulls #{ position } " ) . pluck ( :id ) }
123
+
124
+ assert_equal ids_expected , ids_depr
125
+ assert_equal ids_expected , ids_disabled
126
+ end
127
+ end
128
+ end if current_adapter? ( :PostgreSQLAdapter )
129
+
110
130
test "order: disallows invalid column name" do
111
131
with_unsafe_raw_sql_disabled do
112
132
assert_raises ( ActiveRecord ::UnknownAttributeReference ) do
You can’t perform that action at this time.
0 commit comments