backend/sqlstore: move GetDashboards to Dashboard Service (#49175)
I also did some mild file renaming to try and get the dashboards package closer in line with the sqlstore split design doc.
This commit is contained in:
@@ -6,6 +6,8 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"xorm.io/xorm"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/infra/metrics"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
@@ -904,3 +906,23 @@ func (d *DashboardStore) GetDashboardUIDById(ctx context.Context, query *models.
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (d *DashboardStore) GetDashboards(ctx context.Context, query *models.GetDashboardsQuery) error {
|
||||
return d.sqlStore.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
if len(query.DashboardIds) == 0 && len(query.DashboardUIds) == 0 {
|
||||
return models.ErrCommandValidationFailed
|
||||
}
|
||||
|
||||
var dashboards = make([]*models.Dashboard, 0)
|
||||
var session *xorm.Session
|
||||
if len(query.DashboardIds) > 0 {
|
||||
session = sess.In("id", query.DashboardIds)
|
||||
} else {
|
||||
session = sess.In("uid", query.DashboardUIds)
|
||||
}
|
||||
|
||||
err := session.Find(&dashboards)
|
||||
query.Result = dashboards
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ func TestDashboardProvisioningTest(t *testing.T) {
|
||||
require.Nil(t, err)
|
||||
|
||||
query := &models.GetDashboardsQuery{DashboardIds: []int64{anotherDash.Id}}
|
||||
err = sqlStore.GetDashboards(context.Background(), query)
|
||||
err = dashboardStore.GetDashboards(context.Background(), query)
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, query.Result)
|
||||
|
||||
@@ -86,7 +86,7 @@ func TestDashboardProvisioningTest(t *testing.T) {
|
||||
require.Nil(t, dashboardStore.DeleteOrphanedProvisionedDashboards(context.Background(), deleteCmd))
|
||||
|
||||
query = &models.GetDashboardsQuery{DashboardIds: []int64{dash.Id, anotherDash.Id}}
|
||||
err = sqlStore.GetDashboards(context.Background(), query)
|
||||
err = dashboardStore.GetDashboards(context.Background(), query)
|
||||
require.Nil(t, err)
|
||||
|
||||
require.Equal(t, 1, len(query.Result))
|
||||
|
||||
+13
@@ -127,6 +127,19 @@ func TestDashboardDataAccess(t *testing.T) {
|
||||
require.Equal(t, err, models.ErrDashboardIdentifierNotSet)
|
||||
})
|
||||
|
||||
t.Run("Should be able to get dashboards by IDs & UIDs", func(t *testing.T) {
|
||||
setup()
|
||||
query := models.GetDashboardsQuery{DashboardIds: []int64{savedDash.Id, savedDash2.Id}}
|
||||
err := dashboardStore.GetDashboards(context.Background(), &query)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, len(query.Result), 2)
|
||||
|
||||
query = models.GetDashboardsQuery{DashboardUIds: []string{savedDash.Uid, savedDash2.Uid}}
|
||||
err = dashboardStore.GetDashboards(context.Background(), &query)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, len(query.Result), 2)
|
||||
})
|
||||
|
||||
t.Run("Should be able to delete dashboard", func(t *testing.T) {
|
||||
setup()
|
||||
dash := insertTestDashboard(t, dashboardStore, "delete me", 1, 0, false, "delete this")
|
||||
Reference in New Issue
Block a user