diff --git a/pkg/models/dashboards_public.go b/pkg/models/dashboards_public.go index a25796b7ede..4835b1eabad 100644 --- a/pkg/models/dashboards_public.go +++ b/pkg/models/dashboards_public.go @@ -32,7 +32,7 @@ var ( ) type PublicDashboard struct { - Uid string `json:"uid" xorm:"uid"` + Uid string `json:"uid" xorm:"pk uid"` DashboardUid string `json:"dashboardUid" xorm:"dashboard_uid"` OrgId int64 `json:"-" xorm:"org_id"` // Don't ever marshal orgId to Json TimeSettings *simplejson.Json `json:"timeSettings" xorm:"time_settings"` diff --git a/pkg/services/dashboards/database/database_dashboard_public.go b/pkg/services/dashboards/database/database_dashboard_public.go index 9003b879f5e..0c5ff91aa70 100644 --- a/pkg/services/dashboards/database/database_dashboard_public.go +++ b/pkg/services/dashboards/database/database_dashboard_public.go @@ -124,7 +124,7 @@ func (d *DashboardStore) SavePublicDashboardConfig(ctx context.Context, cmd mode // updates existing public dashboard configuration func (d *DashboardStore) UpdatePublicDashboardConfig(ctx context.Context, cmd models.SavePublicDashboardConfigCommand) error { err := d.sqlStore.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error { - _, err := sess.UseBool("is_enabled").Update(&cmd.PublicDashboard) + _, err := sess.ID(cmd.PublicDashboard.Uid).UseBool("is_enabled").Update(&cmd.PublicDashboard) if err != nil { return err } diff --git a/pkg/services/dashboards/database/database_dashboard_public_test.go b/pkg/services/dashboards/database/database_dashboard_public_test.go index a897cd19c2b..268781cef1d 100644 --- a/pkg/services/dashboards/database/database_dashboard_public_test.go +++ b/pkg/services/dashboards/database/database_dashboard_public_test.go @@ -190,19 +190,37 @@ func TestIntegrationUpdatePublicDashboard(t *testing.T) { var sqlStore *sqlstore.SQLStore var dashboardStore *DashboardStore var savedDashboard *models.Dashboard + var anotherSavedDashboard *models.Dashboard setup := func() { sqlStore = sqlstore.InitTestDB(t, sqlstore.InitTestDBOpt{FeatureFlags: []string{featuremgmt.FlagPublicDashboards}}) dashboardStore = ProvideDashboardStore(sqlStore) savedDashboard = insertTestDashboard(t, dashboardStore, "testDashie", 1, 0, true) + anotherSavedDashboard = insertTestDashboard(t, dashboardStore, "test another Dashie", 1, 0, true) } t.Run("updates an existing dashboard", func(t *testing.T) { setup() - pdUid := "asdf1234" - + // inserting two different public dashboards to test update works and only affect the desired pd by uid + anotherPdUid := "anotherUid" _, err := dashboardStore.SavePublicDashboardConfig(context.Background(), models.SavePublicDashboardConfigCommand{ + DashboardUid: anotherSavedDashboard.Uid, + OrgId: anotherSavedDashboard.OrgId, + PublicDashboard: models.PublicDashboard{ + Uid: anotherPdUid, + DashboardUid: anotherSavedDashboard.Uid, + OrgId: anotherSavedDashboard.OrgId, + IsEnabled: true, + CreatedAt: DefaultTime, + CreatedBy: 7, + AccessToken: "fakeaccesstoken", + }, + }) + require.NoError(t, err) + + pdUid := "asdf1234" + _, err = dashboardStore.SavePublicDashboardConfig(context.Background(), models.SavePublicDashboardConfigCommand{ DashboardUid: savedDashboard.Uid, OrgId: savedDashboard.OrgId, PublicDashboard: models.PublicDashboard{ @@ -234,6 +252,7 @@ func TestIntegrationUpdatePublicDashboard(t *testing.T) { }) require.NoError(t, err) + // updated dashboard should have changed pdRetrieved, err := dashboardStore.GetPublicDashboardConfig(context.Background(), savedDashboard.OrgId, savedDashboard.Uid) require.NoError(t, err) @@ -241,5 +260,12 @@ func TestIntegrationUpdatePublicDashboard(t *testing.T) { // make sure we're correctly updated IsEnabled because we have to call // UseBool with xorm assert.Equal(t, updatedPublicDashboard.IsEnabled, pdRetrieved.IsEnabled) + + // not updated dashboard shouldn't have changed + pdNotUpdatedRetrieved, err := dashboardStore.GetPublicDashboardConfig(context.Background(), anotherSavedDashboard.OrgId, anotherSavedDashboard.Uid) + require.NoError(t, err) + + assert.NotEqual(t, updatedPublicDashboard.UpdatedAt, pdNotUpdatedRetrieved.UpdatedAt) + assert.NotEqual(t, updatedPublicDashboard.IsEnabled, pdNotUpdatedRetrieved.IsEnabled) }) }