Chore: Remove Store interface and use db.DB instead (#60160)

* Chore: Remove Store interface and use db.DB instead

* use old-style session exec
This commit is contained in:
Serge Zaitsev
2022-12-13 11:03:36 +01:00
committed by GitHub
parent 4c4bc3bd32
commit af7d293eaf
13 changed files with 27 additions and 53 deletions
+2 -2
View File
@@ -11,12 +11,12 @@ import (
"github.com/grafana/grafana/pkg/api/response" "github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/api/routing" "github.com/grafana/grafana/pkg/api/routing"
"github.com/grafana/grafana/pkg/components/simplejson" "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/models"
"github.com/grafana/grafana/pkg/services/auth" "github.com/grafana/grafana/pkg/services/auth"
"github.com/grafana/grafana/pkg/services/auth/authtest" "github.com/grafana/grafana/pkg/services/auth/authtest"
"github.com/grafana/grafana/pkg/services/login/logintest" "github.com/grafana/grafana/pkg/services/login/logintest"
"github.com/grafana/grafana/pkg/services/org" "github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore" "github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
"github.com/grafana/grafana/pkg/services/user" "github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/services/user/usertest" "github.com/grafana/grafana/pkg/services/user/usertest"
@@ -233,7 +233,7 @@ func TestAdminAPIEndpoint(t *testing.T) {
} }
func putAdminScenario(t *testing.T, desc string, url string, routePattern string, role org.RoleType, func putAdminScenario(t *testing.T, desc string, url string, routePattern string, role org.RoleType,
cmd dtos.AdminUpdateUserPermissionsForm, fn scenarioFunc, sqlStore sqlstore.Store, userSvc user.Service) { cmd dtos.AdminUpdateUserPermissionsForm, fn scenarioFunc, sqlStore db.DB, userSvc user.Service) {
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) { t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
hs := &HTTPServer{ hs := &HTTPServer{
Cfg: setting.NewCfg(), Cfg: setting.NewCfg(),
+2 -3
View File
@@ -22,7 +22,6 @@ import (
"github.com/grafana/grafana/pkg/services/dashboards" "github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/guardian" "github.com/grafana/grafana/pkg/services/guardian"
"github.com/grafana/grafana/pkg/services/org" "github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore" "github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
"github.com/grafana/grafana/pkg/services/team/teamtest" "github.com/grafana/grafana/pkg/services/team/teamtest"
) )
@@ -282,7 +281,7 @@ func TestAnnotationsAPIEndpoint(t *testing.T) {
} }
func postAnnotationScenario(t *testing.T, desc string, url string, routePattern string, role org.RoleType, func postAnnotationScenario(t *testing.T, desc string, url string, routePattern string, role org.RoleType,
cmd dtos.PostAnnotationsCmd, store sqlstore.Store, dashSvc dashboards.DashboardService, fn scenarioFunc) { cmd dtos.PostAnnotationsCmd, store db.DB, dashSvc dashboards.DashboardService, fn scenarioFunc) {
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) { t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
hs := setupSimpleHTTPServer(nil) hs := setupSimpleHTTPServer(nil)
hs.SQLStore = store hs.SQLStore = store
@@ -358,7 +357,7 @@ func patchAnnotationScenario(t *testing.T, desc string, url string, routePattern
} }
func deleteAnnotationsScenario(t *testing.T, desc string, url string, routePattern string, role org.RoleType, func deleteAnnotationsScenario(t *testing.T, desc string, url string, routePattern string, role org.RoleType,
cmd dtos.MassDeleteAnnotationsCmd, store sqlstore.Store, dashSvc dashboards.DashboardService, fn scenarioFunc) { cmd dtos.MassDeleteAnnotationsCmd, store db.DB, dashSvc dashboards.DashboardService, fn scenarioFunc) {
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) { t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
hs := setupSimpleHTTPServer(nil) hs := setupSimpleHTTPServer(nil)
hs.SQLStore = store hs.SQLStore = store
+4 -4
View File
@@ -68,11 +68,11 @@ import (
"github.com/grafana/grafana/pkg/web/webtest" "github.com/grafana/grafana/pkg/web/webtest"
) )
func loggedInUserScenario(t *testing.T, desc string, url string, routePattern string, fn scenarioFunc, sqlStore sqlstore.Store) { func loggedInUserScenario(t *testing.T, desc string, url string, routePattern string, fn scenarioFunc, sqlStore db.DB) {
loggedInUserScenarioWithRole(t, desc, "GET", url, routePattern, org.RoleEditor, fn, sqlStore) loggedInUserScenarioWithRole(t, desc, "GET", url, routePattern, org.RoleEditor, fn, sqlStore)
} }
func loggedInUserScenarioWithRole(t *testing.T, desc string, method string, url string, routePattern string, role org.RoleType, fn scenarioFunc, sqlStore sqlstore.Store) { func loggedInUserScenarioWithRole(t *testing.T, desc string, method string, url string, routePattern string, role org.RoleType, fn scenarioFunc, sqlStore db.DB) {
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) { t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
sc := setupScenarioContext(t, url) sc := setupScenarioContext(t, url)
sc.sqlStore = sqlStore sc.sqlStore = sqlStore
@@ -186,7 +186,7 @@ type scenarioContext struct {
req *http.Request req *http.Request
url string url string
userAuthTokenService *authtest.FakeUserAuthTokenService userAuthTokenService *authtest.FakeUserAuthTokenService
sqlStore sqlstore.Store sqlStore db.DB
authInfoService *logintest.AuthInfoServiceFake authInfoService *logintest.AuthInfoServiceFake
dashboardVersionService dashver.Service dashboardVersionService dashver.Service
userService user.Service userService user.Service
@@ -376,7 +376,7 @@ func setupHTTPServerWithCfg(t *testing.T, useFakeAccessControl bool, cfg *settin
func setupHTTPServerWithCfgDb( func setupHTTPServerWithCfgDb(
t *testing.T, useFakeAccessControl bool, cfg *setting.Cfg, db *sqlstore.SQLStore, t *testing.T, useFakeAccessControl bool, cfg *setting.Cfg, db *sqlstore.SQLStore,
store sqlstore.Store, features *featuremgmt.FeatureManager, options ...APITestServerOption, store db.DB, features *featuremgmt.FeatureManager, options ...APITestServerOption,
) accessControlScenarioContext { ) accessControlScenarioContext {
t.Helper() t.Helper()
license := &licensing.OSSLicensingService{} license := &licensing.OSSLicensingService{}
+3 -4
View File
@@ -42,7 +42,6 @@ import (
"github.com/grafana/grafana/pkg/services/preference/preftest" "github.com/grafana/grafana/pkg/services/preference/preftest"
"github.com/grafana/grafana/pkg/services/provisioning" "github.com/grafana/grafana/pkg/services/provisioning"
"github.com/grafana/grafana/pkg/services/quota/quotatest" "github.com/grafana/grafana/pkg/services/quota/quotatest"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore" "github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
"github.com/grafana/grafana/pkg/services/tag/tagimpl" "github.com/grafana/grafana/pkg/services/tag/tagimpl"
"github.com/grafana/grafana/pkg/services/team/teamtest" "github.com/grafana/grafana/pkg/services/team/teamtest"
@@ -1114,7 +1113,7 @@ func postDashboardScenario(t *testing.T, desc string, url string, routePattern s
} }
func postValidateScenario(t *testing.T, desc string, url string, routePattern string, cmd models.ValidateDashboardCommand, func postValidateScenario(t *testing.T, desc string, url string, routePattern string, cmd models.ValidateDashboardCommand,
role org.RoleType, fn scenarioFunc, sqlmock sqlstore.Store) { role org.RoleType, fn scenarioFunc, sqlmock db.DB) {
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) { t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
cfg := setting.NewCfg() cfg := setting.NewCfg()
hs := HTTPServer{ hs := HTTPServer{
@@ -1150,7 +1149,7 @@ func postValidateScenario(t *testing.T, desc string, url string, routePattern st
} }
func postDiffScenario(t *testing.T, desc string, url string, routePattern string, cmd dtos.CalculateDiffOptions, func postDiffScenario(t *testing.T, desc string, url string, routePattern string, cmd dtos.CalculateDiffOptions,
role org.RoleType, fn scenarioFunc, sqlmock sqlstore.Store, fakeDashboardVersionService *dashvertest.FakeDashboardVersionService) { role org.RoleType, fn scenarioFunc, sqlmock db.DB, fakeDashboardVersionService *dashvertest.FakeDashboardVersionService) {
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) { t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
cfg := setting.NewCfg() cfg := setting.NewCfg()
hs := HTTPServer{ hs := HTTPServer{
@@ -1188,7 +1187,7 @@ func postDiffScenario(t *testing.T, desc string, url string, routePattern string
func restoreDashboardVersionScenario(t *testing.T, desc string, url string, routePattern string, func restoreDashboardVersionScenario(t *testing.T, desc string, url string, routePattern string,
mock *dashboards.FakeDashboardService, fakeDashboardVersionService *dashvertest.FakeDashboardVersionService, mock *dashboards.FakeDashboardService, fakeDashboardVersionService *dashvertest.FakeDashboardVersionService,
cmd dtos.RestoreDashboardVersionCommand, fn scenarioFunc, sqlStore sqlstore.Store) { cmd dtos.RestoreDashboardVersionCommand, fn scenarioFunc, sqlStore db.DB) {
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) { t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
cfg := setting.NewCfg() cfg := setting.NewCfg()
hs := HTTPServer{ hs := HTTPServer{
+6 -2
View File
@@ -4,7 +4,7 @@ import (
"context" "context"
"time" "time"
"github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/infra/db"
) )
func (hs *HTTPServer) databaseHealthy(ctx context.Context) bool { func (hs *HTTPServer) databaseHealthy(ctx context.Context) bool {
@@ -14,7 +14,11 @@ func (hs *HTTPServer) databaseHealthy(ctx context.Context) bool {
return cached.(bool) return cached.(bool)
} }
healthy := hs.SQLStore.GetDBHealthQuery(ctx, &models.GetDBHealthQuery{}) == nil err := hs.SQLStore.WithDbSession(ctx, func(session *db.Session) error {
_, err := session.Exec("SELECT 1")
return err
})
healthy := err == nil
hs.CacheService.Set(cacheKey, healthy, time.Second*5) hs.CacheService.Set(cacheKey, healthy, time.Second*5)
return healthy return healthy
+2 -1
View File
@@ -30,6 +30,7 @@ import (
"github.com/grafana/grafana/pkg/api/routing" "github.com/grafana/grafana/pkg/api/routing"
httpstatic "github.com/grafana/grafana/pkg/api/static" httpstatic "github.com/grafana/grafana/pkg/api/static"
"github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/kvstore" "github.com/grafana/grafana/pkg/infra/kvstore"
"github.com/grafana/grafana/pkg/infra/localcache" "github.com/grafana/grafana/pkg/infra/localcache"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
@@ -150,7 +151,7 @@ type HTTPServer struct {
QueryLibraryHTTPService querylibrary.HTTPService QueryLibraryHTTPService querylibrary.HTTPService
QueryLibraryService querylibrary.Service QueryLibraryService querylibrary.Service
ContextHandler *contexthandler.ContextHandler ContextHandler *contexthandler.ContextHandler
SQLStore sqlstore.Store SQLStore db.DB
AlertEngine *alerting.AlertEngine AlertEngine *alerting.AlertEngine
AlertNG *ngalert.AlertNG AlertNG *ngalert.AlertNG
LibraryPanelService librarypanels.Service LibraryPanelService librarypanels.Service
+1 -1
View File
@@ -122,7 +122,7 @@ func TestTeamMembersAPIEndpoint_userLoggedIn(t *testing.T) {
}) })
} }
func createUser(db sqlstore.Store, orgId int64, t *testing.T) int64 { func createUser(db db.DB, orgId int64, t *testing.T) int64 {
quotaService := quotaimpl.ProvideService(db, setting.NewCfg()) quotaService := quotaimpl.ProvideService(db, setting.NewCfg())
orgService, err := orgimpl.ProvideService(db, setting.NewCfg(), quotaService) orgService, err := orgimpl.ProvideService(db, setting.NewCfg(), quotaService)
require.NoError(t, err) require.NoError(t, err)
+1 -2
View File
@@ -302,8 +302,7 @@ var wireSet = wire.NewSet(
wire.Bind(new(notifications.WebhookSender), new(*notifications.NotificationServiceMock)), wire.Bind(new(notifications.WebhookSender), new(*notifications.NotificationServiceMock)),
wire.Bind(new(notifications.EmailSender), new(*notifications.NotificationServiceMock)), wire.Bind(new(notifications.EmailSender), new(*notifications.NotificationServiceMock)),
dbtest.NewFakeDB, dbtest.NewFakeDB,
wire.Bind(new(sqlstore.Store), new(*sqlstore.SQLStore)), wire.Bind(new(db.DB), new(*sqlstore.SQLStore)),
wire.Bind(new(db.DB), new(*dbtest.FakeDB)),
prefimpl.ProvideService, prefimpl.ProvideService,
opentsdb.ProvideService, opentsdb.ProvideService,
acimpl.ProvideAccessControl, acimpl.ProvideAccessControl,
@@ -8,6 +8,7 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/httpclient" "github.com/grafana/grafana/pkg/infra/httpclient"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/metrics" "github.com/grafana/grafana/pkg/infra/metrics"
@@ -18,14 +19,13 @@ import (
"github.com/grafana/grafana/pkg/registry" "github.com/grafana/grafana/pkg/registry"
"github.com/grafana/grafana/pkg/services/datasources" "github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/stats" "github.com/grafana/grafana/pkg/services/stats"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
) )
type Service struct { type Service struct {
cfg *setting.Cfg cfg *setting.Cfg
sqlstore sqlstore.Store sqlstore db.DB
plugins plugins.Store plugins plugins.Store
social social.Service social social.Service
usageStats usagestats.Service usageStats usagestats.Service
@@ -46,7 +46,7 @@ func ProvideService(
us usagestats.Service, us usagestats.Service,
statsService stats.Service, statsService stats.Service,
cfg *setting.Cfg, cfg *setting.Cfg,
store sqlstore.Store, store db.DB,
social social.Service, social social.Service,
plugins plugins.Store, plugins plugins.Store,
features *featuremgmt.FeatureManager, features *featuremgmt.FeatureManager,
@@ -14,6 +14,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/httpclient" "github.com/grafana/grafana/pkg/infra/httpclient"
"github.com/grafana/grafana/pkg/infra/usagestats" "github.com/grafana/grafana/pkg/infra/usagestats"
"github.com/grafana/grafana/pkg/login/social" "github.com/grafana/grafana/pkg/login/social"
@@ -22,7 +23,6 @@ import (
"github.com/grafana/grafana/pkg/registry" "github.com/grafana/grafana/pkg/registry"
"github.com/grafana/grafana/pkg/services/datasources" "github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore" "github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
"github.com/grafana/grafana/pkg/services/stats" "github.com/grafana/grafana/pkg/services/stats"
"github.com/grafana/grafana/pkg/services/stats/statstest" "github.com/grafana/grafana/pkg/services/stats/statstest"
@@ -446,7 +446,7 @@ func setupSomeDataSourcePlugins(t *testing.T, s *Service) {
} }
} }
func createService(t testing.TB, cfg *setting.Cfg, store sqlstore.Store, statsService stats.Service, opts ...func(*serviceOptions)) *Service { func createService(t testing.TB, cfg *setting.Cfg, store db.DB, statsService stats.Service, opts ...func(*serviceOptions)) *Service {
t.Helper() t.Helper()
o := &serviceOptions{datasources: mockDatasourceService{}} o := &serviceOptions{datasources: mockDatasourceService{}}
-2
View File
@@ -361,7 +361,6 @@ var wireSet = wire.NewSet(
wire.Bind(new(notifications.Service), new(*notifications.NotificationService)), wire.Bind(new(notifications.Service), new(*notifications.NotificationService)),
wire.Bind(new(notifications.WebhookSender), new(*notifications.NotificationService)), wire.Bind(new(notifications.WebhookSender), new(*notifications.NotificationService)),
wire.Bind(new(notifications.EmailSender), new(*notifications.NotificationService)), wire.Bind(new(notifications.EmailSender), new(*notifications.NotificationService)),
wire.Bind(new(sqlstore.Store), new(*sqlstore.SQLStore)),
wire.Bind(new(db.DB), new(*sqlstore.SQLStore)), wire.Bind(new(db.DB), new(*sqlstore.SQLStore)),
prefimpl.ProvideService, prefimpl.ProvideService,
oauthtoken.ProvideService, oauthtoken.ProvideService,
@@ -379,7 +378,6 @@ var wireTestSet = wire.NewSet(
wire.Bind(new(notifications.WebhookSender), new(*notifications.NotificationServiceMock)), wire.Bind(new(notifications.WebhookSender), new(*notifications.NotificationServiceMock)),
wire.Bind(new(notifications.EmailSender), new(*notifications.NotificationServiceMock)), wire.Bind(new(notifications.EmailSender), new(*notifications.NotificationServiceMock)),
mockstore.NewSQLStoreMock, mockstore.NewSQLStoreMock,
wire.Bind(new(sqlstore.Store), new(*sqlstore.SQLStore)),
wire.Bind(new(db.DB), new(*sqlstore.SQLStore)), wire.Bind(new(db.DB), new(*sqlstore.SQLStore)),
prefimpl.ProvideService, prefimpl.ProvideService,
oauthtoken.ProvideService, oauthtoken.ProvideService,
@@ -709,7 +709,7 @@ func TestIntegrationDashboard_Filter(t *testing.T) {
assert.Equal(t, dashB.Id, results[0].ID) assert.Equal(t, dashB.Id, results[0].ID)
} }
func insertTestRule(t *testing.T, sqlStore sqlstore.Store, foderOrgID int64, folderUID string) { func insertTestRule(t *testing.T, sqlStore db.DB, foderOrgID int64, folderUID string) {
err := sqlStore.WithDbSession(context.Background(), func(sess *db.Session) error { err := sqlStore.WithDbSession(context.Background(), func(sess *db.Session) error {
type alertQuery struct { type alertQuery struct {
RefID string RefID string
-26
View File
@@ -1,26 +0,0 @@
package sqlstore
import (
"context"
"xorm.io/core"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
"github.com/grafana/grafana/pkg/services/sqlstore/session"
)
type Store interface {
GetDialect() migrator.Dialect
GetDBType() core.DbType
WithDbSession(ctx context.Context, callback DBTransactionFunc) error
WithNewDbSession(ctx context.Context, callback DBTransactionFunc) error
WithTransactionalDbSession(ctx context.Context, callback DBTransactionFunc) error
InTransaction(ctx context.Context, fn func(ctx context.Context) error) error
Migrate(bool) error
Sync() error
Reset() error
Quote(value string) string
GetDBHealthQuery(ctx context.Context, query *models.GetDBHealthQuery) error
GetSqlxSession() *session.SessionDB
}