From e17ebf120522436fa8799e9ad80682db686e4391 Mon Sep 17 00:00:00 2001 From: "grafana-delivery-bot[bot]" <132647405+grafana-delivery-bot[bot]@users.noreply.github.com> Date: Thu, 13 Jul 2023 10:33:43 +0200 Subject: [PATCH] [v10.0.x] Annotations: Fix database lock while updating annotations (#71207) Annotations: Fix database lock while updating annotations (#71199) (cherry picked from commit 56f52dc97e4932763d313832436ccefdcbf01f6f) Co-authored-by: Emil Tullstedt --- .../annotations/annotationsimpl/xorm_store.go | 8 ++++- .../annotationsimpl/xorm_store_test.go | 29 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/pkg/services/annotations/annotationsimpl/xorm_store.go b/pkg/services/annotations/annotationsimpl/xorm_store.go index 06e8e79b193..17b49489e70 100644 --- a/pkg/services/annotations/annotationsimpl/xorm_store.go +++ b/pkg/services/annotations/annotationsimpl/xorm_store.go @@ -143,7 +143,13 @@ func (r *xormRepositoryImpl) synchronizeTags(ctx context.Context, item *annotati } func (r *xormRepositoryImpl) Update(ctx context.Context, item *annotations.Item) error { - return r.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error { + return r.db.InTransaction(ctx, func(ctx context.Context) error { + return r.update(ctx, item) + }) +} + +func (r *xormRepositoryImpl) update(ctx context.Context, item *annotations.Item) error { + return r.db.WithDbSession(ctx, func(sess *db.Session) error { var ( isExist bool err error diff --git a/pkg/services/annotations/annotationsimpl/xorm_store_test.go b/pkg/services/annotations/annotationsimpl/xorm_store_test.go index 4bbd98b907c..ba9a3931fe8 100644 --- a/pkg/services/annotations/annotationsimpl/xorm_store_test.go +++ b/pkg/services/annotations/annotationsimpl/xorm_store_test.go @@ -368,6 +368,35 @@ func TestIntegrationAnnotations(t *testing.T) { assert.Greater(t, items[0].Updated, items[0].Created) }) + t.Run("Can update annotation with additional tags", func(t *testing.T) { + query := &annotations.ItemQuery{ + OrgID: 1, + DashboardID: 1, + From: 0, + To: 15, + SignedInUser: testUser, + } + items, err := repo.Get(context.Background(), query) + require.NoError(t, err) + + annotationId := items[0].ID + err = repo.Update(context.Background(), &annotations.Item{ + ID: annotationId, + OrgID: 1, + Text: "something new", + Tags: []string{"newtag1", "newtag3"}, + }) + require.NoError(t, err) + + items, err = repo.Get(context.Background(), query) + require.NoError(t, err) + + assert.Equal(t, annotationId, items[0].ID) + assert.Equal(t, []string{"newtag1", "newtag3"}, items[0].Tags) + assert.Equal(t, "something new", items[0].Text) + assert.Greater(t, items[0].Updated, items[0].Created) + }) + t.Run("Can update annotations with data", func(t *testing.T) { query := &annotations.ItemQuery{ OrgID: 1,