2018-11-09 13:39:43 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-15 05:35:00 -04:00
|
|
|
module Gitlab
|
|
|
|
module HashedStorage
|
|
|
|
module RakeHelper
|
|
|
|
def self.batch_size
|
|
|
|
ENV.fetch('BATCH', 200).to_i
|
|
|
|
end
|
|
|
|
|
2018-05-21 12:40:27 -04:00
|
|
|
def self.listing_limit
|
|
|
|
ENV.fetch('LIMIT', 500).to_i
|
|
|
|
end
|
|
|
|
|
2018-06-07 11:40:44 -04:00
|
|
|
def self.range_from
|
|
|
|
ENV['ID_FROM']
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.range_to
|
|
|
|
ENV['ID_TO']
|
|
|
|
end
|
|
|
|
|
2019-06-24 14:31:38 -04:00
|
|
|
def self.using_ranges?
|
|
|
|
!range_from.nil? && !range_to.nil?
|
|
|
|
end
|
|
|
|
|
2018-06-07 11:40:44 -04:00
|
|
|
def self.range_single_item?
|
2019-06-24 14:31:38 -04:00
|
|
|
using_ranges? && range_from == range_to
|
2018-06-07 11:40:44 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2019-01-22 21:40:05 -05:00
|
|
|
def self.project_id_batches_migration(&block)
|
2018-06-07 11:40:44 -04:00
|
|
|
Project.with_unmigrated_storage.in_batches(of: batch_size, start: range_from, finish: range_to) do |relation| # rubocop: disable Cop/InBatches
|
2018-05-15 05:35:00 -04:00
|
|
|
ids = relation.pluck(:id)
|
|
|
|
|
|
|
|
yield ids.min, ids.max
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-05-15 05:35:00 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2019-01-22 21:40:05 -05:00
|
|
|
def self.project_id_batches_rollback(&block)
|
|
|
|
Project.with_storage_feature(:repository).in_batches(of: batch_size, start: range_from, finish: range_to) do |relation| # rubocop: disable Cop/InBatches
|
|
|
|
ids = relation.pluck(:id)
|
|
|
|
|
|
|
|
yield ids.min, ids.max
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
2018-05-15 05:35:00 -04:00
|
|
|
def self.legacy_attachments_relation
|
2019-12-05 22:08:02 -05:00
|
|
|
Upload.inner_join_local_uploads_projects.merge(Project.without_storage_feature(:attachments))
|
2018-05-15 05:35:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.hashed_attachments_relation
|
2019-12-05 22:08:02 -05:00
|
|
|
Upload.inner_join_local_uploads_projects.merge(Project.with_storage_feature(:attachments))
|
2018-05-15 05:35:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.relation_summary(relation_name, relation)
|
|
|
|
relation_count = relation.count
|
|
|
|
$stdout.puts "* Found #{relation_count} #{relation_name}".color(:green)
|
|
|
|
|
|
|
|
relation_count
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.projects_list(relation_name, relation)
|
2018-05-21 22:14:27 -04:00
|
|
|
listing(relation_name, relation.with_route) do |project|
|
2018-05-21 12:40:27 -04:00
|
|
|
$stdout.puts " - #{project.full_path} (id: #{project.id})".color(:red)
|
2021-01-18 10:10:42 -05:00
|
|
|
$stdout.puts " #{project.repository.disk_path}"
|
2018-05-15 05:35:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.attachments_list(relation_name, relation)
|
2018-05-21 22:14:27 -04:00
|
|
|
listing(relation_name, relation) do |upload|
|
|
|
|
$stdout.puts " - #{upload.path} (id: #{upload.id})".color(:red)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-05-21 22:14:27 -04:00
|
|
|
def self.listing(relation_name, relation)
|
2018-05-15 05:35:00 -04:00
|
|
|
relation_count = relation_summary(relation_name, relation)
|
|
|
|
return unless relation_count > 0
|
|
|
|
|
2018-05-21 12:40:27 -04:00
|
|
|
limit = listing_limit
|
2018-05-15 05:35:00 -04:00
|
|
|
|
2018-05-21 22:14:27 -04:00
|
|
|
if relation_count > limit
|
|
|
|
$stdout.puts " ! Displaying first #{limit} #{relation_name}..."
|
|
|
|
end
|
2018-05-15 05:35:00 -04:00
|
|
|
|
2018-05-21 22:14:27 -04:00
|
|
|
relation.find_each(batch_size: batch_size).with_index do |element, index|
|
|
|
|
yield element
|
2018-05-15 05:35:00 -04:00
|
|
|
|
2018-05-21 12:40:27 -04:00
|
|
|
break if index + 1 >= limit
|
2018-05-15 05:35:00 -04:00
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2021-01-18 10:10:42 -05:00
|
|
|
|
|
|
|
def self.prune(relation_name, relation, dry_run: true, root: nil)
|
|
|
|
root ||= '../repositories'
|
|
|
|
|
|
|
|
known_paths = Set.new
|
|
|
|
listing(relation_name, relation) { |p| known_paths << "#{root}/#{p.repository.disk_path}" }
|
|
|
|
|
|
|
|
marked_for_deletion = Set.new(Dir["#{root}/@hashed/*/*/*"])
|
|
|
|
marked_for_deletion.reject! do |path|
|
|
|
|
base = path.gsub(/\.(\w+\.)?git$/, '')
|
|
|
|
known_paths.include?(base)
|
|
|
|
end
|
|
|
|
|
|
|
|
if marked_for_deletion.empty?
|
|
|
|
$stdout.puts "No orphaned directories found. Nothing to do!"
|
|
|
|
else
|
|
|
|
n = marked_for_deletion.size
|
|
|
|
$stdout.puts "Found #{n} orphaned #{'directory'.pluralize(n)}"
|
|
|
|
$stdout.puts "Dry run. (Run again with FORCE=1 to delete). We would have deleted:" if dry_run
|
|
|
|
end
|
|
|
|
|
|
|
|
marked_for_deletion.each do |p|
|
|
|
|
p = Pathname.new(p)
|
|
|
|
if dry_run
|
|
|
|
$stdout.puts " - #{p}"
|
|
|
|
else
|
|
|
|
$stdout.puts "Removing #{p}"
|
|
|
|
p.rmtree
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-05-15 05:35:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|