debian-mirror-gitlab/app/services/projects/lfs_pointers/lfs_link_service.rb

63 lines
1.9 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2018-11-08 19:23:39 +05:30
# Given a list of oids, this services links the existent Lfs Objects to the project
module Projects
module LfsPointers
class LfsLinkService < BaseService
2019-12-26 22:10:19 +05:30
TooManyOidsError = Class.new(StandardError)
MAX_OIDS = 100_000
2019-12-04 20:38:33 +05:30
BATCH_SIZE = 1000
2018-11-08 19:23:39 +05:30
# Accept an array of oids to link
#
2019-07-31 22:56:46 +05:30
# Returns an array with the oid of the existent lfs objects
2018-11-08 19:23:39 +05:30
def execute(oids)
2019-07-31 22:56:46 +05:30
return [] unless project&.lfs_enabled?
2018-11-08 19:23:39 +05:30
2019-12-26 22:10:19 +05:30
if oids.size > MAX_OIDS
raise TooManyOidsError, 'Too many LFS object ids to link, please push them manually'
end
2018-11-08 19:23:39 +05:30
# Search and link existing LFS Object
link_existing_lfs_objects(oids)
end
private
def link_existing_lfs_objects(oids)
2019-12-26 22:10:19 +05:30
linked_existing_objects = []
2019-12-04 20:38:33 +05:30
iterations = 0
2019-12-26 22:10:19 +05:30
oids.each_slice(BATCH_SIZE) do |oids_batch|
# Load all existing LFS Objects immediately so we don't issue an extra
# query for the `.any?`
2020-04-08 14:13:33 +05:30
existent_lfs_objects = LfsObject.for_oids(oids_batch).load
2019-12-04 20:38:33 +05:30
next unless existent_lfs_objects.any?
2019-12-26 22:10:19 +05:30
rows = existent_lfs_objects
.not_linked_to_project(project)
.map { |existing_lfs_object| { project_id: project.id, lfs_object_id: existing_lfs_object.id } }
2020-06-23 00:09:42 +05:30
Gitlab::Database.bulk_insert(:lfs_objects_projects, rows) # rubocop:disable Gitlab/BulkInsert
2019-12-04 20:38:33 +05:30
iterations += 1
2018-11-08 19:23:39 +05:30
2019-12-26 22:10:19 +05:30
linked_existing_objects += existent_lfs_objects.map(&:oid)
2019-12-04 20:38:33 +05:30
end
2018-11-08 19:23:39 +05:30
2019-12-26 22:10:19 +05:30
log_lfs_link_results(linked_existing_objects.count, iterations)
2018-11-08 19:23:39 +05:30
2019-12-26 22:10:19 +05:30
linked_existing_objects
2018-11-08 19:23:39 +05:30
end
2019-12-04 20:38:33 +05:30
def log_lfs_link_results(lfs_objects_linked_count, iterations)
Gitlab::Import::Logger.info(
class: self.class.name,
project_id: project.id,
project_path: project.full_path,
lfs_objects_linked_count: lfs_objects_linked_count,
iterations: iterations)
end
2018-11-08 19:23:39 +05:30
end
end
end