Migrate design repositories to Hashed Storage

Handle design repositories when moving existing projects
to Hashed Storage.
parent 161cacae
......@@ -61,3 +61,5 @@ module Projects
end
end
end
Projects::HashedStorage::BaseRepositoryService.prepend_if_ee('EE::Projects::HashedStorage::BaseRepositoryService')
# frozen_string_literal: true
module EE
module Projects
module HashedStorage
module BaseRepositoryService
extend ::Gitlab::Utils::Override
attr_reader :move_design
override :initialize
def initialize(project:, old_disk_path:, logger: nil)
super
@move_design = has_design?
end
protected
def has_design?
gitlab_shell.repository_exists?(project.repository_storage, "#{old_disk_path}.design.git")
end
override :rollback_folder_move
def rollback_folder_move
super
if move_design
move_repository("#{new_disk_path}.design", "#{old_disk_path}.design")
end
end
end
end
end
end
......@@ -17,6 +17,19 @@ module EE
).create!
end
end
private
override :move_repositories
def move_repositories
result = super
if move_design
result &&= move_repository("#{old_disk_path}.design", "#{new_disk_path}.design")
end
result
end
end
end
end
......
......@@ -5,45 +5,107 @@ require 'spec_helper'
describe Projects::HashedStorage::MigrateRepositoryService do
include EE::GeoHelpers
let(:project) { create(:project, :empty_repo, :wiki_repo, :legacy_storage) }
let(:gitlab_shell) { Gitlab::Shell.new }
let(:project) { create(:project, :empty_repo, :wiki_repo, :design_repo, :legacy_storage) }
let(:legacy_storage) { Storage::LegacyProject.new(project) }
let(:hashed_storage) { Storage::HashedProject.new(project) }
let(:old_disk_path) { legacy_storage.disk_path }
let(:new_disk_path) { hashed_storage.disk_path }
subject { described_class.new(project: project, old_disk_path: legacy_storage.disk_path) }
subject(:service) { described_class.new(project: project, old_disk_path: old_disk_path) }
describe '#execute' do
set(:primary) { create(:geo_node, :primary) }
set(:secondary) { create(:geo_node) }
context 'when a project has a design repository' do
before do
allow(service).to receive(:gitlab_shell) { gitlab_shell }
end
before do
TestEnv.clean_test_path
stub_current_geo_node(primary)
end
context 'when succeeds' do
it 'renames project, wiki and design repositories' do
service.execute
expect(gitlab_shell.repository_exists?(project.repository_storage, "#{new_disk_path}.git")).to be_truthy
expect(gitlab_shell.repository_exists?(project.repository_storage, "#{new_disk_path}.wiki.git")).to be_truthy
expect(gitlab_shell.repository_exists?(project.repository_storage, "#{new_disk_path}.design.git")).to be_truthy
end
it 'move operation is called for each repository' do
expect_move_repository(old_disk_path, new_disk_path)
expect_move_repository("#{old_disk_path}.wiki", "#{new_disk_path}.wiki")
expect_move_repository("#{old_disk_path}.design", "#{new_disk_path}.design")
service.execute
end
end
context 'when one move fails' do
it 'rollsback repositories to original name' do
allow(service).to receive(:move_repository).and_call_original
allow(service).to receive(:move_repository).with(old_disk_path, new_disk_path).once { false } # will disable first move only
expect(service).to receive(:rollback_folder_move).and_call_original
service.execute
it 'creates a Geo::HashedStorageMigratedEvent on success' do
expect { subject.execute }.to change(Geo::EventLog, :count).by(1)
expect(gitlab_shell.repository_exists?(project.repository_storage, "#{new_disk_path}.git")).to be_falsey
expect(gitlab_shell.repository_exists?(project.repository_storage, "#{new_disk_path}.wiki.git")).to be_falsey
expect(gitlab_shell.repository_exists?(project.repository_storage, "#{new_disk_path}.design.git")).to be_falsey
expect(project.repository_read_only?).to be_falsey
end
event = Geo::EventLog.first.event
context 'when rollback fails' do
before do
gitlab_shell.mv_repository(project.repository_storage, old_disk_path, new_disk_path)
end
expect(event).to be_a(Geo::HashedStorageMigratedEvent)
expect(event).to have_attributes(
old_storage_version: nil,
new_storage_version: ::Project::HASHED_STORAGE_FEATURES[:repository],
old_disk_path: legacy_storage.disk_path,
new_disk_path: hashed_storage.disk_path,
old_wiki_disk_path: legacy_storage.disk_path + '.wiki',
new_wiki_disk_path: hashed_storage.disk_path + '.wiki'
)
it 'does not try to move nil repository over existing' do
expect(gitlab_shell).not_to receive(:mv_repository).with(project.repository_storage, old_disk_path, new_disk_path)
expect_move_repository("#{old_disk_path}.wiki", "#{new_disk_path}.wiki")
expect_move_repository("#{old_disk_path}.design", "#{new_disk_path}.design")
service.execute
end
end
end
def expect_move_repository(from_name, to_name)
expect(gitlab_shell).to receive(:mv_repository).with(project.repository_storage, from_name, to_name).and_call_original
end
end
it 'does not create a Geo event on failure' do
from_name = project.disk_path
to_name = hashed_storage.disk_path
context 'when running on a Geo primary node' do
let_it_be(:primary) { create(:geo_node, :primary) }
let_it_be(:secondary) { create(:geo_node) }
before do
stub_current_geo_node(primary)
end
it 'creates a Geo::HashedStorageMigratedEvent on success' do
expect { service.execute }.to change(Geo::EventLog, :count).by(1)
event = Geo::EventLog.first.event
expect(event).to be_a(Geo::HashedStorageMigratedEvent)
expect(event).to have_attributes(
old_storage_version: nil,
new_storage_version: ::Project::HASHED_STORAGE_FEATURES[:repository],
old_disk_path: legacy_storage.disk_path,
new_disk_path: hashed_storage.disk_path,
old_wiki_disk_path: legacy_storage.disk_path + '.wiki',
new_wiki_disk_path: hashed_storage.disk_path + '.wiki'
)
end
it 'does not create a Geo event on failure' do
from_name = project.disk_path
to_name = hashed_storage.disk_path
allow(subject).to receive(:move_repository).and_call_original
allow(subject).to receive(:move_repository).with(from_name, to_name).once { false } # will disable first move only
allow(service).to receive(:move_repository).and_call_original
allow(service).to receive(:move_repository).with(from_name, to_name).once { false } # will disable first move only
expect { subject.execute }.not_to change { Geo::EventLog.count }
expect { service.execute }.not_to change { Geo::EventLog.count }
end
end
end
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