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

53 lines
1.5 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-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
# Search and link existing LFS Object
link_existing_lfs_objects(oids)
end
private
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-11-08 19:23:39 +05:30
def link_existing_lfs_objects(oids)
2019-12-04 20:38:33 +05:30
all_existing_objects = []
iterations = 0
LfsObject.where(oid: oids).each_batch(of: BATCH_SIZE) do |existent_lfs_objects|
next unless existent_lfs_objects.any?
iterations += 1
not_linked_lfs_objects = existent_lfs_objects.where.not(id: project.all_lfs_objects)
project.all_lfs_objects << not_linked_lfs_objects
2018-11-08 19:23:39 +05:30
2019-12-04 20:38:33 +05:30
all_existing_objects += existent_lfs_objects.pluck(:oid)
end
2018-11-08 19:23:39 +05:30
2019-12-04 20:38:33 +05:30
log_lfs_link_results(all_existing_objects.count, iterations)
2018-11-08 19:23:39 +05:30
2019-12-04 20:38:33 +05:30
all_existing_objects
2018-11-08 19:23:39 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
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