debian-mirror-gitlab/lib/gitlab/github_import/bulk_importing.rb

28 lines
796 B
Ruby
Raw Normal View History

2018-03-17 18:26:18 +05:30
# frozen_string_literal: true
module Gitlab
module GithubImport
module BulkImporting
# Builds and returns an Array of objects to bulk insert into the
# database.
#
# enum - An Enumerable that returns the objects to turn into database
# rows.
def build_database_rows(enum)
enum.each_with_object([]) do |(object, _), rows|
rows << build(object) unless already_imported?(object)
end
end
# Bulk inserts the given rows into the database.
2018-11-20 20:47:30 +05:30
def bulk_insert(model, rows, batch_size: 100, pre_hook: nil)
2018-03-17 18:26:18 +05:30
rows.each_slice(batch_size) do |slice|
2018-11-20 20:47:30 +05:30
pre_hook.call(slice) if pre_hook
2018-03-17 18:26:18 +05:30
Gitlab::Database.bulk_insert(model.table_name, slice)
end
2018-11-20 20:47:30 +05:30
rows
2018-03-17 18:26:18 +05:30
end
end
end
end