diff --git a/lib/pact_broker/pacticipants/repository.rb b/lib/pact_broker/pacticipants/repository.rb index 1b363cf10..bbb42d1af 100644 --- a/lib/pact_broker/pacticipants/repository.rb +++ b/lib/pact_broker/pacticipants/repository.rb @@ -42,7 +42,13 @@ def find_by_name_or_create name end def create args - PactBroker::Domain::Pacticipant.new(name: args[:name], repository_url: args[:repository_url]).save(raise_on_save_failure: true) + id = PactBroker::Domain::Pacticipant.dataset.insert_ignore.insert( + name: args[:name], + repository_url: args[:repository_url], + created_at: Sequel.datetime_class.now, + updated_at: Sequel.datetime_class.now + ) + PactBroker::Domain::Pacticipant.find(id: id) end def pacticipant_names diff --git a/spec/lib/pact_broker/pacticipants/repository_spec.rb b/spec/lib/pact_broker/pacticipants/repository_spec.rb index 80fedd00c..563ac8d8a 100644 --- a/spec/lib/pact_broker/pacticipants/repository_spec.rb +++ b/spec/lib/pact_broker/pacticipants/repository_spec.rb @@ -5,6 +5,39 @@ module PactBroker module Pacticipants describe Repository do + describe "#create" do + let(:repository) { Repository.new } + + subject { repository.create(name: "Foo") } + + context "when the pacticipant does not already exist" do + before do + TestDataBuilder.new.create_pacticipant("Bar") + end + + subject { repository.create(name: "Foo") } + + it "returns the new pacticipant" do + expect(subject).to be_a(PactBroker::Domain::Pacticipant) + expect(subject.name).to eq "Foo" + end + end + + context "when a race condition occurs and the pacticipant was already created by another request" do + before do + TestDataBuilder.new.create_pacticipant("Foo") + end + + it "does not raise an error" do + subject + end + + it "returns the existing pacticipant" do + expect(subject).to be_a(PactBroker::Domain::Pacticipant) + expect(subject.name).to eq "Foo" + end + end + end describe "#find" do before do