diff --git a/pkg/api/annotations.go b/pkg/api/annotations.go index 8647fbf325c..5221332c741 100644 --- a/pkg/api/annotations.go +++ b/pkg/api/annotations.go @@ -341,7 +341,7 @@ func (hs *HTTPServer) MassDeleteAnnotations(c *models.ReqContext) response.Respo } } - err = repo.Delete(deleteParams) + err = repo.Delete(c.Req.Context(), deleteParams) if err != nil { return response.Error(500, "Failed to delete annotations", err) @@ -376,7 +376,7 @@ func (hs *HTTPServer) DeleteAnnotationByID(c *models.ReqContext) response.Respon return dashboardGuardianResponse(err) } - err = repo.Delete(&annotations.DeleteParams{ + err = repo.Delete(c.Req.Context(), &annotations.DeleteParams{ OrgId: c.OrgId, Id: annotationID, }) @@ -422,7 +422,7 @@ func (hs *HTTPServer) GetAnnotationTags(c *models.ReqContext) response.Response } repo := annotations.GetRepository() - result, err := repo.FindTags(query) + result, err := repo.FindTags(c.Req.Context(), query) if err != nil { return response.Error(500, "Failed to find annotation tags", err) } diff --git a/pkg/api/annotations_test.go b/pkg/api/annotations_test.go index 36b911296fa..a8a5835a4b7 100644 --- a/pkg/api/annotations_test.go +++ b/pkg/api/annotations_test.go @@ -247,7 +247,7 @@ func NewFakeAnnotationsRepo() *fakeAnnotationsRepo { } } -func (repo *fakeAnnotationsRepo) Delete(params *annotations.DeleteParams) error { +func (repo *fakeAnnotationsRepo) Delete(_ context.Context, params *annotations.DeleteParams) error { if params.Id != 0 { delete(repo.annotations, params.Id) } else { @@ -277,7 +277,7 @@ func (repo *fakeAnnotationsRepo) Find(_ context.Context, query *annotations.Item annotations := []*annotations.ItemDTO{{Id: 1, DashboardId: 0}} return annotations, nil } -func (repo *fakeAnnotationsRepo) FindTags(query *annotations.TagsQuery) (annotations.FindTagsResult, error) { +func (repo *fakeAnnotationsRepo) FindTags(_ context.Context, query *annotations.TagsQuery) (annotations.FindTagsResult, error) { result := annotations.FindTagsResult{ Tags: []*annotations.TagsDTO{}, } diff --git a/pkg/services/annotations/annotations.go b/pkg/services/annotations/annotations.go index f571b1f8e62..9b79c1cdeec 100644 --- a/pkg/services/annotations/annotations.go +++ b/pkg/services/annotations/annotations.go @@ -16,8 +16,8 @@ type Repository interface { Save(item *Item) error Update(ctx context.Context, item *Item) error Find(ctx context.Context, query *ItemQuery) ([]*ItemDTO, error) - Delete(params *DeleteParams) error - FindTags(query *TagsQuery) (FindTagsResult, error) + Delete(ctx context.Context, params *DeleteParams) error + FindTags(ctx context.Context, query *TagsQuery) (FindTagsResult, error) } // AnnotationCleaner is responsible for cleaning up old annotations diff --git a/pkg/services/ngalert/store/testing.go b/pkg/services/ngalert/store/testing.go index cf2ec39f5ed..6b6d89b6ba6 100644 --- a/pkg/services/ngalert/store/testing.go +++ b/pkg/services/ngalert/store/testing.go @@ -436,7 +436,7 @@ func (repo *FakeAnnotationsRepo) Len() int { return len(repo.Items) } -func (repo *FakeAnnotationsRepo) Delete(params *annotations.DeleteParams) error { +func (repo *FakeAnnotationsRepo) Delete(_ context.Context, params *annotations.DeleteParams) error { return nil } @@ -456,7 +456,7 @@ func (repo *FakeAnnotationsRepo) Find(_ context.Context, query *annotations.Item return annotations, nil } -func (repo *FakeAnnotationsRepo) FindTags(query *annotations.TagsQuery) (annotations.FindTagsResult, error) { +func (repo *FakeAnnotationsRepo) FindTags(_ context.Context, query *annotations.TagsQuery) (annotations.FindTagsResult, error) { result := annotations.FindTagsResult{ Tags: []*annotations.TagsDTO{}, } diff --git a/pkg/services/sqlstore/annotation.go b/pkg/services/sqlstore/annotation.go index 2dd380adcda..633bc3bb10d 100644 --- a/pkg/services/sqlstore/annotation.go +++ b/pkg/services/sqlstore/annotation.go @@ -239,8 +239,8 @@ func (r *SQLAnnotationRepo) Find(ctx context.Context, query *annotations.ItemQue return items, err } -func (r *SQLAnnotationRepo) Delete(params *annotations.DeleteParams) error { - return inTransaction(func(sess *DBSession) error { +func (r *SQLAnnotationRepo) Delete(ctx context.Context, params *annotations.DeleteParams) error { + return r.sql.WithTransactionalDbSession(ctx, func(sess *DBSession) error { var ( sql string annoTagSQL string @@ -275,17 +275,19 @@ func (r *SQLAnnotationRepo) Delete(params *annotations.DeleteParams) error { }) } -func (r *SQLAnnotationRepo) FindTags(query *annotations.TagsQuery) (annotations.FindTagsResult, error) { - if query.Limit == 0 { - query.Limit = 100 - } +func (r *SQLAnnotationRepo) FindTags(ctx context.Context, query *annotations.TagsQuery) (annotations.FindTagsResult, error) { + var items []*annotations.Tag + err := r.sql.WithDbSession(ctx, func(dbSession *DBSession) error { + if query.Limit == 0 { + query.Limit = 100 + } - var sql bytes.Buffer - params := make([]interface{}, 0) - tagKey := `tag.` + dialect.Quote("key") - tagValue := `tag.` + dialect.Quote("value") + var sql bytes.Buffer + params := make([]interface{}, 0) + tagKey := `tag.` + dialect.Quote("key") + tagValue := `tag.` + dialect.Quote("value") - sql.WriteString(` + sql.WriteString(` SELECT ` + tagKey + `, ` + tagValue + `, @@ -294,21 +296,22 @@ func (r *SQLAnnotationRepo) FindTags(query *annotations.TagsQuery) (annotations. INNER JOIN annotation_tag ON tag.id = annotation_tag.tag_id `) - sql.WriteString(`WHERE EXISTS(SELECT 1 FROM annotation WHERE annotation.id = annotation_tag.annotation_id AND annotation.org_id = ?)`) - params = append(params, query.OrgID) + sql.WriteString(`WHERE EXISTS(SELECT 1 FROM annotation WHERE annotation.id = annotation_tag.annotation_id AND annotation.org_id = ?)`) + params = append(params, query.OrgID) - sql.WriteString(` AND (` + tagKey + ` ` + dialect.LikeStr() + ` ? OR ` + tagValue + ` ` + dialect.LikeStr() + ` ?)`) - params = append(params, `%`+query.Tag+`%`, `%`+query.Tag+`%`) + sql.WriteString(` AND (` + tagKey + ` ` + dialect.LikeStr() + ` ? OR ` + tagValue + ` ` + dialect.LikeStr() + ` ?)`) + params = append(params, `%`+query.Tag+`%`, `%`+query.Tag+`%`) - sql.WriteString(` GROUP BY ` + tagKey + `,` + tagValue) - sql.WriteString(` ORDER BY ` + tagKey + `,` + tagValue) - sql.WriteString(` ` + dialect.Limit(query.Limit)) + sql.WriteString(` GROUP BY ` + tagKey + `,` + tagValue) + sql.WriteString(` ORDER BY ` + tagKey + `,` + tagValue) + sql.WriteString(` ` + dialect.Limit(query.Limit)) - var items []*annotations.Tag - if err := x.SQL(sql.String(), params...).Find(&items); err != nil { + err := dbSession.SQL(sql.String(), params...).Find(&items) + return err + }) + if err != nil { return annotations.FindTagsResult{Tags: []*annotations.TagsDTO{}}, err } - tags := make([]*annotations.TagsDTO, 0) for _, item := range items { tag := item.Key diff --git a/pkg/services/sqlstore/annotation_test.go b/pkg/services/sqlstore/annotation_test.go index 6de716258d4..8721dd3045c 100644 --- a/pkg/services/sqlstore/annotation_test.go +++ b/pkg/services/sqlstore/annotation_test.go @@ -245,7 +245,7 @@ func TestAnnotations(t *testing.T) { require.NoError(t, err) annotationId := items[0].Id - err = repo.Delete(&annotations.DeleteParams{Id: annotationId, OrgId: 1}) + err = repo.Delete(context.Background(), &annotations.DeleteParams{Id: annotationId, OrgId: 1}) require.NoError(t, err) items, err = repo.Find(context.Background(), query) @@ -275,7 +275,7 @@ func TestAnnotations(t *testing.T) { dashboardId := items[0].DashboardId panelId := items[0].PanelId - err = repo.Delete(&annotations.DeleteParams{DashboardId: dashboardId, PanelId: panelId, OrgId: 1}) + err = repo.Delete(context.Background(), &annotations.DeleteParams{DashboardId: dashboardId, PanelId: panelId, OrgId: 1}) require.NoError(t, err) items, err = repo.Find(context.Background(), query) @@ -284,7 +284,7 @@ func TestAnnotations(t *testing.T) { }) t.Run("Should find tags by key", func(t *testing.T) { - result, err := repo.FindTags(&annotations.TagsQuery{ + result, err := repo.FindTags(context.Background(), &annotations.TagsQuery{ OrgID: 1, Tag: "server", }) @@ -295,7 +295,7 @@ func TestAnnotations(t *testing.T) { }) t.Run("Should find tags by value", func(t *testing.T) { - result, err := repo.FindTags(&annotations.TagsQuery{ + result, err := repo.FindTags(context.Background(), &annotations.TagsQuery{ OrgID: 1, Tag: "outage", }) @@ -308,7 +308,7 @@ func TestAnnotations(t *testing.T) { }) t.Run("Should not find tags in other org", func(t *testing.T) { - result, err := repo.FindTags(&annotations.TagsQuery{ + result, err := repo.FindTags(context.Background(), &annotations.TagsQuery{ OrgID: 0, Tag: "server-1", }) @@ -317,7 +317,7 @@ func TestAnnotations(t *testing.T) { }) t.Run("Should not find tags that do not exist", func(t *testing.T) { - result, err := repo.FindTags(&annotations.TagsQuery{ + result, err := repo.FindTags(context.Background(), &annotations.TagsQuery{ OrgID: 0, Tag: "unknown:tag", })