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,8 +3,8 @@ package dashverimpl
import (
"context"
"github.com/grafana/grafana/pkg/infra/db"
dashver "github.com/grafana/grafana/pkg/services/dashboardversion"
"github.com/grafana/grafana/pkg/services/sqlstore/db"
"github.com/grafana/grafana/pkg/setting"
)
@@ -3,11 +3,11 @@ package dashverimpl
import (
"testing"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/infra/db"
)
func TestIntegrationSQLxGetDashboardVersion(t *testing.T) {
testIntegrationGetDashboardVersion(t, func(ss *sqlstore.SQLStore) store {
testIntegrationGetDashboardVersion(t, func(ss db.DB) store {
return &sqlxStore{
sess: ss.GetSqlxSession(),
}
@@ -6,23 +6,23 @@ import (
"time"
"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"
dashver "github.com/grafana/grafana/pkg/services/dashboardversion"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type getStore func(*sqlstore.SQLStore) store
type getStore func(db.DB) store
func testIntegrationGetDashboardVersion(t *testing.T, fn getStore) {
if testing.Short() {
t.Skip("skipping integration test")
}
ss := sqlstore.InitTestDB(t)
ss := db.InitTestDB(t)
dashVerStore := fn(ss)
t.Run("Get a Dashboard ID and version ID", func(t *testing.T) {
@@ -112,9 +112,9 @@ func testIntegrationGetDashboardVersion(t *testing.T, fn getStore) {
})
}
func getDashboard(t *testing.T, sqlStore *sqlstore.SQLStore, dashboard *models.Dashboard) error {
func getDashboard(t *testing.T, sqlStore db.DB, dashboard *models.Dashboard) error {
t.Helper()
return sqlStore.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
return sqlStore.WithDbSession(context.Background(), func(sess *db.Session) error {
has, err := sess.Get(dashboard)
if err != nil {
@@ -129,7 +129,7 @@ func getDashboard(t *testing.T, sqlStore *sqlstore.SQLStore, dashboard *models.D
})
}
func insertTestDashboard(t *testing.T, sqlStore *sqlstore.SQLStore, title string, orgId int64,
func insertTestDashboard(t *testing.T, sqlStore db.DB, title string, orgId int64,
folderId int64, isFolder bool, tags ...interface{}) *models.Dashboard {
t.Helper()
cmd := models.SaveDashboardCommand{
@@ -144,7 +144,7 @@ func insertTestDashboard(t *testing.T, sqlStore *sqlstore.SQLStore, title string
}
var dash *models.Dashboard
err := sqlStore.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
err := sqlStore.WithDbSession(context.Background(), func(sess *db.Session) error {
dash = cmd.GetDashboardModel()
dash.SetVersion(1)
dash.Created = time.Now()
@@ -159,7 +159,7 @@ func insertTestDashboard(t *testing.T, sqlStore *sqlstore.SQLStore, title string
dash.Data.Set("id", dash.Id)
dash.Data.Set("uid", dash.Uid)
err = sqlStore.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
err = sqlStore.WithDbSession(context.Background(), func(sess *db.Session) error {
dashVersion := &dashver.DashboardVersion{
DashboardID: dash.Id,
ParentVersion: dash.Version,
@@ -184,7 +184,7 @@ func insertTestDashboard(t *testing.T, sqlStore *sqlstore.SQLStore, title string
return dash
}
func updateTestDashboard(t *testing.T, sqlStore *sqlstore.SQLStore, dashboard *models.Dashboard, data map[string]interface{}) {
func updateTestDashboard(t *testing.T, sqlStore db.DB, dashboard *models.Dashboard, data map[string]interface{}) {
t.Helper()
data["id"] = dashboard.Id
@@ -197,7 +197,7 @@ func updateTestDashboard(t *testing.T, sqlStore *sqlstore.SQLStore, dashboard *m
Dashboard: simplejson.NewFromAny(data),
}
var dash *models.Dashboard
err := sqlStore.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
err := sqlStore.WithDbSession(context.Background(), func(sess *db.Session) error {
var existing models.Dashboard
dash = cmd.GetDashboardModel()
dashWithIdExists, err := sess.Where("id=? AND org_id=?", dash.Id, dash.OrgId).Get(&existing)
@@ -221,7 +221,7 @@ func updateTestDashboard(t *testing.T, sqlStore *sqlstore.SQLStore, dashboard *m
require.Nil(t, err)
err = sqlStore.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
err = sqlStore.WithDbSession(context.Background(), func(sess *db.Session) error {
dashVersion := &dashver.DashboardVersion{
DashboardID: dash.Id,
ParentVersion: parentVersion,
@@ -4,9 +4,8 @@ import (
"context"
"strings"
"github.com/grafana/grafana/pkg/infra/db"
dashver "github.com/grafana/grafana/pkg/services/dashboardversion"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/sqlstore/db"
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
)
@@ -17,7 +16,7 @@ type sqlStore struct {
func (ss *sqlStore) Get(ctx context.Context, query *dashver.GetDashboardVersionQuery) (*dashver.DashboardVersion, error) {
var version dashver.DashboardVersion
err := ss.db.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
err := ss.db.WithDbSession(ctx, func(sess *db.Session) error {
has, err := sess.Where("dashboard_version.dashboard_id=? AND dashboard_version.version=? AND dashboard.org_id=?", query.DashboardID, query.Version, query.OrgID).
Join("LEFT", "dashboard", `dashboard.id = dashboard_version.dashboard_id`).
Get(&version)
@@ -39,7 +38,7 @@ func (ss *sqlStore) Get(ctx context.Context, query *dashver.GetDashboardVersionQ
func (ss *sqlStore) GetBatch(ctx context.Context, cmd *dashver.DeleteExpiredVersionsCommand, perBatch int, versionsToKeep int) ([]interface{}, error) {
var versionIds []interface{}
err := ss.db.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
err := ss.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
versionIdsToDeleteQuery := `SELECT id
FROM dashboard_version, (
SELECT dashboard_id, count(version) as count, min(version) as min
@@ -58,7 +57,7 @@ func (ss *sqlStore) GetBatch(ctx context.Context, cmd *dashver.DeleteExpiredVers
func (ss *sqlStore) DeleteBatch(ctx context.Context, cmd *dashver.DeleteExpiredVersionsCommand, versionIdsToDelete []interface{}) (int64, error) {
var deleted int64
err := ss.db.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
err := ss.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
deleteExpiredSQL := `DELETE FROM dashboard_version WHERE id IN (?` + strings.Repeat(",?", len(versionIdsToDelete)-1) + `)`
sqlOrArgs := append([]interface{}{deleteExpiredSQL}, versionIdsToDelete...)
expiredResponse, err := sess.Exec(sqlOrArgs...)
@@ -74,7 +73,7 @@ func (ss *sqlStore) DeleteBatch(ctx context.Context, cmd *dashver.DeleteExpiredV
func (ss *sqlStore) List(ctx context.Context, query *dashver.ListDashboardVersionsQuery) ([]*dashver.DashboardVersionDTO, error) {
var dashboardVersion []*dashver.DashboardVersionDTO
err := ss.db.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
err := ss.db.WithDbSession(ctx, func(sess *db.Session) error {
err := sess.Table("dashboard_version").
Select(`dashboard_version.id,
dashboard_version.dashboard_id,
@@ -3,11 +3,11 @@ package dashverimpl
import (
"testing"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/infra/db"
)
func TestIntegrationXORMGetDashboardVersion(t *testing.T) {
testIntegrationGetDashboardVersion(t, func(ss *sqlstore.SQLStore) store {
testIntegrationGetDashboardVersion(t, func(ss db.DB) store {
return &sqlStore{
db: ss,
dialect: ss.GetDialect(),