Commit 65d0b8be authored by Alex Kalderimis's avatar Alex Kalderimis Committed by Alex Kalderimis

Allow set_all to do the grouping

parent 1567fd8f
...@@ -102,22 +102,16 @@ module RelativePositioning ...@@ -102,22 +102,16 @@ module RelativePositioning
delta = at_end ? gap : -gap delta = at_end ? gap : -gap
indexed = (at_end ? objects : objects.reverse).each_with_index indexed = (at_end ? objects : objects.reverse).each_with_index
# Some classes are polymorphic, and not all siblings are in the same table.
by_model = indexed.group_by { |pair| pair.first.model_class }
lower_bound, upper_bound = at_end ? [position, MAX_POSITION] : [MIN_POSITION, position] lower_bound, upper_bound = at_end ? [position, MAX_POSITION] : [MIN_POSITION, position]
by_model.each do |model, pairs| representative.model_class.transaction do
model.transaction do indexed.each_slice(100) do |batch|
pairs.each_slice(100) do |batch|
mapping = batch.to_h.transform_values! do |i| mapping = batch.to_h.transform_values! do |i|
desired_pos = position + delta * (i + 1) desired_pos = position + delta * (i + 1)
{ relative_position: desired_pos.clamp(lower_bound, upper_bound) } { relative_position: desired_pos.clamp(lower_bound, upper_bound) }
end end
::Gitlab::Database::SetAll::Setter ::Gitlab::Database::SetAll.set_all([:relative_position], mapping, &:model_class)
.new(model, [:relative_position], mapping)
.update!
end
end end
end end
......
...@@ -103,8 +103,8 @@ module Gitlab ...@@ -103,8 +103,8 @@ module Gitlab
end end
end end
def self.set_all(columns, mapping) def self.set_all(columns, mapping, &to_class)
mapping.group_by { |k, v| k.class }.each do |model, entries| mapping.group_by { |k, v| block_given? ? to_class.call(k) : k.class }.each do |model, entries|
Setter.new(model, columns, entries).update! Setter.new(model, columns, entries).update!
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment