Skip to content

Commit 58a2187

Browse files
authored
Merge pull request rails#55269 from fatkodima/delete_all-annotate
Fix `annotate` comments to propagate to `update_all`/`delete_all`
2 parents b6963db + f7d6e48 commit 58a2187

File tree

11 files changed

+48
-6
lines changed

11 files changed

+48
-6
lines changed

activerecord/lib/arel/crud.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def compile_update(values, key = nil)
2121
um.offset(offset)
2222
um.order(*orders)
2323
um.wheres = constraints
24+
um.comment(comment)
2425
um.key = key
2526

2627
um.ast.groups = @ctx.groups
@@ -34,6 +35,7 @@ def compile_delete(key = nil)
3435
dm.offset(offset)
3536
dm.order(*orders)
3637
dm.wheres = constraints
38+
dm.comment(comment)
3739
dm.key = key
3840
dm.ast.groups = @ctx.groups
3941
@ctx.havings.each { |h| dm.having(h) }

activerecord/lib/arel/delete_manager.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,10 @@ def having(expr)
2828
@ast.havings << expr
2929
self
3030
end
31+
32+
def comment(value)
33+
@ast.comment = value
34+
self
35+
end
3136
end
3237
end

activerecord/lib/arel/nodes/delete_statement.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module Arel # :nodoc: all
44
module Nodes
55
class DeleteStatement < Arel::Nodes::Node
6-
attr_accessor :relation, :wheres, :groups, :havings, :orders, :limit, :offset, :key
6+
attr_accessor :relation, :wheres, :groups, :havings, :orders, :limit, :offset, :comment, :key
77

88
def initialize(relation = nil, wheres = [])
99
super()
@@ -14,6 +14,7 @@ def initialize(relation = nil, wheres = [])
1414
@orders = []
1515
@limit = nil
1616
@offset = nil
17+
@comment = nil
1718
@key = nil
1819
end
1920

@@ -24,7 +25,7 @@ def initialize_copy(other)
2425
end
2526

2627
def hash
27-
[self.class, @relation, @wheres, @orders, @limit, @offset, @key].hash
28+
[self.class, @relation, @wheres, @orders, @limit, @offset, @comment, @key].hash
2829
end
2930

3031
def eql?(other)
@@ -36,6 +37,7 @@ def eql?(other)
3637
self.havings == other.havings &&
3738
self.limit == other.limit &&
3839
self.offset == other.offset &&
40+
self.comment == other.comment &&
3941
self.key == other.key
4042
end
4143
alias :== :eql?

activerecord/lib/arel/nodes/update_statement.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module Arel # :nodoc: all
44
module Nodes
55
class UpdateStatement < Arel::Nodes::Node
6-
attr_accessor :relation, :wheres, :values, :groups, :havings, :orders, :limit, :offset, :key
6+
attr_accessor :relation, :wheres, :values, :groups, :havings, :orders, :limit, :offset, :comment, :key
77

88
def initialize(relation = nil)
99
super()
@@ -15,6 +15,7 @@ def initialize(relation = nil)
1515
@orders = []
1616
@limit = nil
1717
@offset = nil
18+
@comment = nil
1819
@key = nil
1920
end
2021

@@ -25,7 +26,7 @@ def initialize_copy(other)
2526
end
2627

2728
def hash
28-
[@relation, @wheres, @values, @orders, @limit, @offset, @key].hash
29+
[@relation, @wheres, @values, @orders, @limit, @offset, @comment, @key].hash
2930
end
3031

3132
def eql?(other)
@@ -38,6 +39,7 @@ def eql?(other)
3839
self.orders == other.orders &&
3940
self.limit == other.limit &&
4041
self.offset == other.offset &&
42+
self.comment == other.comment &&
4143
self.key == other.key
4244
end
4345
alias :== :eql?

activerecord/lib/arel/select_manager.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,12 @@ def source
255255
end
256256

257257
def comment(*values)
258-
@ctx.comment = Nodes::Comment.new(values)
259-
self
258+
if values.any?
259+
@ctx.comment = Nodes::Comment.new(values)
260+
self
261+
else
262+
@ctx.comment
263+
end
260264
end
261265

262266
private

activerecord/lib/arel/update_manager.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,10 @@ def having(expr)
4545
@ast.havings << expr
4646
self
4747
end
48+
49+
def comment(value)
50+
@ast.comment = value
51+
self
52+
end
4853
end
4954
end

activerecord/lib/arel/visitors/dot.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def visit_Arel_Nodes_UpdateStatement(o)
150150
visit_edge o, "orders"
151151
visit_edge o, "limit"
152152
visit_edge o, "offset"
153+
visit_edge o, "comment"
153154
visit_edge o, "key"
154155
end
155156

@@ -159,6 +160,7 @@ def visit_Arel_Nodes_DeleteStatement(o)
159160
visit_edge o, "orders"
160161
visit_edge o, "limit"
161162
visit_edge o, "offset"
163+
visit_edge o, "comment"
162164
visit_edge o, "key"
163165
end
164166

activerecord/lib/arel/visitors/postgresql.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def visit_Arel_Nodes_UpdateStatement(o, collector)
2929
collect_nodes_for o.wheres, collector, " WHERE ", " AND "
3030
collect_nodes_for o.orders, collector, " ORDER BY "
3131
maybe_visit o.limit, collector
32+
maybe_visit o.comment, collector
3233
end
3334

3435
# In the simple case, PostgreSQL allows us to place FROM or JOINs directly into the UPDATE

activerecord/lib/arel/visitors/sqlite.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def visit_Arel_Nodes_UpdateStatement(o, collector)
2929
collect_nodes_for o.wheres, collector, " WHERE ", " AND "
3030
collect_nodes_for o.orders, collector, " ORDER BY "
3131
maybe_visit o.limit, collector
32+
maybe_visit o.comment, collector
3233
end
3334

3435
def prepare_update_statement(o)

activerecord/lib/arel/visitors/to_sql.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def visit_Arel_Nodes_DeleteStatement(o, collector)
3535
collect_nodes_for o.wheres, collector, " WHERE ", " AND "
3636
collect_nodes_for o.orders, collector, " ORDER BY "
3737
maybe_visit o.limit, collector
38+
maybe_visit o.comment, collector
3839
end
3940

4041
def visit_Arel_Nodes_UpdateStatement(o, collector)
@@ -48,6 +49,7 @@ def visit_Arel_Nodes_UpdateStatement(o, collector)
4849
collect_nodes_for o.wheres, collector, " WHERE ", " AND "
4950
collect_nodes_for o.orders, collector, " ORDER BY "
5051
maybe_visit o.limit, collector
52+
maybe_visit o.comment, collector
5153
end
5254

5355
def visit_Arel_Nodes_InsertStatement(o, collector)

0 commit comments

Comments
 (0)