From be24a8ad650f0ed49993283b22ba1d1a744fb3e8 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Fri, 3 Jun 2022 13:25:55 +1000 Subject: [PATCH] fix: fix bug where pacticipant with blank name was being created instead of updating existing record --- lib/pact_broker/domain/pacticipant.rb | 1 + lib/pact_broker/pacticipants/repository.rb | 2 +- spec/lib/pact_broker/pacticipants/repository_spec.rb | 4 ++++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/pact_broker/domain/pacticipant.rb b/lib/pact_broker/domain/pacticipant.rb index 013bad7e4..a6fc208bd 100644 --- a/lib/pact_broker/domain/pacticipant.rb +++ b/lib/pact_broker/domain/pacticipant.rb @@ -44,6 +44,7 @@ class Pacticipant < Sequel::Model using PactBroker::StringRefinements plugin :insert_ignore, identifying_columns: [:name] + plugin :upsert, identifying_columns: [:name] plugin :timestamps, update_on_create: true set_primary_key :id diff --git a/lib/pact_broker/pacticipants/repository.rb b/lib/pact_broker/pacticipants/repository.rb index 9fec7b4cf..3256a82fe 100644 --- a/lib/pact_broker/pacticipants/repository.rb +++ b/lib/pact_broker/pacticipants/repository.rb @@ -75,7 +75,7 @@ def replace(pacticipant_name, open_struct_pacticipant) repository_name: open_struct_pacticipant.repository_name, repository_namespace: open_struct_pacticipant.repository_namespace, main_branch: open_struct_pacticipant.main_branch - ).save + ).upsert end def delete(pacticipant) diff --git a/spec/lib/pact_broker/pacticipants/repository_spec.rb b/spec/lib/pact_broker/pacticipants/repository_spec.rb index a858f448b..d054a7cf0 100644 --- a/spec/lib/pact_broker/pacticipants/repository_spec.rb +++ b/spec/lib/pact_broker/pacticipants/repository_spec.rb @@ -62,6 +62,10 @@ module Pacticipants subject { Repository.new.replace("Bar", OpenStruct.new(main_branch: "bar", repository_url: "new_url")) } + it "does not create a new record" do + expect { subject }.to_not change { PactBroker::Domain::Pacticipant.count } + end + it "replaces the pacticipant" do expect(subject.name).to eq "Bar" expect(subject.main_branch).to eq "bar"