Chore: implement sqlx into tag service (#55908)
* add sqlx store to tag service * add sqlx into tag service * fix test * change to camal cases * change in xorm camal case
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package tagimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/session"
|
||||
"github.com/grafana/grafana/pkg/services/tag"
|
||||
)
|
||||
|
||||
type sqlxStore struct {
|
||||
sess *session.SessionDB
|
||||
}
|
||||
|
||||
func (s *sqlxStore) EnsureTagsExist(ctx context.Context, tags []*tag.Tag) ([]*tag.Tag, error) {
|
||||
for _, tagElement := range tags {
|
||||
var existingTag tag.Tag
|
||||
err := s.sess.Get(ctx, &existingTag, `SELECT * FROM tag WHERE "key"=? AND "value"=?`, tagElement.Key, tagElement.Value)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
tagElement.Id, err = s.sess.ExecWithReturningId(ctx, `INSERT INTO tag ("key", "value") VALUES (?, ?)`, tagElement.Key, tagElement.Value)
|
||||
if err != nil {
|
||||
return tags, err
|
||||
}
|
||||
} else {
|
||||
return tags, err
|
||||
}
|
||||
} else {
|
||||
tagElement.Id = existingTag.Id
|
||||
}
|
||||
}
|
||||
return tags, nil
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package tagimpl
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
)
|
||||
|
||||
func TestIntegrationSQLxSavingTags(t *testing.T) {
|
||||
testIntegrationSavingTags(t, func(ss *sqlstore.SQLStore) store {
|
||||
return &sqlxStore{sess: ss.GetSqlxSession()}
|
||||
})
|
||||
}
|
||||
@@ -3,37 +3,9 @@ package tagimpl
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/db"
|
||||
"github.com/grafana/grafana/pkg/services/tag"
|
||||
)
|
||||
|
||||
type store interface {
|
||||
EnsureTagsExist(context.Context, []*tag.Tag) ([]*tag.Tag, error)
|
||||
}
|
||||
|
||||
type sqlStore struct {
|
||||
db db.DB
|
||||
}
|
||||
|
||||
func (s *sqlStore) EnsureTagsExist(ctx context.Context, tags []*tag.Tag) ([]*tag.Tag, error) {
|
||||
err := s.db.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
for _, tagelement := range tags {
|
||||
var existingTag tag.Tag
|
||||
exists, err := sess.Table("tag").Where("`key`=? AND `value`=?", tagelement.Key, tagelement.Value).Get(&existingTag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exists {
|
||||
tagelement.Id = existingTag.Id
|
||||
} else {
|
||||
_, err := sess.Table("tag").Insert(tagelement)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return tags, err
|
||||
}
|
||||
|
||||
@@ -10,12 +10,14 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestIntegrationSavingTags(t *testing.T) {
|
||||
type getStore func(*sqlstore.SQLStore) store
|
||||
|
||||
func testIntegrationSavingTags(t *testing.T, fn getStore) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test")
|
||||
}
|
||||
ss := sqlstore.InitTestDB(t)
|
||||
store := sqlStore{db: ss}
|
||||
store := fn(ss)
|
||||
tagPairs := []*tag.Tag{
|
||||
{Key: "outage"},
|
||||
{Key: "type", Value: "outage"},
|
||||
|
||||
@@ -5,13 +5,21 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/db"
|
||||
"github.com/grafana/grafana/pkg/services/tag"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
store store
|
||||
}
|
||||
|
||||
func ProvideService(db db.DB) *Service {
|
||||
func ProvideService(db db.DB, cfg *setting.Cfg) *Service {
|
||||
if cfg.IsFeatureToggleEnabled("newDBLibrary") {
|
||||
return &Service{
|
||||
store: &sqlxStore{
|
||||
sess: db.GetSqlxSession(),
|
||||
},
|
||||
}
|
||||
}
|
||||
return &Service{
|
||||
store: &sqlStore{
|
||||
db: db,
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package tagimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/db"
|
||||
"github.com/grafana/grafana/pkg/services/tag"
|
||||
)
|
||||
|
||||
type sqlStore struct {
|
||||
db db.DB
|
||||
}
|
||||
|
||||
func (s *sqlStore) EnsureTagsExist(ctx context.Context, tags []*tag.Tag) ([]*tag.Tag, error) {
|
||||
err := s.db.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
for _, tagElement := range tags {
|
||||
var existingTag tag.Tag
|
||||
exists, err := sess.Table("tag").Where("`key`=? AND `value`=?", tagElement.Key, tagElement.Value).Get(&existingTag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exists {
|
||||
tagElement.Id = existingTag.Id
|
||||
} else {
|
||||
_, err := sess.Table("tag").Insert(tagElement)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return tags, err
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package tagimpl
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
)
|
||||
|
||||
func TestIntegrationXormSavingTags(t *testing.T) {
|
||||
testIntegrationSavingTags(t, func(ss *sqlstore.SQLStore) store {
|
||||
return &sqlStore{db: ss}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user