diff options
author | Gabriel Mazetto <brodock@gmail.com> | 2017-11-15 16:42:38 +0300 |
---|---|---|
committer | Gabriel Mazetto <brodock@gmail.com> | 2017-11-23 16:19:36 +0300 |
commit | 4af26c1c65e4cc1d18a37acbc2af6ae29e91b336 (patch) | |
tree | 28b8b35469e6fb41c3c914e051e433b51d26005c /app/services | |
parent | 0a4d55a1c9f58f383f23b8f60bbe1acba1b8511c (diff) |
WIP Attachments migration
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/projects/hashed_storage/migrate_attachments_service.rb | 54 | ||||
-rw-r--r-- | app/services/projects/hashed_storage_migration_service.rb | 7 |
2 files changed, 60 insertions, 1 deletions
diff --git a/app/services/projects/hashed_storage/migrate_attachments_service.rb b/app/services/projects/hashed_storage/migrate_attachments_service.rb new file mode 100644 index 00000000000..58a47da2fcb --- /dev/null +++ b/app/services/projects/hashed_storage/migrate_attachments_service.rb @@ -0,0 +1,54 @@ +module Projects + module HashedStorage + 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 + project.storage_version = ::Project::HASHED_STORAGE_FEATURES[:attachments] + + 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 + + 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})") + 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})") + return + end + + FileUtils.mv(old_path, new_path) + logger.info("Migrated project attachment from '#{old_path}' to '#{new_path}' (PROJECT_ID=#{project.id})") + end + end + end +end diff --git a/app/services/projects/hashed_storage_migration_service.rb b/app/services/projects/hashed_storage_migration_service.rb index b61f71cf9a0..662702c1db5 100644 --- a/app/services/projects/hashed_storage_migration_service.rb +++ b/app/services/projects/hashed_storage_migration_service.rb @@ -1,7 +1,7 @@ module Projects class HashedStorageMigrationService < BaseService attr_reader :logger - + def initialize(project, logger = nil) @project = project @logger = logger || Rails.logger @@ -12,6 +12,11 @@ module Projects unless project.hashed_storage?(:repository) return unless HashedStorage::MigrateRepositoryService.new(project, logger).execute end + + # Migrate attachments from Legacy to Hashed Storage + unless project.hashed_storage?(:attachments) + HashedStorage::MigrateAttachmentsService.new(project, logger).execute + end end end end |