File tree 1 file changed +40
-0
lines changed
1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ # ActiveRecord's update_all invokes compile_update for the Arel::SelectManager returned
4
+ # by build_arel. It returns an Arel::UpdateManager. This makes sure that internal call
5
+ # assembles the update query correctly.
6
+
7
+ module Unreliable
8
+ class SqlTestingData
9
+ class_attribute :update_manager_sql
10
+ end
11
+ end
12
+
13
+ RSpec . describe "update_manager" do
14
+ it "in ActiveRecord >= 7, updates by subquery with select in random order" ,
15
+ skip : ( ( ActiveRecord ::VERSION ::MAJOR < 7 ) ? "test is for ActiveRecord >= 7 only" : false ) do
16
+ module Arel
17
+ class SelectManager
18
+ def testing_compile_update ( *args )
19
+ um = old_compile_update ( *args )
20
+ Unreliable ::SqlTestingData . update_manager_sql = um . to_sql
21
+ um
22
+ end
23
+ alias_method :old_compile_update , :compile_update
24
+ alias_method :compile_update , :testing_compile_update
25
+ end
26
+ end
27
+ Cat . update_all ( name : "bar" )
28
+ expect ( Unreliable ::SqlTestingData . update_manager_sql ) . to end_with ( "ORDER BY RANDOM())" )
29
+ Cat . where ( name : "foo" ) . update_all ( name : "bar" )
30
+ expect ( Unreliable ::SqlTestingData . update_manager_sql ) . to end_with ( "ORDER BY RANDOM())" )
31
+ Cat . where ( name : "bar" ) . order ( :id ) . update_all ( name : "baz" )
32
+ expect ( Unreliable ::SqlTestingData . update_manager_sql ) . to end_with ( "ORDER BY \" cats\" .\" id\" ASC)" )
33
+ ensure
34
+ module Arel
35
+ class SelectManager
36
+ alias_method :compile_update , :old_compile_update
37
+ end
38
+ end
39
+ end
40
+ end
You can’t perform that action at this time.
0 commit comments