Skip to content

Commit

Permalink
Fix update-all for composite key (#1234)
Browse files Browse the repository at this point in the history
  • Loading branch information
aidanharan authored Oct 2, 2024
1 parent 95118ba commit c6c01fa
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions lib/arel/visitors/sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,42 @@ def visit_Arel_Nodes_Concat(o, collector)
end

def visit_Arel_Nodes_UpdateStatement(o, collector)
if o.orders.any? && o.limit.nil?
o.limit = Nodes::Limit.new(9_223_372_036_854_775_807)
if has_join_and_composite_primary_key?(o)
update_statement_using_join(o, collector)
else
o.limit = Nodes::Limit.new(9_223_372_036_854_775_807) if o.orders.any? && o.limit.nil?

super
end
super
end

def visit_Arel_Nodes_DeleteStatement(o, collector)
if has_join_and_composite_primary_key?(o)
delete_statement_using_join(o, collector)
else
super
end
end

def has_join_and_composite_primary_key?(o)
has_join_sources?(o) && o.relation.left.instance_variable_get(:@klass).composite_primary_key?
end

def delete_statement_using_join(o, collector)
collector << "DELETE "
visit o.relation.left, collector
collector << " FROM "
visit o.relation, collector
collect_nodes_for o.wheres, collector, " WHERE ", " AND "
end

def update_statement_using_join(o, collector)
collector << "UPDATE "
visit o.relation.left, collector
collect_nodes_for o.values, collector, " SET "
collector << " FROM "
visit o.relation, collector
collect_nodes_for o.wheres, collector, " WHERE ", " AND "
end

def visit_Arel_Nodes_Lock(o, collector)
Expand Down

0 comments on commit c6c01fa

Please sign in to comment.