diff options
Diffstat (limited to 'lib/bulk_imports/uniquify.rb')
-rw-r--r-- | lib/bulk_imports/uniquify.rb | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/bulk_imports/uniquify.rb b/lib/bulk_imports/uniquify.rb new file mode 100644 index 00000000000..a4290eb86bf --- /dev/null +++ b/lib/bulk_imports/uniquify.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +module BulkImports + module Uniquify + private + + def uniquify(namespace, data_item, data_type) + return data_item unless namespace.present? + + children_items = Set.new + + # index_namespaces_on_parent_id_and_id index supports this + Namespace.by_parent(namespace).each_batch do |relation| + children_items.merge(relation.pluck(data_type).to_set) # rubocop: disable CodeReuse/ActiveRecord + end + + return data_item unless children_items.include?(data_item) + + data_item = Gitlab::Utils::Uniquify.new(1).string(->(counter) { "#{data_item}_#{counter}" }) do |base| + children_items.include?(base) + end + end + end +end |