Commit f8ae33ca authored by Steve Abrams's avatar Steve Abrams

Fix race condition on container repository create

Return the existing container repository if
creation fails due to invalid record or
uniqueness constraint error.

Changelog: fixed
parent 16c1637e
...@@ -154,6 +154,8 @@ class ContainerRepository < ApplicationRecord ...@@ -154,6 +154,8 @@ class ContainerRepository < ApplicationRecord
def self.create_from_path!(path) def self.create_from_path!(path)
build_from_path(path).tap(&:save!) build_from_path(path).tap(&:save!)
rescue ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid
self.find_by_path!(path)
end end
def self.build_root_repository(project) def self.build_root_repository(project)
......
...@@ -281,6 +281,16 @@ RSpec.describe ContainerRepository do ...@@ -281,6 +281,16 @@ RSpec.describe ContainerRepository do
expect(repository.name).to be_empty expect(repository.name).to be_empty
end end
end end
context 'when repository already exists' do
let(:path) { project.full_path + '/some/image' }
it 'returns the existing repository' do
container_repository = create(:container_repository, project: project, name: 'some/image')
expect(repository.id).to eq(container_repository.id)
end
end
end end
describe '.build_root_repository' do describe '.build_root_repository' 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