mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
chore: remove redundant transactions from Dashboard (get) sql queries (#89186)
* chore: remove redundant transactions from sql queries * remove unused parameter
This commit is contained in:
parent
57b760824f
commit
e2f65dda0c
@ -17,7 +17,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/services/folder"
|
"github.com/grafana/grafana/pkg/services/folder"
|
||||||
"github.com/grafana/grafana/pkg/services/quota"
|
"github.com/grafana/grafana/pkg/services/quota"
|
||||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||||
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
|
||||||
"github.com/grafana/grafana/pkg/services/sqlstore/permissions"
|
"github.com/grafana/grafana/pkg/services/sqlstore/permissions"
|
||||||
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
|
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
|
||||||
"github.com/grafana/grafana/pkg/services/star"
|
"github.com/grafana/grafana/pkg/services/star"
|
||||||
@ -72,12 +71,12 @@ func (d *dashboardStore) ValidateDashboardBeforeSave(ctx context.Context, dashbo
|
|||||||
isParentFolderChanged := false
|
isParentFolderChanged := false
|
||||||
err := d.store.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
|
err := d.store.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
|
||||||
var err error
|
var err error
|
||||||
isParentFolderChanged, err = getExistingDashboardByIDOrUIDForUpdate(sess, dashboard, d.store.GetDialect(), overwrite)
|
isParentFolderChanged, err = getExistingDashboardByIDOrUIDForUpdate(sess, dashboard, overwrite)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
isParentFolderChanged, err = getExistingDashboardByTitleAndFolder(sess, dashboard, d.store.GetDialect(), overwrite,
|
isParentFolderChanged, err = getExistingDashboardByTitleAndFolder(sess, dashboard, overwrite,
|
||||||
isParentFolderChanged)
|
isParentFolderChanged)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -94,7 +93,7 @@ func (d *dashboardStore) ValidateDashboardBeforeSave(ctx context.Context, dashbo
|
|||||||
|
|
||||||
func (d *dashboardStore) GetProvisionedDataByDashboardID(ctx context.Context, dashboardID int64) (*dashboards.DashboardProvisioning, error) {
|
func (d *dashboardStore) GetProvisionedDataByDashboardID(ctx context.Context, dashboardID int64) (*dashboards.DashboardProvisioning, error) {
|
||||||
var data dashboards.DashboardProvisioning
|
var data dashboards.DashboardProvisioning
|
||||||
err := d.store.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
|
err := d.store.WithDbSession(ctx, func(sess *db.Session) error {
|
||||||
_, err := sess.Where("dashboard_id = ?", dashboardID).Get(&data)
|
_, err := sess.Where("dashboard_id = ?", dashboardID).Get(&data)
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
@ -107,7 +106,7 @@ func (d *dashboardStore) GetProvisionedDataByDashboardID(ctx context.Context, da
|
|||||||
|
|
||||||
func (d *dashboardStore) GetProvisionedDataByDashboardUID(ctx context.Context, orgID int64, dashboardUID string) (*dashboards.DashboardProvisioning, error) {
|
func (d *dashboardStore) GetProvisionedDataByDashboardUID(ctx context.Context, orgID int64, dashboardUID string) (*dashboards.DashboardProvisioning, error) {
|
||||||
var provisionedDashboard dashboards.DashboardProvisioning
|
var provisionedDashboard dashboards.DashboardProvisioning
|
||||||
err := d.store.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
|
err := d.store.WithDbSession(ctx, func(sess *db.Session) error {
|
||||||
var dashboard dashboards.Dashboard
|
var dashboard dashboards.Dashboard
|
||||||
exists, err := sess.Where("org_id = ? AND uid = ?", orgID, dashboardUID).Get(&dashboard)
|
exists, err := sess.Where("org_id = ? AND uid = ?", orgID, dashboardUID).Get(&dashboard)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -130,7 +129,7 @@ func (d *dashboardStore) GetProvisionedDataByDashboardUID(ctx context.Context, o
|
|||||||
|
|
||||||
func (d *dashboardStore) GetProvisionedDashboardData(ctx context.Context, name string) ([]*dashboards.DashboardProvisioning, error) {
|
func (d *dashboardStore) GetProvisionedDashboardData(ctx context.Context, name string) ([]*dashboards.DashboardProvisioning, error) {
|
||||||
var result []*dashboards.DashboardProvisioning
|
var result []*dashboards.DashboardProvisioning
|
||||||
err := d.store.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
|
err := d.store.WithDbSession(ctx, func(sess *db.Session) error {
|
||||||
return sess.Where("name = ?", name).Find(&result)
|
return sess.Where("name = ?", name).Find(&result)
|
||||||
})
|
})
|
||||||
return result, err
|
return result, err
|
||||||
@ -248,7 +247,7 @@ func (d *dashboardStore) Count(ctx context.Context, scopeParams *quota.ScopePara
|
|||||||
return u, nil
|
return u, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getExistingDashboardByIDOrUIDForUpdate(sess *db.Session, dash *dashboards.Dashboard, dialect migrator.Dialect, overwrite bool) (bool, error) {
|
func getExistingDashboardByIDOrUIDForUpdate(sess *db.Session, dash *dashboards.Dashboard, overwrite bool) (bool, error) {
|
||||||
dashWithIdExists := false
|
dashWithIdExists := false
|
||||||
isParentFolderChanged := false
|
isParentFolderChanged := false
|
||||||
var existingById dashboards.Dashboard
|
var existingById dashboards.Dashboard
|
||||||
@ -323,7 +322,7 @@ func getExistingDashboardByIDOrUIDForUpdate(sess *db.Session, dash *dashboards.D
|
|||||||
}
|
}
|
||||||
|
|
||||||
// getExistingDashboardByTitleAndFolder returns a boolean (on whether the parent folder changed) and an error for if the dashboard already exists.
|
// getExistingDashboardByTitleAndFolder returns a boolean (on whether the parent folder changed) and an error for if the dashboard already exists.
|
||||||
func getExistingDashboardByTitleAndFolder(sess *db.Session, dash *dashboards.Dashboard, dialect migrator.Dialect, overwrite,
|
func getExistingDashboardByTitleAndFolder(sess *db.Session, dash *dashboards.Dashboard, overwrite,
|
||||||
isParentFolderChanged bool) (bool, error) {
|
isParentFolderChanged bool) (bool, error) {
|
||||||
var existing dashboards.Dashboard
|
var existing dashboards.Dashboard
|
||||||
condition := "org_id=? AND title=?"
|
condition := "org_id=? AND title=?"
|
||||||
|
@ -787,7 +787,7 @@ func TestGetExistingDashboardByTitleAndFolder(t *testing.T) {
|
|||||||
insertTestDashboard(t, dashboardStore, "Apple", 1, 0, "", false)
|
insertTestDashboard(t, dashboardStore, "Apple", 1, 0, "", false)
|
||||||
t.Run("Finds a dashboard with existing name in root directory and throws DashboardWithSameNameInFolderExists error", func(t *testing.T) {
|
t.Run("Finds a dashboard with existing name in root directory and throws DashboardWithSameNameInFolderExists error", func(t *testing.T) {
|
||||||
err = sqlStore.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
err = sqlStore.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
||||||
_, err = getExistingDashboardByTitleAndFolder(sess, &dashboards.Dashboard{Title: "Apple", OrgID: 1}, sqlStore.GetDialect(), false, false)
|
_, err = getExistingDashboardByTitleAndFolder(sess, &dashboards.Dashboard{Title: "Apple", OrgID: 1}, false, false)
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
require.ErrorIs(t, err, dashboards.ErrDashboardWithSameNameInFolderExists)
|
require.ErrorIs(t, err, dashboards.ErrDashboardWithSameNameInFolderExists)
|
||||||
@ -795,7 +795,7 @@ func TestGetExistingDashboardByTitleAndFolder(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("Returns no error when dashboard does not exist in root folder", func(t *testing.T) {
|
t.Run("Returns no error when dashboard does not exist in root folder", func(t *testing.T) {
|
||||||
err = sqlStore.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
err = sqlStore.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
||||||
_, err = getExistingDashboardByTitleAndFolder(sess, &dashboards.Dashboard{Title: "Beta", OrgID: 1}, sqlStore.GetDialect(), false, false)
|
_, err = getExistingDashboardByTitleAndFolder(sess, &dashboards.Dashboard{Title: "Beta", OrgID: 1}, false, false)
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@ -805,7 +805,7 @@ func TestGetExistingDashboardByTitleAndFolder(t *testing.T) {
|
|||||||
savedFolder := insertTestDashboard(t, dashboardStore, "test dash folder", 1, 0, "", true, "prod", "webapp")
|
savedFolder := insertTestDashboard(t, dashboardStore, "test dash folder", 1, 0, "", true, "prod", "webapp")
|
||||||
savedDash := insertTestDashboard(t, dashboardStore, "test dash", 1, savedFolder.ID, savedFolder.UID, false, "prod", "webapp")
|
savedDash := insertTestDashboard(t, dashboardStore, "test dash", 1, savedFolder.ID, savedFolder.UID, false, "prod", "webapp")
|
||||||
err = sqlStore.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
err = sqlStore.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
||||||
_, err = getExistingDashboardByTitleAndFolder(sess, &dashboards.Dashboard{Title: savedDash.Title, FolderUID: savedFolder.UID, OrgID: 1}, sqlStore.GetDialect(), false, false)
|
_, err = getExistingDashboardByTitleAndFolder(sess, &dashboards.Dashboard{Title: savedDash.Title, FolderUID: savedFolder.UID, OrgID: 1}, false, false)
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
require.ErrorIs(t, err, dashboards.ErrDashboardWithSameNameInFolderExists)
|
require.ErrorIs(t, err, dashboards.ErrDashboardWithSameNameInFolderExists)
|
||||||
|
Loading…
Reference in New Issue
Block a user