Chore: StoreSplit tag service (#55453)

* move tag service outside

* fix dashboard

* fix test

* lint

* fix linter

* remove spew
This commit is contained in:
ying-jeanne
2022-09-21 08:04:01 -04:00
committed by GitHub
parent e3e954cbba
commit 7b4cea8151
46 changed files with 355 additions and 293 deletions
-7
View File
@@ -34,13 +34,6 @@ func (ss *SQLStore) NewSession(ctx context.Context) *DBSession {
return sess
}
func (ss *SQLStore) newSession(ctx context.Context) *DBSession {
sess := &DBSession{Session: ss.engine.NewSession()}
sess.Session = sess.Session.Context(ctx)
return sess
}
func startSessionOrUseExisting(ctx context.Context, engine *xorm.Engine, beginTran bool) (*DBSession, bool, error) {
value := ctx.Value(ContextSessionKey{})
var sess *DBSession
-26
View File
@@ -1,26 +0,0 @@
package sqlstore
import "github.com/grafana/grafana/pkg/models"
// Will insert if needed any new key/value pars and return ids
func EnsureTagsExist(sess *DBSession, tags []*models.Tag) ([]*models.Tag, error) {
for _, tag := range tags {
var existingTag models.Tag
// check if it exists
exists, err := sess.Table("tag").Where("`key`=? AND `value`=?", tag.Key, tag.Value).Get(&existingTag)
if err != nil {
return nil, err
}
if exists {
tag.Id = existingTag.Id
} else {
_, err := sess.Table("tag").Insert(tag)
if err != nil {
return nil, err
}
}
}
return tags, nil
}
-28
View File
@@ -1,28 +0,0 @@
package sqlstore
import (
"context"
"testing"
"github.com/grafana/grafana/pkg/models"
"github.com/stretchr/testify/require"
)
func TestIntegrationSavingTags(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
ss := InitTestDB(t)
tagPairs := []*models.Tag{
{Key: "outage"},
{Key: "type", Value: "outage"},
{Key: "server", Value: "server-1"},
{Key: "error"},
}
tags, err := EnsureTagsExist(ss.newSession(context.Background()), tagPairs)
require.Nil(t, err)
require.Equal(t, 4, len(tags))
}