Commit b1907694 authored by Gabriel Mazetto's avatar Gabriel Mazetto

Moved renaming operations to storage layer abstraction

When project storage_version is `2` means attachments are using
hashed storage.
parent dfd6c3f8
...@@ -26,7 +26,10 @@ class Project < ActiveRecord::Base ...@@ -26,7 +26,10 @@ class Project < ActiveRecord::Base
NUMBER_OF_PERMITTED_BOARDS = 1 NUMBER_OF_PERMITTED_BOARDS = 1
UNKNOWN_IMPORT_URL = 'http://unknown.git'.freeze UNKNOWN_IMPORT_URL = 'http://unknown.git'.freeze
LATEST_STORAGE_VERSION = 1 # Hashed Storage versions handle rolling out new storage to project and dependents models
# 1: repository
# 2: attachments
LATEST_STORAGE_VERSION = 2
cache_markdown_field :description, pipeline: :description cache_markdown_field :description, pipeline: :description
...@@ -1384,7 +1387,7 @@ class Project < ActiveRecord::Base ...@@ -1384,7 +1387,7 @@ class Project < ActiveRecord::Base
if storage.rename_repo if storage.rename_repo
Gitlab::AppLogger.info "Project was renamed: #{full_path_was} -> #{new_full_path}" Gitlab::AppLogger.info "Project was renamed: #{full_path_was} -> #{new_full_path}"
rename_repo_notify! rename_repo_notify!
after_rename_repo storage.after_rename_repo
else else
Rails.logger.error "Repository could not be renamed: #{full_path_was} -> #{new_full_path}" Rails.logger.error "Repository could not be renamed: #{full_path_was} -> #{new_full_path}"
...@@ -1404,13 +1407,6 @@ class Project < ActiveRecord::Base ...@@ -1404,13 +1407,6 @@ class Project < ActiveRecord::Base
reload_repository! reload_repository!
end end
def after_rename_repo
path_before_change = previous_changes['path'].first
Gitlab::UploadsTransfer.new.rename_project(path_before_change, self.path, namespace.full_path)
Gitlab::PagesTransfer.new.rename_project(path_before_change, self.path, namespace.full_path)
end
def running_or_pending_build_count(force: false) def running_or_pending_build_count(force: false)
Rails.cache.fetch(['projects', id, 'running_or_pending_build_count'], force: force) do Rails.cache.fetch(['projects', id, 'running_or_pending_build_count'], force: force) do
builds.running_or_pending.count(:all) builds.running_or_pending.count(:all)
......
...@@ -32,6 +32,19 @@ module Storage ...@@ -32,6 +32,19 @@ module Storage
true true
end end
def after_rename_repo
path_before_change = project.previous_changes['path'].first
# We need to check if project had been rolled out to move resource to hashed storage or not and decide
# if we need execute any take action or no-op.
unless project.storage_version >= 2
Gitlab::UploadsTransfer.new.rename_project(path_before_change, project.path, project.namespace.full_path)
end
Gitlab::PagesTransfer.new.rename_project(path_before_change, project.path, project.namespace.full_path)
end
private private
# Generates the hash for the project path and name on disk # Generates the hash for the project path and name on disk
......
...@@ -47,5 +47,12 @@ module Storage ...@@ -47,5 +47,12 @@ module Storage
false false
end end
def after_rename_repo
path_before_change = project.previous_changes['path'].first
Gitlab::UploadsTransfer.new.rename_project(path_before_change, project.path, project.namespace.full_path)
Gitlab::PagesTransfer.new.rename_project(path_before_change, project.path, project.namespace.full_path)
end
end end
end end
...@@ -2452,6 +2452,7 @@ describe Project do ...@@ -2452,6 +2452,7 @@ describe Project do
context 'legacy storage' do context 'legacy storage' do
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let(:gitlab_shell) { Gitlab::Shell.new } let(:gitlab_shell) { Gitlab::Shell.new }
let(:project_storage) { project.send(:storage) }
before do before do
allow(project).to receive(:gitlab_shell).and_return(gitlab_shell) allow(project).to receive(:gitlab_shell).and_return(gitlab_shell)
...@@ -2546,6 +2547,30 @@ describe Project do ...@@ -2546,6 +2547,30 @@ describe Project do
it { expect { subject }.to raise_error(StandardError) } it { expect { subject }.to raise_error(StandardError) }
end end
context 'gitlab pages' do
before do
expect(project_storage).to receive(:rename_repo) { true }
end
it 'moves pages folder to new location' do
expect_any_instance_of(Gitlab::PagesTransfer).to receive(:rename_project)
project.rename_repo
end
end
context 'attachments' do
before do
expect(project_storage).to receive(:rename_repo) { true }
end
it 'moves uploads folder to new location' do
expect_any_instance_of(Gitlab::UploadsTransfer).to receive(:rename_project)
project.rename_repo
end
end
end end
describe '#pages_path' do describe '#pages_path' do
...@@ -2649,10 +2674,6 @@ describe Project do ...@@ -2649,10 +2674,6 @@ describe Project do
.to receive(:execute_hooks_for) .to receive(:execute_hooks_for)
.with(project, :rename) .with(project, :rename)
expect_any_instance_of(Gitlab::UploadsTransfer)
.to receive(:rename_project)
.with('foo', project.path, project.namespace.full_path)
expect(project).to receive(:expire_caches_before_rename) expect(project).to receive(:expire_caches_before_rename)
expect(project).to receive(:expires_full_path_cache) expect(project).to receive(:expires_full_path_cache)
...@@ -2673,6 +2694,32 @@ describe Project do ...@@ -2673,6 +2694,32 @@ describe Project do
it { expect { subject }.to raise_error(StandardError) } it { expect { subject }.to raise_error(StandardError) }
end end
context 'gitlab pages' do
it 'moves pages folder to new location' do
expect_any_instance_of(Gitlab::PagesTransfer).to receive(:rename_project)
project.rename_repo
end
end
context 'attachments' do
it 'keeps uploads folder location unchanged' do
expect_any_instance_of(Gitlab::UploadsTransfer).not_to receive(:rename_project)
project.rename_repo
end
context 'when not rolled out' do
let(:project) { create(:project, :repository, storage_version: 1) }
it 'moves pages folder to new location' do
expect_any_instance_of(Gitlab::UploadsTransfer).to receive(:rename_project)
project.rename_repo
end
end
end
end end
describe '#pages_path' do describe '#pages_path' do
......
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