Search: Move entity events to transaction (#52394)
This commit is contained in:
@@ -21,7 +21,7 @@ func TestIntegrationDashboardACLDataAccess(t *testing.T) {
|
||||
|
||||
setup := func(t *testing.T) {
|
||||
sqlStore = sqlstore.InitTestDB(t)
|
||||
dashboardStore = ProvideDashboardStore(sqlStore)
|
||||
dashboardStore = ProvideDashboardStore(sqlStore, testFeatureToggles)
|
||||
currentUser = createUser(t, sqlStore, "viewer", "Viewer", false)
|
||||
savedFolder = insertTestDashboard(t, dashboardStore, "1 test dash folder", 1, 0, true, "prod", "webapp")
|
||||
childDash = insertTestDashboard(t, dashboardStore, "2 test dash", 1, savedFolder.Id, false, "prod", "webapp")
|
||||
|
||||
@@ -14,10 +14,12 @@ import (
|
||||
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
dashver "github.com/grafana/grafana/pkg/services/dashboardversion"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"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/searchstore"
|
||||
"github.com/grafana/grafana/pkg/services/store"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
@@ -25,13 +27,18 @@ type DashboardStore struct {
|
||||
sqlStore *sqlstore.SQLStore
|
||||
log log.Logger
|
||||
dialect migrator.Dialect
|
||||
features featuremgmt.FeatureToggles
|
||||
}
|
||||
|
||||
// DashboardStore implements the Store interface
|
||||
var _ dashboards.Store = (*DashboardStore)(nil)
|
||||
|
||||
func ProvideDashboardStore(sqlStore *sqlstore.SQLStore) *DashboardStore {
|
||||
return &DashboardStore{sqlStore: sqlStore, log: log.New("dashboard-store"), dialect: sqlStore.Dialect}
|
||||
func ProvideDashboardStore(sqlStore *sqlstore.SQLStore, features featuremgmt.FeatureToggles) *DashboardStore {
|
||||
return &DashboardStore{sqlStore: sqlStore, log: log.New("dashboard-store"), dialect: sqlStore.Dialect, features: features}
|
||||
}
|
||||
|
||||
func (d *DashboardStore) emitEntityEvent() bool {
|
||||
return d.features != nil && d.features.IsEnabled(featuremgmt.FlagPanelTitleSearch)
|
||||
}
|
||||
|
||||
func (d *DashboardStore) ValidateDashboardBeforeSave(dashboard *models.Dashboard, overwrite bool) (bool, error) {
|
||||
@@ -171,7 +178,7 @@ func (d *DashboardStore) GetProvisionedDashboardData(name string) ([]*models.Das
|
||||
|
||||
func (d *DashboardStore) SaveProvisionedDashboard(cmd models.SaveDashboardCommand, provisioning *models.DashboardProvisioning) (*models.Dashboard, error) {
|
||||
err := d.sqlStore.WithTransactionalDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
||||
if err := saveDashboard(sess, &cmd); err != nil {
|
||||
if err := saveDashboard(sess, &cmd, d.emitEntityEvent()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -187,7 +194,7 @@ func (d *DashboardStore) SaveProvisionedDashboard(cmd models.SaveDashboardComman
|
||||
|
||||
func (d *DashboardStore) SaveDashboard(cmd models.SaveDashboardCommand) (*models.Dashboard, error) {
|
||||
err := d.sqlStore.WithTransactionalDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
||||
return saveDashboard(sess, &cmd)
|
||||
return saveDashboard(sess, &cmd, d.emitEntityEvent())
|
||||
})
|
||||
return cmd.Result, err
|
||||
}
|
||||
@@ -396,7 +403,7 @@ func getExistingDashboardByTitleAndFolder(sess *sqlstore.DBSession, dash *models
|
||||
return isParentFolderChanged, nil
|
||||
}
|
||||
|
||||
func saveDashboard(sess *sqlstore.DBSession, cmd *models.SaveDashboardCommand) error {
|
||||
func saveDashboard(sess *sqlstore.DBSession, cmd *models.SaveDashboardCommand, emitEntityEvent bool) error {
|
||||
dash := cmd.GetDashboardModel()
|
||||
|
||||
userId := cmd.UserId
|
||||
@@ -507,6 +514,12 @@ func saveDashboard(sess *sqlstore.DBSession, cmd *models.SaveDashboardCommand) e
|
||||
|
||||
cmd.Result = dash
|
||||
|
||||
if emitEntityEvent {
|
||||
_, err := sess.Insert(createEntityEvent(dash, store.EntityEventTypeUpdate))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -698,11 +711,11 @@ func (d *DashboardStore) GetDashboardsByPluginID(ctx context.Context, query *mod
|
||||
|
||||
func (d *DashboardStore) DeleteDashboard(ctx context.Context, cmd *models.DeleteDashboardCommand) error {
|
||||
return d.sqlStore.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
return d.deleteDashboard(cmd, sess)
|
||||
return d.deleteDashboard(cmd, sess, d.emitEntityEvent())
|
||||
})
|
||||
}
|
||||
|
||||
func (d *DashboardStore) deleteDashboard(cmd *models.DeleteDashboardCommand, sess *sqlstore.DBSession) error {
|
||||
func (d *DashboardStore) deleteDashboard(cmd *models.DeleteDashboardCommand, sess *sqlstore.DBSession, emitEntityEvent bool) error {
|
||||
dashboard := models.Dashboard{Id: cmd.Id, OrgId: cmd.OrgId}
|
||||
has, err := sess.Get(&dashboard)
|
||||
if err != nil {
|
||||
@@ -812,9 +825,33 @@ func (d *DashboardStore) deleteDashboard(cmd *models.DeleteDashboardCommand, ses
|
||||
}
|
||||
}
|
||||
|
||||
if emitEntityEvent {
|
||||
_, err := sess.Insert(createEntityEvent(&dashboard, store.EntityEventTypeDelete))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func createEntityEvent(dashboard *models.Dashboard, eventType store.EntityEventType) *store.EntityEvent {
|
||||
var entityEvent *store.EntityEvent
|
||||
if dashboard.IsFolder {
|
||||
entityEvent = &store.EntityEvent{
|
||||
EventType: eventType,
|
||||
EntityId: store.CreateDatabaseEntityId(dashboard.Uid, dashboard.OrgId, store.EntityTypeFolder),
|
||||
Created: time.Now().Unix(),
|
||||
}
|
||||
} else {
|
||||
entityEvent = &store.EntityEvent{
|
||||
EventType: eventType,
|
||||
EntityId: store.CreateDatabaseEntityId(dashboard.Uid, dashboard.OrgId, store.EntityTypeDashboard),
|
||||
Created: time.Now().Unix(),
|
||||
}
|
||||
}
|
||||
return entityEvent
|
||||
}
|
||||
|
||||
func (d *DashboardStore) deleteAlertDefinition(dashboardId int64, sess *sqlstore.DBSession) error {
|
||||
alerts := make([]*models.Alert, 0)
|
||||
if err := sess.Where("dashboard_id = ?", dashboardId).Find(&alerts); err != nil {
|
||||
|
||||
@@ -9,10 +9,13 @@ import (
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
)
|
||||
|
||||
var testFeatureToggles = featuremgmt.WithFeatures(featuremgmt.FlagPanelTitleSearch)
|
||||
|
||||
func TestIntegrationDashboardFolderDataAccess(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test")
|
||||
@@ -26,7 +29,7 @@ func TestIntegrationDashboardFolderDataAccess(t *testing.T) {
|
||||
setup := func() {
|
||||
sqlStore = sqlstore.InitTestDB(t)
|
||||
sqlStore.Cfg.RBACEnabled = false
|
||||
dashboardStore = ProvideDashboardStore(sqlStore)
|
||||
dashboardStore = ProvideDashboardStore(sqlStore, testFeatureToggles)
|
||||
folder = insertTestDashboard(t, dashboardStore, "1 test dash folder", 1, 0, true, "prod", "webapp")
|
||||
dashInRoot = insertTestDashboard(t, dashboardStore, "test dash 67", 1, 0, false, "prod", "webapp")
|
||||
childDash = insertTestDashboard(t, dashboardStore, "test dash 23", 1, folder.Id, false, "prod", "webapp")
|
||||
@@ -179,7 +182,7 @@ func TestIntegrationDashboardFolderDataAccess(t *testing.T) {
|
||||
|
||||
setup2 := func() {
|
||||
sqlStore = sqlstore.InitTestDB(t)
|
||||
dashboardStore := ProvideDashboardStore(sqlStore)
|
||||
dashboardStore := ProvideDashboardStore(sqlStore, testFeatureToggles)
|
||||
folder1 = insertTestDashboard(t, dashboardStore, "1 test dash folder", 1, 0, true, "prod")
|
||||
folder2 = insertTestDashboard(t, dashboardStore, "2 test dash folder", 1, 0, true, "prod")
|
||||
dashInRoot = insertTestDashboard(t, dashboardStore, "test dash 67", 1, 0, false, "prod")
|
||||
@@ -284,7 +287,7 @@ func TestIntegrationDashboardFolderDataAccess(t *testing.T) {
|
||||
|
||||
setup3 := func() {
|
||||
sqlStore = sqlstore.InitTestDB(t)
|
||||
dashboardStore := ProvideDashboardStore(sqlStore)
|
||||
dashboardStore := ProvideDashboardStore(sqlStore, testFeatureToggles)
|
||||
folder1 = insertTestDashboard(t, dashboardStore, "1 test dash folder", 1, 0, true, "prod")
|
||||
folder2 = insertTestDashboard(t, dashboardStore, "2 test dash folder", 1, 0, true, "prod")
|
||||
insertTestDashboard(t, dashboardStore, "folder in another org", 2, 0, true, "prod")
|
||||
@@ -466,7 +469,7 @@ func TestIntegrationDashboardFolderDataAccess(t *testing.T) {
|
||||
var sqlStore *sqlstore.SQLStore
|
||||
var folder1, folder2 *models.Dashboard
|
||||
sqlStore = sqlstore.InitTestDB(t)
|
||||
dashboardStore := ProvideDashboardStore(sqlStore)
|
||||
dashboardStore := ProvideDashboardStore(sqlStore, testFeatureToggles)
|
||||
folder2 = insertTestDashboard(t, dashboardStore, "TEST", orgId, 0, true, "prod")
|
||||
_ = insertTestDashboard(t, dashboardStore, title, orgId, folder2.Id, false, "prod")
|
||||
folder1 = insertTestDashboard(t, dashboardStore, title, orgId, 0, true, "prod")
|
||||
@@ -481,7 +484,7 @@ func TestIntegrationDashboardFolderDataAccess(t *testing.T) {
|
||||
t.Run("GetFolderByUID", func(t *testing.T) {
|
||||
var orgId int64 = 1
|
||||
sqlStore := sqlstore.InitTestDB(t)
|
||||
dashboardStore := ProvideDashboardStore(sqlStore)
|
||||
dashboardStore := ProvideDashboardStore(sqlStore, testFeatureToggles)
|
||||
folder := insertTestDashboard(t, dashboardStore, "TEST", orgId, 0, true, "prod")
|
||||
dash := insertTestDashboard(t, dashboardStore, "Very Unique Name", orgId, folder.Id, false, "prod")
|
||||
|
||||
@@ -505,7 +508,7 @@ func TestIntegrationDashboardFolderDataAccess(t *testing.T) {
|
||||
t.Run("GetFolderByID", func(t *testing.T) {
|
||||
var orgId int64 = 1
|
||||
sqlStore := sqlstore.InitTestDB(t)
|
||||
dashboardStore := ProvideDashboardStore(sqlStore)
|
||||
dashboardStore := ProvideDashboardStore(sqlStore, testFeatureToggles)
|
||||
folder := insertTestDashboard(t, dashboardStore, "TEST", orgId, 0, true, "prod")
|
||||
dash := insertTestDashboard(t, dashboardStore, "Very Unique Name", orgId, folder.Id, false, "prod")
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ func TestIntegrationDashboardProvisioningTest(t *testing.T) {
|
||||
t.Skip("skipping integration test")
|
||||
}
|
||||
sqlStore := sqlstore.InitTestDB(t)
|
||||
dashboardStore := ProvideDashboardStore(sqlStore)
|
||||
dashboardStore := ProvideDashboardStore(sqlStore, testFeatureToggles)
|
||||
|
||||
folderCmd := models.SaveDashboardCommand{
|
||||
OrgId: 1,
|
||||
|
||||
@@ -33,7 +33,7 @@ func TestIntegrationDashboardDataAccess(t *testing.T) {
|
||||
setup := func() {
|
||||
sqlStore = sqlstore.InitTestDB(t)
|
||||
starService = starimpl.ProvideService(sqlStore)
|
||||
dashboardStore = ProvideDashboardStore(sqlStore)
|
||||
dashboardStore = ProvideDashboardStore(sqlStore, testFeatureToggles)
|
||||
savedFolder = insertTestDashboard(t, dashboardStore, "1 test dash folder", 1, 0, true, "prod", "webapp")
|
||||
savedDash = insertTestDashboard(t, dashboardStore, "test dash 23", 1, savedFolder.Id, false, "prod", "webapp")
|
||||
insertTestDashboard(t, dashboardStore, "test dash 45", 1, savedFolder.Id, false, "prod")
|
||||
@@ -484,7 +484,7 @@ func TestIntegrationDashboardDataAccessGivenPluginWithImportedDashboards(t *test
|
||||
t.Skip("skipping integration test")
|
||||
}
|
||||
sqlStore := sqlstore.InitTestDB(t)
|
||||
dashboardStore := ProvideDashboardStore(sqlStore)
|
||||
dashboardStore := ProvideDashboardStore(sqlStore, testFeatureToggles)
|
||||
pluginId := "test-app"
|
||||
|
||||
appFolder := insertTestDashboardForPlugin(t, dashboardStore, "app-test", 1, 0, true, pluginId)
|
||||
@@ -506,7 +506,7 @@ func TestIntegrationDashboard_SortingOptions(t *testing.T) {
|
||||
t.Skip("skipping integration test")
|
||||
}
|
||||
sqlStore := sqlstore.InitTestDB(t)
|
||||
dashboardStore := ProvideDashboardStore(sqlStore)
|
||||
dashboardStore := ProvideDashboardStore(sqlStore, testFeatureToggles)
|
||||
|
||||
dashB := insertTestDashboard(t, dashboardStore, "Beta", 1, 0, false)
|
||||
dashA := insertTestDashboard(t, dashboardStore, "Alfa", 1, 0, false)
|
||||
@@ -555,7 +555,7 @@ func TestIntegrationDashboard_Filter(t *testing.T) {
|
||||
t.Skip("skipping integration test")
|
||||
}
|
||||
sqlStore := sqlstore.InitTestDB(t)
|
||||
dashboardStore := ProvideDashboardStore(sqlStore)
|
||||
dashboardStore := ProvideDashboardStore(sqlStore, testFeatureToggles)
|
||||
insertTestDashboard(t, dashboardStore, "Alfa", 1, 0, false)
|
||||
dashB := insertTestDashboard(t, dashboardStore, "Beta", 1, 0, false)
|
||||
qNoFilter := &models.FindPersistedDashboardsQuery{
|
||||
|
||||
Reference in New Issue
Block a user