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

63 lines
1.6 KiB
Ruby
Raw Normal View History

2018-03-17 18:26:18 +05:30
# frozen_string_literal: true
module Gitlab
module GithubImport
module BulkImporting
2021-10-27 15:23:28 +05:30
attr_reader :project, :client
# project - An instance of `Project`.
# client - An instance of `Gitlab::GithubImport::Client`.
def initialize(project, client)
@project = project
@client = client
end
2018-03-17 18:26:18 +05:30
# 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)
2021-10-27 15:23:28 +05:30
rows = enum.each_with_object([]) do |(object, _), result|
result << build(object) unless already_imported?(object)
2018-03-17 18:26:18 +05:30
end
2021-10-27 15:23:28 +05:30
log_and_increment_counter(rows.size, :fetched)
rows
2018-03-17 18:26:18 +05:30
end
# Bulk inserts the given rows into the database.
2019-03-02 22:35:43 +05:30
def bulk_insert(model, rows, batch_size: 100)
2018-03-17 18:26:18 +05:30
rows.each_slice(batch_size) do |slice|
2021-10-27 15:23:28 +05:30
Gitlab::Database.main.bulk_insert(model.table_name, slice) # rubocop:disable Gitlab/BulkInsert
log_and_increment_counter(slice.size, :imported)
2018-03-17 18:26:18 +05:30
end
end
2021-10-27 15:23:28 +05:30
def object_type
raise NotImplementedError
end
private
def log_and_increment_counter(value, operation)
Gitlab::Import::Logger.info(
import_type: :github,
project_id: project.id,
importer: self.class.name,
message: "#{value} #{object_type.to_s.pluralize} #{operation}"
)
Gitlab::GithubImport::ObjectCounter.increment(
project,
object_type,
operation,
value: value
)
end
2018-03-17 18:26:18 +05:30
end
end
end