Commit ddf054fe authored by Patrick Steinhardt's avatar Patrick Steinhardt

repository: Do not fail if repository exists already

The `#create_if_not_exists` function is supposed to create a repository
if it doesn't exist yet. It may happen though that the existence-cache
may pretend a repo doesn't exist yet even though it does, and in that
case we'd call the CreateRepository RPC. This worked alright until now
given that this RPC call is idempotent and simply reinitializes the repo
in case it exists already. But Gitaly is going to change semantics of
the RPC such that it raises an error if the repo exists already, where
it then won't touch the preexisting repo at all.

Prepare for this change by introducing a new `RepositoryExists` error
which gets raised when `#create_repository` returns an AlreadyExists
error. This error is rescued from in `#create_if_not_exists` such that
semantics of this RPC call remain the same.
parent a40a136e
...@@ -1091,6 +1091,13 @@ class Repository ...@@ -1091,6 +1091,13 @@ class Repository
after_create after_create
true true
rescue Gitlab::Git::Repository::RepositoryExists
# We do not want to call `#after_create` given that we didn't create the
# repo, but we obviously have a mismatch between what's in our exists cache
# and actual on-disk state as seen by Gitaly. Let's thus expire our caches.
expire_status_cache
nil
end end
def create_from_bundle(bundle_path) def create_from_bundle(bundle_path)
......
...@@ -20,6 +20,7 @@ module Gitlab ...@@ -20,6 +20,7 @@ module Gitlab
EMPTY_REPOSITORY_CHECKSUM = '0000000000000000000000000000000000000000' EMPTY_REPOSITORY_CHECKSUM = '0000000000000000000000000000000000000000'
NoRepository = Class.new(::Gitlab::Git::BaseError) NoRepository = Class.new(::Gitlab::Git::BaseError)
RepositoryExists = Class.new(::Gitlab::Git::BaseError)
InvalidRepository = Class.new(::Gitlab::Git::BaseError) InvalidRepository = Class.new(::Gitlab::Git::BaseError)
InvalidBlobName = Class.new(::Gitlab::Git::BaseError) InvalidBlobName = Class.new(::Gitlab::Git::BaseError)
InvalidRef = Class.new(::Gitlab::Git::BaseError) InvalidRef = Class.new(::Gitlab::Git::BaseError)
...@@ -101,6 +102,8 @@ module Gitlab ...@@ -101,6 +102,8 @@ module Gitlab
def create_repository def create_repository
wrapped_gitaly_errors do wrapped_gitaly_errors do
gitaly_repository_client.create_repository gitaly_repository_client.create_repository
rescue GRPC::AlreadyExists => e
raise RepositoryExists, e.message
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