Commit 1e46fd73 authored by Micaël Bergeron's avatar Micaël Bergeron

first iteration of the gitlab:uploads:migrate task

parent e77fc3f5
......@@ -36,8 +36,8 @@ class Upload < ActiveRecord::Base
self.checksum = self.class.hexdigest(absolute_path)
end
def build_uploader
uploader_class.new(model, mount_point, **uploader_context).tap do |uploader|
def build_uploader(mounted_as = nil)
uploader_class.new(model, mounted_as || mount_point).tap do |uploader|
uploader.upload = self
uploader.retrieve_from_store!(identifier)
end
......
......@@ -19,6 +19,77 @@ namespace :gitlab do
puts 'Done!'
end
desc 'GitLab | Uploads | Migrate the uploaded files to object storage'
task :migrate, [:uploader_class, :model_class, :mounted_as] => :environment do |task, args|
MIGRATE_TO_STORE = ObjectStorage::Store::REMOTE
class MigrationResult
attr_reader :upload
attr_accessor :error
def initialize(upload, error = nil)
@upload, @error = upload, error
end
def success?
error.nil?
end
def to_s
success? ? "Migration sucessful." : "Error while migrating #{upload.id}: #{error.message}"
end
end
uploader_class = args.uploader_class.constantize
model_class = args.model_class.constantize
mounted_as = args.mounted_as&.gsub(':', '')&.to_sym
global_results = []
Upload.preload(:model)
.where.not(store: MIGRATE_TO_STORE)
.where(uploader: uploader_class.to_s,
model_type: model_class.to_s)
.in_batches(of: batch_size) do |batch|
results = migrate(build_uploaders(batch, mounted_as))
report(results)
global_results.concat(results)
end
puts "\n === Migration summary ==="
report(global_results)
end
def report(results)
results = results.group_by(&:success?)
success, errors = [
results.fetch(true, []),
results.fetch(false, [])
]
batch_color = errors.count == 0 ? :green : :red
puts "Migrated #{success.count}/#{success.count + errors.count} files.".color(batch_color)
errors.each { |e| puts("\t#{e}").color(:red) }
end
def build_uploaders(uploads, mounted_as)
uploads.map { |upload| upload.build_uploader(mounted_as) }
end
def migrate(uploaders)
uploaders.map do |uploader|
result = MigrationResult.new(uploader.upload)
begin
uploader.migrate!(MIGRATE_TO_STORE)
result
rescue CarrierWave::UploadError => e
result.error = e
result
end
end
end
def batch_size
ENV.fetch('BATCH', 200).to_i
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment