Skip to content

Commit a557183

Browse files
authored
fix(backend): upsert annotations (#1508)
1 parent c2b2cfe commit a557183

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

pkg/service/annotation.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ func (svc AnnotationsService) CreateAnnotation(ctx context.Context, params model
3030
tx := svc.db.WithContext(ctx)
3131

3232
// Upsert
33-
if err := tx.Where(model.CreateAnnotation{
34-
AppName: params.AppName, Timestamp: params.Timestamp,
35-
}).Attrs(u).FirstOrCreate(&u).Error; err != nil {
36-
return nil, err
33+
if tx.Where(model.CreateAnnotation{
34+
AppName: params.AppName,
35+
Timestamp: params.Timestamp,
36+
}).Updates(&u).RowsAffected == 0 {
37+
return &u, tx.Create(&u).Error
3738
}
3839

3940
return &u, nil

pkg/service/annotation_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ var _ = Describe("AnnotationsService", func() {
6161
})
6262

6363
It("upserts", func() {
64-
annotation, err := svc.CreateAnnotation(context.Background(), p)
64+
annotation, err := svc.CreateAnnotation(context.Background(), model.CreateAnnotation{
65+
AppName: p.AppName,
66+
Timestamp: p.Timestamp,
67+
Content: "mycontent updated",
68+
})
6569
Expect(err).ToNot(HaveOccurred())
6670
Expect(annotation).ToNot(BeNil())
6771

@@ -70,6 +74,7 @@ var _ = Describe("AnnotationsService", func() {
7074
time.Now().Add(-time.Hour),
7175
time.Now())
7276

77+
Expect(annotations[0].Content).To(Equal("mycontent updated"))
7378
Expect(err).ToNot(HaveOccurred())
7479
Expect(annotations).ToNot(BeEmpty())
7580
Expect(len(annotations)).To(Equal(1))

0 commit comments

Comments
 (0)