Commit f35ff1ea authored by Toon Claes's avatar Toon Claes

Ensure all Routables have a parent

Or otherwise do not try to write repo config.
parent de0cc8e4
......@@ -7,6 +7,8 @@ module Gitlab
# Storing the full project path in the git config allows admins to
# easily identify a project when it is using hashed storage.
module BackfillProjectFullpathInRepoConfig
OrphanedNamespaceError = Class.new(StandardError)
module Storage
# Class that returns the disk path for a project using hashed storage
class HashedProject
......@@ -51,11 +53,15 @@ module Gitlab
end
def build_full_path
if parent && path
parent.full_path + '/' + path
else
path
end
return path unless has_parent?
raise OrphanedNamespaceError if parent.nil?
parent.full_path + '/' + path
end
def has_parent?
read_attribute(association(:parent).reflection.foreign_key)
end
end
......@@ -81,7 +87,9 @@ module Gitlab
include Routable
belongs_to :parent, class_name: "Namespace"
belongs_to :parent, class_name: 'Namespace', inverse_of: 'namespaces'
has_many :projects, inverse_of: :parent
has_many :namespaces, inverse_of: :parent
end
# Project is where the repository (etc.) is stored
......@@ -93,9 +101,8 @@ module Gitlab
FULLPATH_CONFIG_KEY = 'gitlab.fullpath'
belongs_to :namespace
belongs_to :parent, class_name: 'Namespace', foreign_key: :namespace_id, inverse_of: 'projects'
delegate :disk_path, to: :storage
alias_method :parent, :namespace
def add_fullpath_config
entries = { FULLPATH_CONFIG_KEY => full_path }
......@@ -150,14 +157,14 @@ module Gitlab
end
def perform(start_id, end_id)
Project.where(id: start_id..end_id).each do |project|
Project.includes(:parent).where(id: start_id..end_id).each do |project|
safe_perform_one(project)
end
end
def safe_perform_one(project, retry_count = 0)
perform_one(project)
rescue GRPC::NotFound, GRPC::InvalidArgument
rescue GRPC::NotFound, GRPC::InvalidArgument, OrphanedNamespaceError
nil
rescue GRPC::BadStatus
schedule_retry(project, retry_count + 1) if retry_count < MAX_RETRIES
......
......@@ -34,6 +34,12 @@ describe Gitlab::BackgroundMigration::BackfillProjectFullpathInRepoConfig, :migr
it 'returns path containing all parent namespaces' do
expect(project.full_path).to eq('foo/bar/baz')
end
it 'raises OrphanedNamespaceError when any parent namespace does not exist' do
subgroup.update_attribute(:parent_id, namespaces.maximum(:id).succ)
expect { project.full_path }.to raise_error(Gitlab::BackgroundMigration::BackfillProjectFullpathInRepoConfig::OrphanedNamespaceError)
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