Skip to content

Commit 0afbaa3

Browse files
papagianevictorero
andauthored
Dashboards: Fix restoring dashboard to root folder (grafana#89020)
* Fix restoring dashboard to root folder * use a root folder representation instead of nil * change root folder by general folder --------- Co-authored-by: Ezequiel Victorero <[email protected]>
1 parent b4c5c62 commit 0afbaa3

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

pkg/services/dashboards/database/database.go

+4
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,10 @@ func (d *dashboardStore) GetSoftDeletedDashboard(ctx context.Context, orgID int6
536536

537537
func (d *dashboardStore) RestoreDashboard(ctx context.Context, orgID int64, dashboardUID string, folder *folder.Folder) error {
538538
return d.store.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
539+
if folder == nil || folder.UID == "" {
540+
_, err := sess.Exec("UPDATE dashboard SET deleted=NULL, folder_id=0, folder_uid=NULL WHERE org_id=? AND uid=?", orgID, dashboardUID)
541+
return err
542+
}
539543
// nolint:staticcheck
540544
_, err := sess.Exec("UPDATE dashboard SET deleted=NULL, folder_id = ?, folder_uid=? WHERE org_id=? AND uid=?", folder.ID, folder.UID, orgID, dashboardUID)
541545
return err

pkg/services/folder/folderimpl/folder.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,10 @@ func (s *Service) Get(ctx context.Context, q *folder.GetFolderQuery) (*folder.Fo
209209
var dashFolder *folder.Folder
210210
var err error
211211
switch {
212-
case q.UID != nil && *q.UID != "":
212+
case q.UID != nil:
213+
if *q.UID == "" {
214+
return &folder.GeneralFolder, nil
215+
}
213216
dashFolder, err = s.getFolderByUID(ctx, q.OrgID, *q.UID)
214217
if err != nil {
215218
return nil, err

pkg/services/folder/folderimpl/folder_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,21 @@ func TestIntegrationFolderService(t *testing.T) {
395395
"For error '%s' expected error '%s', actual '%s'", tc.ActualError, tc.ExpectedError, actualError)
396396
}
397397
})
398+
399+
t.Run("Returns root folder", func(t *testing.T) {
400+
t.Run("When the folder UID is blank should return the root folder", func(t *testing.T) {
401+
emptyString := ""
402+
actual, err := service.Get(context.Background(), &folder.GetFolderQuery{
403+
UID: &emptyString,
404+
OrgID: 1,
405+
SignedInUser: usr,
406+
})
407+
408+
assert.NoError(t, err)
409+
assert.Equal(t, folder.GeneralFolder.UID, actual.UID)
410+
assert.Equal(t, folder.GeneralFolder.Title, actual.Title)
411+
})
412+
})
398413
})
399414
}
400415

0 commit comments

Comments
 (0)