From 6a4534b62fad51e5e9da0d132ed64626ffdc2d10 Mon Sep 17 00:00:00 2001 From: Gabriel Mazetto Date: Tue, 31 Oct 2017 02:00:40 +0100 Subject: [PATCH] Code Style changes and `hashed_storage?` now receives optional feature --- app/models/project.rb | 28 ++++++++++++++++++++++++---- app/models/storage/hashed_project.rb | 13 ------------- app/models/storage/legacy_project.rb | 7 ------- spec/models/project_spec.rb | 18 ++++++++++++++++-- 4 files changed, 40 insertions(+), 26 deletions(-) diff --git a/app/models/project.rb b/app/models/project.rb index d9bd6362e70..a43d1a7f484 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -26,10 +26,15 @@ class Project < ActiveRecord::Base NUMBER_OF_PERMITTED_BOARDS = 1 UNKNOWN_IMPORT_URL = 'http://unknown.git'.freeze - # Hashed Storage versions handle rolling out new storage to project and dependents models + # Hashed Storage versions handle rolling out new storage to project and dependents models: + # nil: legacy # 1: repository # 2: attachments LATEST_STORAGE_VERSION = 2 + HASHED_STORAGE_FEATURES = { + repository: 1, + attachments: 2 + }.freeze cache_markdown_field :description, pipeline: :description @@ -1387,7 +1392,7 @@ class Project < ActiveRecord::Base if storage.rename_repo Gitlab::AppLogger.info "Project was renamed: #{full_path_was} -> #{new_full_path}" rename_repo_notify! - storage.after_rename_repo + after_rename_repo else Rails.logger.error "Repository could not be renamed: #{full_path_was} -> #{new_full_path}" @@ -1397,6 +1402,19 @@ class Project < ActiveRecord::Base end end + def after_rename_repo + path_before_change = 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 hashed_storage?(:attachments) + Gitlab::UploadsTransfer.new.rename_project(path_before_change, self.path, namespace.full_path) + end + + Gitlab::PagesTransfer.new.rename_project(path_before_change, self.path, namespace.full_path) + end + def rename_repo_notify! send_move_instructions(full_path_was) expires_full_path_cache @@ -1596,8 +1614,10 @@ class Project < ActiveRecord::Base [nil, 0].include?(self.storage_version) end - def hashed_storage? - self.storage_version && self.storage_version >= 1 + def hashed_storage?(feature=:repository) + raise ArgumentError, "Invalid feature" unless HASHED_STORAGE_FEATURES.include?(feature) + + self.storage_version && self.storage_version >= HASHED_STORAGE_FEATURES[feature] end def renamed? diff --git a/app/models/storage/hashed_project.rb b/app/models/storage/hashed_project.rb index 6be365166c4..f025f40994e 100644 --- a/app/models/storage/hashed_project.rb +++ b/app/models/storage/hashed_project.rb @@ -32,19 +32,6 @@ module Storage true 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 # Generates the hash for the project path and name on disk diff --git a/app/models/storage/legacy_project.rb b/app/models/storage/legacy_project.rb index 6e7dce8d25a..9d9e5e1d352 100644 --- a/app/models/storage/legacy_project.rb +++ b/app/models/storage/legacy_project.rb @@ -47,12 +47,5 @@ module Storage false 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 diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index d80af378140..fb5d0f9db9c 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2630,8 +2630,22 @@ describe Project do end describe '#hashed_storage?' do - it 'returns true' do - expect(project.hashed_storage?).to be_truthy + context 'without specifying feature' do + it 'returns true' do + expect(project.hashed_storage?).to be_truthy + end + end + + context 'specifying feature' do + it 'returns true if rolled out' do + expect(project.hashed_storage?(:attachments)).to be_truthy + end + + it 'returns false when not rolled out yet' do + project.storage_version = 1 + + expect(project.hashed_storage?(:attachments)).to be_falsey + end end end