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
-21
View File
@@ -1,21 +0,0 @@
package db
import (
"context"
"xorm.io/core"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
"github.com/grafana/grafana/pkg/services/sqlstore/session"
)
type DB interface {
WithTransactionalDbSession(ctx context.Context, callback sqlstore.DBTransactionFunc) error
WithDbSession(ctx context.Context, callback sqlstore.DBTransactionFunc) error
WithNewDbSession(ctx context.Context, callback sqlstore.DBTransactionFunc) error
GetDialect() migrator.Dialect
GetDBType() core.DbType
GetSqlxSession() *session.SessionDB
InTransaction(ctx context.Context, fn func(ctx context.Context) error) error
}
-27
View File
@@ -1,27 +0,0 @@
package dbtest
import (
"context"
"github.com/grafana/grafana/pkg/services/sqlstore"
)
type FakeDB struct {
ExpectedError error
}
func NewFakeDB() *FakeDB {
return &FakeDB{}
}
func (f *FakeDB) WithTransactionalDbSession(ctx context.Context, callback sqlstore.DBTransactionFunc) error {
return f.ExpectedError
}
func (f *FakeDB) WithDbSession(ctx context.Context, callback sqlstore.DBTransactionFunc) error {
return f.ExpectedError
}
func (f *FakeDB) WithNewDbSession(ctx context.Context, callback sqlstore.DBTransactionFunc) error {
return f.ExpectedError
}
+4 -2
View File
@@ -3,13 +3,15 @@ package mockstore
import (
"context"
"xorm.io/core"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/apikey"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
"github.com/grafana/grafana/pkg/services/sqlstore/session"
"github.com/grafana/grafana/pkg/services/user"
"xorm.io/core"
)
type OrgListResponse []struct {
@@ -198,7 +200,7 @@ func (m SQLStoreMock) GetTeamMembers(ctx context.Context, query *models.GetTeamM
return m.ExpectedError
}
func (m *SQLStoreMock) NewSession(ctx context.Context) *sqlstore.DBSession {
func (m *SQLStoreMock) NewSession(ctx context.Context) *db.Session {
return nil
}
@@ -9,10 +9,10 @@ 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/models"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/sqlstore/permissions"
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
"github.com/grafana/grafana/pkg/services/user"
@@ -31,24 +31,24 @@ func TestBuilder_EqualResults_Basic(t *testing.T) {
OrgRole: org.RoleEditor,
}
db := setupTestEnvironment(t)
dashIds := createDashboards(t, db, 0, 1, user.OrgID)
store := setupTestEnvironment(t)
dashIds := createDashboards(t, store, 0, 1, user.OrgID)
require.Len(t, dashIds, 1)
// create one dashboard in another organization that shouldn't
// be listed in the results.
createDashboards(t, db, 1, 2, 2)
createDashboards(t, store, 1, 2, 2)
builder := &searchstore.Builder{
Filters: []interface{}{
searchstore.OrgFilter{OrgId: user.OrgID},
searchstore.TitleSorter{},
},
Dialect: db.Dialect,
Dialect: store.GetDialect(),
}
res := []dashboards.DashboardSearchProjection{}
err := db.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
err := store.WithDbSession(context.Background(), func(sess *db.Session) error {
sql, params := builder.ToSQL(limit, page)
return sess.SQL(sql, params...).Find(&res)
})
@@ -73,21 +73,21 @@ func TestBuilder_Pagination(t *testing.T) {
OrgRole: org.RoleViewer,
}
db := setupTestEnvironment(t)
createDashboards(t, db, 0, 25, user.OrgID)
store := setupTestEnvironment(t)
createDashboards(t, store, 0, 25, user.OrgID)
builder := &searchstore.Builder{
Filters: []interface{}{
searchstore.OrgFilter{OrgId: user.OrgID},
searchstore.TitleSorter{},
},
Dialect: db.Dialect,
Dialect: store.GetDialect(),
}
resPg1 := []dashboards.DashboardSearchProjection{}
resPg2 := []dashboards.DashboardSearchProjection{}
resPg3 := []dashboards.DashboardSearchProjection{}
err := db.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
err := store.WithDbSession(context.Background(), func(sess *db.Session) error {
sql, params := builder.ToSQL(15, 1)
err := sess.SQL(sql, params...).Find(&resPg1)
if err != nil {
@@ -119,8 +119,8 @@ func TestBuilder_Permissions(t *testing.T) {
OrgRole: org.RoleViewer,
}
db := setupTestEnvironment(t)
createDashboards(t, db, 0, 1, user.OrgID)
store := setupTestEnvironment(t)
createDashboards(t, store, 0, 1, user.OrgID)
level := models.PERMISSION_EDIT
@@ -129,18 +129,18 @@ func TestBuilder_Permissions(t *testing.T) {
searchstore.OrgFilter{OrgId: user.OrgID},
searchstore.TitleSorter{},
permissions.DashboardPermissionFilter{
Dialect: db.Dialect,
Dialect: store.GetDialect(),
OrgRole: user.OrgRole,
OrgId: user.OrgID,
UserId: user.UserID,
PermissionLevel: level,
},
},
Dialect: db.Dialect,
Dialect: store.GetDialect(),
}
res := []dashboards.DashboardSearchProjection{}
err := db.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
err := store.WithDbSession(context.Background(), func(sess *db.Session) error {
sql, params := builder.ToSQL(limit, page)
return sess.SQL(sql, params...).Find(&res)
})
@@ -149,13 +149,13 @@ func TestBuilder_Permissions(t *testing.T) {
assert.Len(t, res, 0)
}
func setupTestEnvironment(t *testing.T) *sqlstore.SQLStore {
func setupTestEnvironment(t *testing.T) db.DB {
t.Helper()
store := sqlstore.InitTestDB(t)
store := db.InitTestDB(t)
return store
}
func createDashboards(t *testing.T, db *sqlstore.SQLStore, startID, endID int, orgID int64) []int64 {
func createDashboards(t *testing.T, store db.DB, startID, endID int, orgID int64) []int64 {
t.Helper()
require.GreaterOrEqual(t, endID, startID)
@@ -174,7 +174,7 @@ func createDashboards(t *testing.T, db *sqlstore.SQLStore, startID, endID int, o
require.NoError(t, err)
var dash *models.Dashboard
err = db.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
err = store.WithDbSession(context.Background(), func(sess *db.Session) error {
dash = models.NewDashboardFromJson(dashboard)
dash.OrgId = orgID
dash.Uid = util.GenerateShortUID()
@@ -186,7 +186,7 @@ func createDashboards(t *testing.T, db *sqlstore.SQLStore, startID, endID int, o
tags := dash.GetTags()
if len(tags) > 0 {
for _, tag := range tags {
if _, err := sess.Insert(&sqlstore.DashboardTag{DashboardId: dash.Id, Term: tag}); err != nil {
if _, err := sess.Insert(&DashboardTag{DashboardId: dash.Id, Term: tag}); err != nil {
return err
}
}
@@ -213,3 +213,9 @@ func lexiCounter(n int) string {
return value
}
type DashboardTag struct {
Id int64
DashboardId int64
Term string
}
+5
View File
@@ -505,6 +505,11 @@ func InitTestDB(t ITestDB, opts ...InitTestDBOpt) *SQLStore {
return store
}
func InitTestDBWithCfg(t ITestDB, opts ...InitTestDBOpt) (*SQLStore, *setting.Cfg) {
store := InitTestDB(t, opts...)
return store, store.Cfg
}
func initTestDB(migration registry.DatabaseMigrator, opts ...InitTestDBOpt) (*SQLStore, error) {
testSQLStoreMutex.Lock()
defer testSQLStoreMutex.Unlock()
-9
View File
@@ -1,9 +0,0 @@
package sqlstore
// extra tables not required by the core/outside model
type DashboardTag struct {
Id int64
DashboardId int64
Term string
}