Chore: Remove global database engine variable from annotation (#46940)

* Chore: Remove global database engine variable from annotation

* 💩
This commit is contained in:
Kat Yang 2022-03-25 13:23:09 -04:00 committed by GitHub
parent 19be0b4170
commit 90f2233ea9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 39 additions and 36 deletions

View File

@ -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)
}

View File

@ -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{},
}

View File

@ -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

View File

@ -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{},
}

View File

@ -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,7 +275,9 @@ func (r *SQLAnnotationRepo) Delete(params *annotations.DeleteParams) error {
})
}
func (r *SQLAnnotationRepo) FindTags(query *annotations.TagsQuery) (annotations.FindTagsResult, error) {
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
}
@ -304,11 +306,12 @@ func (r *SQLAnnotationRepo) FindTags(query *annotations.TagsQuery) (annotations.
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

View File

@ -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",
})