From 2515fc50811328f43cf9cd21a4ac831d320f9562 Mon Sep 17 00:00:00 2001 From: soxx Date: Fri, 21 Jun 2024 15:08:42 +0200 Subject: [PATCH] fix(postgres): check if source exists before creating Signed-off-by: soxx --- internal/postgres/source.go | 2 +- internal/postgres/source_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/postgres/source.go b/internal/postgres/source.go index 1b013da..0e60a5e 100644 --- a/internal/postgres/source.go +++ b/internal/postgres/source.go @@ -16,7 +16,7 @@ func CreateSourceNode(ctx context.Context, db *gorm.DB, anthroveSource *pgModels return fmt.Errorf("anthroveSource domain is required") } - result := db.WithContext(ctx).Create(anthroveSource) + result := db.WithContext(ctx).Where(pgModels.Source{Domain: anthroveSource.Domain}).FirstOrCreate(anthroveSource) if result.Error != nil { return result.Error diff --git a/internal/postgres/source_test.go b/internal/postgres/source_test.go index 2bde71b..d0514c2 100644 --- a/internal/postgres/source_test.go +++ b/internal/postgres/source_test.go @@ -65,7 +65,7 @@ func TestCreateSourceNode(t *testing.T) { db: gormDB, anthroveSource: validAnthroveSource, }, - wantErr: true, + wantErr: false, }, } for _, tt := range tests { @@ -249,7 +249,7 @@ func checkSourceNode(got *pgModels.Source, want *pgModels.Source) bool { if want == nil && got == nil { return true } - + if got.Domain != want.Domain { return false }