2017-10-13 16:50:36 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module GithubImport
|
|
|
|
module BulkImporting
|
2021-07-22 18:08:29 +00:00
|
|
|
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
|
|
|
|
|
2017-10-13 16:50:36 +00:00
|
|
|
# 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-07-22 18:08:29 +00:00
|
|
|
rows = enum.each_with_object([]) do |(object, _), result|
|
|
|
|
result << build(object) unless already_imported?(object)
|
2017-10-13 16:50:36 +00:00
|
|
|
end
|
2021-07-22 18:08:29 +00:00
|
|
|
|
|
|
|
log_and_increment_counter(rows.size, :fetched)
|
|
|
|
|
|
|
|
rows
|
2017-10-13 16:50:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Bulk inserts the given rows into the database.
|
2019-01-02 15:05:40 +00:00
|
|
|
def bulk_insert(model, rows, batch_size: 100)
|
2017-10-13 16:50:36 +00:00
|
|
|
rows.each_slice(batch_size) do |slice|
|
2021-07-29 15:09:48 +00:00
|
|
|
Gitlab::Database.main.bulk_insert(model.table_name, slice) # rubocop:disable Gitlab/BulkInsert
|
2021-07-22 18:08:29 +00:00
|
|
|
|
|
|
|
log_and_increment_counter(slice.size, :imported)
|
2017-10-13 16:50:36 +00:00
|
|
|
end
|
|
|
|
end
|
2021-07-22 18:08:29 +00:00
|
|
|
|
|
|
|
def object_type
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def log_and_increment_counter(value, operation)
|
|
|
|
Gitlab::Import::Logger.info(
|
|
|
|
import_source: :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
|
2017-10-13 16:50:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|