chore(dashboard version service): remove user table join from store implementations (#60779)

chore (dashboardversion service): remove (one) join from store implementations

We return the userID from the dashboardservice store; the service (or api) layer can use that to get the user's login when needed.
This commit is contained in:
Kristin Laemmert 2022-12-28 13:59:10 +01:00 committed by GitHub
parent 6e9419ea80
commit b356526ebe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 8 deletions

View File

@ -67,9 +67,9 @@ func (ss *sqlxStore) List(ctx context.Context, query *dashver.ListDashboardVersi
dashboard_version.restored_from,
dashboard_version.version,
dashboard_version.created,
dashboard_version.created_by,
dashboard_version.message
FROM dashboard_version
LEFT JOIN "user" ON "user".id = dashboard_version.created_by
LEFT JOIN dashboard ON dashboard.id = dashboard_version.dashboard_id
WHERE dashboard_version.dashboard_id=? AND dashboard.org_id=?
ORDER BY dashboard_version.version DESC

View File

@ -5,15 +5,15 @@ import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"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"
dashver "github.com/grafana/grafana/pkg/services/dashboardversion"
"github.com/grafana/grafana/pkg/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type getStore func(db.DB) store
@ -37,6 +37,7 @@ func testIntegrationGetDashboardVersion(t *testing.T, fn getStore) {
require.Nil(t, err)
assert.Equal(t, query.DashboardID, savedDash.Id)
assert.Equal(t, query.Version, savedDash.Version)
assert.Equal(t, createdById, res.CreatedBy)
dashCmd := &models.Dashboard{
Id: res.ID,
@ -48,6 +49,7 @@ func testIntegrationGetDashboardVersion(t *testing.T, fn getStore) {
assert.EqualValues(t, dashCmd.Data.Get("uid"), res.Data.Get("uid"))
assert.EqualValues(t, dashCmd.Data.Get("orgId"), res.Data.Get("orgId"))
assert.Equal(t, createdById, res.CreatedBy)
})
t.Run("Attempt to get a version that doesn't exist", func(t *testing.T) {
@ -128,6 +130,10 @@ func getDashboard(t *testing.T, sqlStore db.DB, dashboard *models.Dashboard) err
})
}
var (
createdById = int64(3)
)
func insertTestDashboard(t *testing.T, sqlStore db.DB, title string, orgId int64,
folderId int64, isFolder bool, tags ...interface{}) *models.Dashboard {
t.Helper()
@ -140,6 +146,7 @@ func insertTestDashboard(t *testing.T, sqlStore db.DB, title string, orgId int64
"title": title,
"tags": tags,
}),
UserId: createdById,
}
var dash *models.Dashboard

View File

@ -81,11 +81,9 @@ func (ss *sqlStore) List(ctx context.Context, query *dashver.ListDashboardVersio
dashboard_version.restored_from,
dashboard_version.version,
dashboard_version.created,
dashboard_version.created_by as created_by_id,
dashboard_version.created_by,
dashboard_version.message,
dashboard_version.data,`+
ss.dialect.Quote("user")+`.login as created_by`).
Join("LEFT", ss.dialect.Quote("user"), `dashboard_version.created_by = `+ss.dialect.Quote("user")+`.id`).
dashboard_version.data`).
Join("LEFT", "dashboard", `dashboard.id = dashboard_version.dashboard_id`).
Where("dashboard_version.dashboard_id=? AND dashboard.org_id=?", query.DashboardID, query.OrgID).
OrderBy("dashboard_version.version DESC").