Skip to content

Commit 019f162

Browse files
committed
Don't recalculate HAVING in {update,delete}_all
These values are already calculated when building the SelectManager arel, so we can use theminstead of recalculating/rebuilding them.
1 parent e0d1491 commit 019f162

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

activerecord/lib/active_record/relation.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -628,13 +628,12 @@ def update_all(updates)
628628
arel = eager_loading? ? apply_join_dependency.arel : build_arel(c)
629629
arel.source.left = table
630630

631-
having_clause_ast = having_clause.ast unless having_clause.empty?
632631
key = if model.composite_primary_key?
633632
primary_key.map { |pk| table[pk] }
634633
else
635634
table[primary_key]
636635
end
637-
stmt = arel.compile_update(values, key, having_clause_ast)
636+
stmt = arel.compile_update(values, key)
638637
c.update(stmt, "#{model} Update All").tap { reset }
639638
end
640639
end
@@ -1044,13 +1043,12 @@ def delete_all
10441043
arel = eager_loading? ? apply_join_dependency.arel : build_arel(c)
10451044
arel.source.left = table
10461045

1047-
having_clause_ast = having_clause.ast unless having_clause.empty?
10481046
key = if model.composite_primary_key?
10491047
primary_key.map { |pk| table[pk] }
10501048
else
10511049
table[primary_key]
10521050
end
1053-
stmt = arel.compile_delete(key, having_clause_ast)
1051+
stmt = arel.compile_delete(key)
10541052

10551053
c.delete(stmt, "#{model} Delete All").tap { reset }
10561054
end

activerecord/lib/arel/crud.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ def create_insert
1414
InsertManager.new
1515
end
1616

17-
def compile_update(
18-
values,
19-
key = nil,
20-
having_clause = nil
21-
)
17+
def compile_update(values, key = nil)
2218
um = UpdateManager.new(source)
2319
um.set(values)
2420
um.take(limit)
@@ -28,19 +24,19 @@ def compile_update(
2824
um.key = key
2925

3026
um.ast.groups = @ctx.groups
31-
um.having(having_clause) unless having_clause.nil?
27+
@ctx.havings.each { |h| um.having(h) }
3228
um
3329
end
3430

35-
def compile_delete(key = nil, having_clause = nil)
31+
def compile_delete(key = nil)
3632
dm = DeleteManager.new(source)
3733
dm.take(limit)
3834
dm.offset(offset)
3935
dm.order(*orders)
4036
dm.wheres = constraints
4137
dm.key = key
4238
dm.ast.groups = @ctx.groups
43-
dm.having(having_clause) unless having_clause.nil?
39+
@ctx.havings.each { |h| dm.having(h) }
4440
dm
4541
end
4642
end

0 commit comments

Comments
 (0)