chore: remove sqlstore & mockstore dependencies from (most) packages (#57087)

* chore: add alias for InitTestDB and Session

Adds an alias for the sqlstore InitTestDB and Session, and updates tests using these to reduce dependencies on the sqlstore.Store.

* next pass of removing sqlstore imports
* last little bit
* remove mockstore where possible
This commit is contained in:
Kristin Laemmert
2022-10-19 09:02:15 -04:00
committed by GitHub
parent 5285d34cc0
commit 05709ce411
273 changed files with 1595 additions and 1491 deletions
@@ -3,9 +3,9 @@ package annotationsimpl
import (
"context"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/annotations"
"github.com/grafana/grafana/pkg/services/sqlstore/db"
"github.com/grafana/grafana/pkg/services/tag"
"github.com/grafana/grafana/pkg/setting"
)
@@ -3,8 +3,8 @@ package annotationsimpl
import (
"context"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/sqlstore/db"
"github.com/grafana/grafana/pkg/setting"
)
@@ -5,19 +5,20 @@ import (
"testing"
"time"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/annotations"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/setting"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/annotations"
"github.com/grafana/grafana/pkg/setting"
)
func TestAnnotationCleanUp(t *testing.T) {
fakeSQL := sqlstore.InitTestDB(t)
fakeSQL := db.InitTestDB(t)
t.Cleanup(func() {
err := fakeSQL.WithDbSession(context.Background(), func(session *sqlstore.DBSession) error {
err := fakeSQL.WithDbSession(context.Background(), func(session *db.Session) error {
_, err := session.Exec("DELETE FROM annotation")
return err
})
@@ -111,10 +112,10 @@ func TestAnnotationCleanUp(t *testing.T) {
}
func TestOldAnnotationsAreDeletedFirst(t *testing.T) {
fakeSQL := sqlstore.InitTestDB(t)
fakeSQL := db.InitTestDB(t)
t.Cleanup(func() {
err := fakeSQL.WithDbSession(context.Background(), func(session *sqlstore.DBSession) error {
err := fakeSQL.WithDbSession(context.Background(), func(session *db.Session) error {
_, err := session.Exec("DELETE FROM annotation")
return err
})
@@ -132,7 +133,7 @@ func TestOldAnnotationsAreDeletedFirst(t *testing.T) {
Created: time.Now().AddDate(-10, 0, -10).UnixNano() / int64(time.Millisecond),
}
err := fakeSQL.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
err := fakeSQL.WithDbSession(context.Background(), func(sess *db.Session) error {
_, err := sess.Insert(a)
require.NoError(t, err, "cannot insert annotation")
_, err = sess.Insert(a)
@@ -163,10 +164,10 @@ func TestOldAnnotationsAreDeletedFirst(t *testing.T) {
require.NoError(t, err)
}
func assertAnnotationCount(t *testing.T, fakeSQL *sqlstore.SQLStore, sql string, expectedCount int64) {
func assertAnnotationCount(t *testing.T, fakeSQL db.DB, sql string, expectedCount int64) {
t.Helper()
err := fakeSQL.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
err := fakeSQL.WithDbSession(context.Background(), func(sess *db.Session) error {
count, err := sess.Where(sql).Count(&annotations.Item{})
require.NoError(t, err)
require.Equal(t, expectedCount, count)
@@ -175,10 +176,10 @@ func assertAnnotationCount(t *testing.T, fakeSQL *sqlstore.SQLStore, sql string,
require.NoError(t, err)
}
func assertAnnotationTagCount(t *testing.T, fakeSQL *sqlstore.SQLStore, expectedCount int64) {
func assertAnnotationTagCount(t *testing.T, fakeSQL db.DB, expectedCount int64) {
t.Helper()
err := fakeSQL.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
err := fakeSQL.WithDbSession(context.Background(), func(sess *db.Session) error {
count, err := sess.SQL("select count(*) from annotation_tag").Count()
require.NoError(t, err)
require.Equal(t, expectedCount, count)
@@ -187,7 +188,7 @@ func assertAnnotationTagCount(t *testing.T, fakeSQL *sqlstore.SQLStore, expected
require.NoError(t, err)
}
func createTestAnnotations(t *testing.T, store *sqlstore.SQLStore, expectedCount int, oldAnnotations int) {
func createTestAnnotations(t *testing.T, store db.DB, expectedCount int, oldAnnotations int) {
t.Helper()
cutoffDate := time.Now()
@@ -221,7 +222,7 @@ func createTestAnnotations(t *testing.T, store *sqlstore.SQLStore, expectedCount
a.Created = cutoffDate.AddDate(-10, 0, -10).UnixNano() / int64(time.Millisecond)
}
err := store.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
err := store.WithDbSession(context.Background(), func(sess *db.Session) error {
_, err := sess.Insert(a)
require.NoError(t, err, "should be able to save annotation", err)
@@ -8,12 +8,11 @@ import (
"strings"
"time"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/annotations"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/sqlstore/db"
"github.com/grafana/grafana/pkg/services/sqlstore/permissions"
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
"github.com/grafana/grafana/pkg/services/tag"
@@ -60,7 +59,7 @@ func (r *xormRepositoryImpl) Add(ctx context.Context, item *annotations.Item) er
return err
}
return r.db.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
return r.db.WithDbSession(ctx, func(sess *db.Session) error {
if _, err := sess.Table("annotation").Insert(item); err != nil {
return err
}
@@ -81,7 +80,7 @@ func (r *xormRepositoryImpl) Add(ctx context.Context, item *annotations.Item) er
}
func (r *xormRepositoryImpl) Update(ctx context.Context, item *annotations.Item) error {
return r.db.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
return r.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
var (
isExist bool
err error
@@ -137,7 +136,7 @@ func (r *xormRepositoryImpl) Get(ctx context.Context, query *annotations.ItemQue
var sql bytes.Buffer
params := make([]interface{}, 0)
items := make([]*annotations.ItemDTO, 0)
err := r.db.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
err := r.db.WithDbSession(ctx, func(sess *db.Session) error {
sql.WriteString(`
SELECT
annotation.id,
@@ -293,7 +292,7 @@ func getAccessControlFilter(user *user.SignedInUser) (string, []interface{}, err
}
func (r *xormRepositoryImpl) Delete(ctx context.Context, params *annotations.DeleteParams) error {
return r.db.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
return r.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
var (
sql string
annoTagSQL string
@@ -330,7 +329,7 @@ func (r *xormRepositoryImpl) Delete(ctx context.Context, params *annotations.Del
func (r *xormRepositoryImpl) GetTags(ctx context.Context, query *annotations.TagsQuery) (annotations.FindTagsResult, error) {
var items []*annotations.Tag
err := r.db.WithDbSession(ctx, func(dbSession *sqlstore.DBSession) error {
err := r.db.WithDbSession(ctx, func(dbSession *db.Session) error {
if query.Limit == 0 {
query.Limit = 100
}
@@ -446,7 +445,7 @@ func (r *xormRepositoryImpl) executeUntilDoneOrCancelled(ctx context.Context, sq
return totalAffected, ctx.Err()
default:
var affected int64
err := r.db.WithDbSession(ctx, func(session *sqlstore.DBSession) error {
err := r.db.WithDbSession(ctx, func(session *db.Session) error {
res, err := session.Exec(sql)
if err != nil {
return err
@@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/accesscontrol"
@@ -17,7 +18,6 @@ import (
"github.com/grafana/grafana/pkg/services/dashboards"
dashboardstore "github.com/grafana/grafana/pkg/services/dashboards/database"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/tag/tagimpl"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/setting"
@@ -27,7 +27,7 @@ func TestIntegrationAnnotations(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
sql := sqlstore.InitTestDB(t)
sql := db.InitTestDB(t)
var maximumTagsLength int64 = 60
repo := xormRepositoryImpl{db: sql, cfg: setting.NewCfg(), log: log.New("annotation.test"), tagService: tagimpl.ProvideService(sql, sql.Cfg), maximumTagsLength: maximumTagsLength}
@@ -43,7 +43,7 @@ func TestIntegrationAnnotations(t *testing.T) {
t.Run("Testing annotation create, read, update and delete", func(t *testing.T) {
t.Cleanup(func() {
err := sql.WithDbSession(context.Background(), func(dbSession *sqlstore.DBSession) error {
err := sql.WithDbSession(context.Background(), func(dbSession *db.Session) error {
_, err := dbSession.Exec("DELETE FROM annotation WHERE 1=1")
if err != nil {
return err
@@ -406,7 +406,7 @@ func TestIntegrationAnnotationListingWithRBAC(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
sql := sqlstore.InitTestDB(t, sqlstore.InitTestDBOpt{})
sql := db.InitTestDB(t)
var maximumTagsLength int64 = 60
repo := xormRepositoryImpl{db: sql, cfg: setting.NewCfg(), log: log.New("annotation.test"), tagService: tagimpl.ProvideService(sql, sql.Cfg), maximumTagsLength: maximumTagsLength}
dashboardStore := dashboardstore.ProvideDashboardStore(sql, sql.Cfg, featuremgmt.WithFeatures(), tagimpl.ProvideService(sql, sql.Cfg))