diff options
Diffstat (limited to 'app/services/projects/hashed_storage/migrate_attachments_service.rb')
-rw-r--r-- | app/services/projects/hashed_storage/migrate_attachments_service.rb | 37 |
1 files changed, 12 insertions, 25 deletions
diff --git a/app/services/projects/hashed_storage/migrate_attachments_service.rb b/app/services/projects/hashed_storage/migrate_attachments_service.rb index 58a47da2fcb..68b9a72661c 100644 --- a/app/services/projects/hashed_storage/migrate_attachments_service.rb +++ b/app/services/projects/hashed_storage/migrate_attachments_service.rb @@ -3,51 +3,38 @@ module Projects class MigrateAttachmentsService < BaseService attr_reader :logger - BATCH_SIZE = 500 - def initialize(project, logger = nil) @project = project @logger = logger || Rails.logger end def execute - project_before_migration = project.dup + old_path = FileUploader.dynamic_path_segment(project) project.storage_version = ::Project::HASHED_STORAGE_FEATURES[:attachments] + new_path = FileUploader.dynamic_path_segment(project) - project.uploads.find_each(batch_size: BATCH_SIZE) do |upload| - old_path = attachments_path(project_before_migration, upload) - new_path = attachments_path(project, upload) - move_attachment(old_path, new_path) - end - + move_folder!(old_path, new_path) project.save! end private - def attachments_path(project, upload) - File.join( - FileUploader.dynamic_path_segment(project), - upload.path - ) - end - - def move_attachment(old_path, new_path) - unless File.file?(old_path) - logger.error("Failed to migrate attachment from '#{old_path}' to '#{new_path}', source file doesn't exist (PROJECT_ID=#{project.id})") + def move_folder!(old_path, new_path) + unless File.exist?(old_path) + logger.info("Skipped attachments migration from '#{old_path}' to '#{new_path}', source path doesn't exist (PROJECT_ID=#{project.id})") return end - # Create attachments folder if doesn't exist yet - FileUtils.mkdir_p(File.dirname(new_path)) unless Dir.exist?(File.dirname(new_path)) - - if File.file?(new_path) - logger.info("Skipped attachment migration from '#{old_path}' to '#{new_path}', target file already exist (PROJECT_ID=#{project.id})") + if File.exist?(new_path) + logger.error("Cannot migrate attachments from '#{old_path}' to '#{new_path}', target path already exist (PROJECT_ID=#{project.id})") return end + # Create hashed storage base path folder + FileUtils.mkdir_p(File.expand_path('..', new_path)) + FileUtils.mv(old_path, new_path) - logger.info("Migrated project attachment from '#{old_path}' to '#{new_path}' (PROJECT_ID=#{project.id})") + logger.info("Migrated project attachments from '#{old_path}' to '#{new_path}' (PROJECT_ID=#{project.id})") end end end |