dashboards: created/updated and createdby/updatedby should be set before save

If creating a new folder/dashboard created/updated and createdby/updatedby should always be set.
If updating an existing folder/dashboard updated and updatedby should always be set.
This commit is contained in:
Marcus Efraimsson 2018-02-22 11:54:28 +01:00
parent 20ebb614f6
commit 3f588b4c48
3 changed files with 40 additions and 9 deletions

View File

@ -166,10 +166,6 @@ func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
userId = -1
}
if dash.Data.Get("version").MustInt(0) == 0 {
dash.CreatedBy = userId
}
dash.UpdatedBy = userId
dash.OrgId = cmd.OrgId
dash.PluginId = cmd.PluginId

View File

@ -37,6 +37,12 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error {
func saveDashboard(sess *DBSession, cmd *m.SaveDashboardCommand) error {
dash := cmd.GetDashboardModel()
userId := cmd.UserId
if userId == 0 {
userId = -1
}
if dash.Id > 0 {
var existing m.Dashboard
dashWithIdExists, err := sess.Where("id=? AND org_id=?", dash.Id, dash.OrgId).Get(&existing)
@ -60,8 +66,6 @@ func saveDashboard(sess *DBSession, cmd *m.SaveDashboardCommand) error {
if existing.PluginId != "" && cmd.Overwrite == false {
return m.UpdatePluginDashboardError{PluginId: existing.PluginId}
}
} else {
dash.Created = time.Now()
}
if dash.Uid == "" {
@ -78,17 +82,23 @@ func saveDashboard(sess *DBSession, cmd *m.SaveDashboardCommand) error {
if dash.Id == 0 {
dash.SetVersion(1)
dash.Created = time.Now()
dash.CreatedBy = userId
dash.Updated = time.Now()
dash.UpdatedBy = userId
metrics.M_Api_Dashboard_Insert.Inc()
affectedRows, err = sess.Insert(dash)
} else {
v := dash.Version
v++
dash.SetVersion(v)
dash.SetVersion(dash.Version + 1)
if !cmd.UpdatedAt.IsZero() {
dash.Updated = cmd.UpdatedAt
} else {
dash.Updated = time.Now()
}
dash.UpdatedBy = userId
affectedRows, err = sess.MustCols("folder_id").ID(dash.Id).Update(dash)
}

View File

@ -3,6 +3,7 @@ package sqlstore
import (
"fmt"
"testing"
"time"
"github.com/go-xorm/xorm"
"github.com/grafana/grafana/pkg/components/simplejson"
@ -124,6 +125,24 @@ func TestDashboardDataAccess(t *testing.T) {
generateNewUid = util.GenerateShortUid
})
Convey("Should be able to create dashboard", func() {
cmd := m.SaveDashboardCommand{
OrgId: 1,
Dashboard: simplejson.NewFromAny(map[string]interface{}{
"title": "folderId",
"tags": []interface{}{},
}),
UserId: 100,
}
err := SaveDashboard(&cmd)
So(err, ShouldBeNil)
So(cmd.Result.CreatedBy, ShouldEqual, 100)
So(cmd.Result.Created.IsZero(), ShouldBeFalse)
So(cmd.Result.UpdatedBy, ShouldEqual, 100)
So(cmd.Result.Updated.IsZero(), ShouldBeFalse)
})
Convey("Should be able to update dashboard by id and remove folderId", func() {
cmd := m.SaveDashboardCommand{
OrgId: 1,
@ -134,6 +153,7 @@ func TestDashboardDataAccess(t *testing.T) {
}),
Overwrite: true,
FolderId: 2,
UserId: 100,
}
err := SaveDashboard(&cmd)
@ -149,6 +169,7 @@ func TestDashboardDataAccess(t *testing.T) {
}),
FolderId: 0,
Overwrite: true,
UserId: 100,
}
err = SaveDashboard(&cmd)
@ -162,6 +183,10 @@ func TestDashboardDataAccess(t *testing.T) {
err = GetDashboard(&query)
So(err, ShouldBeNil)
So(query.Result.FolderId, ShouldEqual, 0)
So(query.Result.CreatedBy, ShouldEqual, savedDash.CreatedBy)
So(query.Result.Created, ShouldEqual, savedDash.Created.Truncate(time.Second))
So(query.Result.UpdatedBy, ShouldEqual, 100)
So(query.Result.Updated.IsZero(), ShouldBeFalse)
})
Convey("Should be able to delete a dashboard folder and its children", func() {