debian-mirror-gitlab/app/services/projects/hashed_storage/migrate_repository_service.rb

82 lines
2.5 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
module Projects
module HashedStorage
class MigrateRepositoryService < BaseService
include Gitlab::ShellAdapter
2018-11-18 11:00:15 +05:30
attr_reader :old_disk_path, :new_disk_path, :old_wiki_disk_path, :old_storage_version, :logger, :move_wiki
2018-03-17 18:26:18 +05:30
2018-11-18 11:00:15 +05:30
def initialize(project, old_disk_path, logger: nil)
2018-03-17 18:26:18 +05:30
@project = project
@logger = logger || Rails.logger
2018-11-18 11:00:15 +05:30
@old_disk_path = old_disk_path
@old_wiki_disk_path = "#{old_disk_path}.wiki"
@move_wiki = has_wiki?
2018-03-17 18:26:18 +05:30
end
def execute
@old_storage_version = project.storage_version
project.storage_version = ::Project::HASHED_STORAGE_FEATURES[:repository]
project.ensure_storage_path_exists
@new_disk_path = project.disk_path
2018-11-18 11:00:15 +05:30
result = move_repository(old_disk_path, new_disk_path)
2018-03-17 18:26:18 +05:30
2018-11-18 11:00:15 +05:30
if move_wiki
result &&= move_repository("#{old_wiki_disk_path}", "#{new_disk_path}.wiki")
2018-03-17 18:26:18 +05:30
end
if result
project.write_repository_config
else
rollback_folder_move
project.storage_version = nil
end
project.repository_read_only = false
project.save!
if result && block_given?
yield
end
result
end
private
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-11-18 11:00:15 +05:30
def has_wiki?
gitlab_shell.exists?(project.repository_storage, "#{old_wiki_disk_path}.git")
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-11-18 11:00:15 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
def move_repository(from_name, to_name)
2018-10-15 14:42:47 +05:30
from_exists = gitlab_shell.exists?(project.repository_storage, "#{from_name}.git")
to_exists = gitlab_shell.exists?(project.repository_storage, "#{to_name}.git")
2018-03-17 18:26:18 +05:30
# If we don't find the repository on either original or target we should log that as it could be an issue if the
# project was not originally empty.
if !from_exists && !to_exists
logger.warn "Can't find a repository on either source or target paths for #{project.full_path} (ID=#{project.id}) ..."
return false
elsif !from_exists
# Repository have been moved already.
return true
end
2018-10-15 14:42:47 +05:30
gitlab_shell.mv_repository(project.repository_storage, from_name, to_name)
2018-03-17 18:26:18 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
def rollback_folder_move
2018-11-18 11:00:15 +05:30
move_repository(new_disk_path, old_disk_path)
move_repository("#{new_disk_path}.wiki", old_wiki_disk_path)
2018-03-17 18:26:18 +05:30
end
end
end
end