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

View File

@@ -5,9 +5,9 @@ import (
"errors"
"time"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/services/featuremgmt"
pref "github.com/grafana/grafana/pkg/services/preference"
"github.com/grafana/grafana/pkg/services/sqlstore/db"
"github.com/grafana/grafana/pkg/setting"
)

View File

@@ -3,11 +3,11 @@ package prefimpl
import (
"testing"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/infra/db"
)
func TestIntegrationSQLxPreferencesDataAccess(t *testing.T) {
testIntegrationPreferencesDataAccess(t, func(ss *sqlstore.SQLStore) store {
testIntegrationPreferencesDataAccess(t, func(ss db.DB) store {
return &sqlxStore{sess: ss.GetSqlxSession()}
})
}

View File

@@ -6,19 +6,20 @@ import (
"time"
"github.com/google/go-cmp/cmp"
pref "github.com/grafana/grafana/pkg/services/preference"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/user"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/infra/db"
pref "github.com/grafana/grafana/pkg/services/preference"
"github.com/grafana/grafana/pkg/services/user"
)
type getStore func(*sqlstore.SQLStore) store
type getStore func(db.DB) store
func testIntegrationPreferencesDataAccess(t *testing.T, fn getStore) {
if testing.Short() {
t.Skip("skipping integration test")
}
ss := sqlstore.InitTestDB(t)
ss := db.InitTestDB(t)
prefStore := fn(ss)
orgNavbarPreferences := pref.NavbarPreference{
SavedItems: []pref.NavLink{{
@@ -116,7 +117,7 @@ func testIntegrationPreferencesDataAccess(t *testing.T, fn getStore) {
})
t.Run("Update for a user should only modify a single value", func(t *testing.T) {
ss := sqlstore.InitTestDB(t)
ss := db.InitTestDB(t)
prefStore := fn(ss)
id, err := prefStore.Insert(context.Background(), &pref.Preference{
UserID: user.SignedInUser{}.UserID,

View File

@@ -4,9 +4,8 @@ import (
"context"
"strings"
"github.com/grafana/grafana/pkg/infra/db"
pref "github.com/grafana/grafana/pkg/services/preference"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/sqlstore/db"
)
type sqlStore struct {
@@ -15,7 +14,7 @@ type sqlStore struct {
func (s *sqlStore) Get(ctx context.Context, query *pref.Preference) (*pref.Preference, error) {
var prefs pref.Preference
err := s.db.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
err := s.db.WithDbSession(ctx, func(sess *db.Session) error {
exist, err := sess.Where("org_id=? AND user_id=? AND team_id=?", query.OrgID, query.UserID, query.TeamID).Get(&prefs)
if err != nil {
return err
@@ -49,7 +48,7 @@ func (s *sqlStore) List(ctx context.Context, query *pref.Preference) ([]*pref.Pr
params = append(params, query.UserID)
params = append(params, query.OrgID)
err := s.db.WithDbSession(ctx, func(dbSession *sqlstore.DBSession) error {
err := s.db.WithDbSession(ctx, func(dbSession *db.Session) error {
err := dbSession.Where(filter, params...).
OrderBy("user_id ASC, team_id ASC").
Find(&prefs)
@@ -64,7 +63,7 @@ func (s *sqlStore) List(ctx context.Context, query *pref.Preference) ([]*pref.Pr
}
func (s *sqlStore) Update(ctx context.Context, cmd *pref.Preference) error {
return s.db.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
return s.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
_, err := sess.ID(cmd.ID).AllCols().Update(cmd)
return err
})
@@ -73,7 +72,7 @@ func (s *sqlStore) Update(ctx context.Context, cmd *pref.Preference) error {
func (s *sqlStore) Insert(ctx context.Context, cmd *pref.Preference) (int64, error) {
var ID int64
var err error
err = s.db.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
err = s.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
ID, err = sess.Insert(cmd)
return err
})
@@ -81,7 +80,7 @@ func (s *sqlStore) Insert(ctx context.Context, cmd *pref.Preference) (int64, err
}
func (s *sqlStore) DeleteByUser(ctx context.Context, userID int64) error {
return s.db.WithDbSession(ctx, func(dbSession *sqlstore.DBSession) error {
return s.db.WithDbSession(ctx, func(dbSession *db.Session) error {
var rawSQL = "DELETE FROM preferences WHERE user_id = ?"
_, err := dbSession.Exec(rawSQL, userID)
return err

View File

@@ -3,11 +3,11 @@ package prefimpl
import (
"testing"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/infra/db"
)
func TestIntegrationXORMPreferencesDataAccess(t *testing.T) {
testIntegrationPreferencesDataAccess(t, func(ss *sqlstore.SQLStore) store {
testIntegrationPreferencesDataAccess(t, func(ss db.DB) store {
return &sqlStore{db: ss}
})
}