Commit 0b1f5c5e by Hugo Häggmark Committed by GitHub

PanelLibrary: better handling of deleted panels (#30709)

parent 2028f89c
...@@ -142,7 +142,8 @@ ...@@ -142,7 +142,8 @@
}, },
"id": 3, "id": 3,
"libraryPanel": { "libraryPanel": {
"uid": "MAnX2ifMk" "uid": "MAnX2ifMk",
"name": "React Table"
} }
}, },
{ {
...@@ -154,7 +155,8 @@ ...@@ -154,7 +155,8 @@
}, },
"id": 2, "id": 2,
"libraryPanel": { "libraryPanel": {
"uid": "g1sNpCaMz" "uid": "g1sNpCaMz",
"name": "React Gauge"
} }
} }
], ],
......
...@@ -113,13 +113,20 @@ func (lps *LibraryPanelService) connectLibraryPanelsForDashboard(c *models.ReqCo ...@@ -113,13 +113,20 @@ func (lps *LibraryPanelService) connectLibraryPanelsForDashboard(c *models.ReqCo
// deleteLibraryPanel deletes a Library Panel. // deleteLibraryPanel deletes a Library Panel.
func (lps *LibraryPanelService) deleteLibraryPanel(c *models.ReqContext, uid string) error { func (lps *LibraryPanelService) deleteLibraryPanel(c *models.ReqContext, uid string) error {
orgID := c.SignedInUser.OrgId
return lps.SQLStore.WithTransactionalDbSession(context.Background(), func(session *sqlstore.DBSession) error { return lps.SQLStore.WithTransactionalDbSession(context.Background(), func(session *sqlstore.DBSession) error {
result, err := session.Exec("DELETE FROM library_panel WHERE uid=? and org_id=?", uid, orgID) panel, err := getLibraryPanel(session, uid, c.SignedInUser.OrgId)
if err != nil { if err != nil {
return err return err
} }
if _, err := session.Exec("DELETE FROM library_panel_dashboard WHERE librarypanel_id=?", panel.ID); err != nil {
return err
}
result, err := session.Exec("DELETE FROM library_panel WHERE id=?", panel.ID)
if err != nil {
return err
}
if rowsAffected, err := result.RowsAffected(); err != nil { if rowsAffected, err := result.RowsAffected(); err != nil {
return err return err
} else if rowsAffected != 1 { } else if rowsAffected != 1 {
......
...@@ -71,7 +71,16 @@ func (lps *LibraryPanelService) LoadLibraryPanelsForDashboard(dash *models.Dashb ...@@ -71,7 +71,16 @@ func (lps *LibraryPanelService) LoadLibraryPanelsForDashboard(dash *models.Dashb
libraryPanelInDB, ok := libraryPanels[uid] libraryPanelInDB, ok := libraryPanels[uid]
if !ok { if !ok {
return fmt.Errorf("found connection to library panel %q that isn't in database", uid) name := libraryPanel.Get("name").MustString()
elem := dash.Data.Get("panels").GetIndex(i)
elem.Set("gridPos", panelAsJSON.Get("gridPos").MustMap())
elem.Set("id", panelAsJSON.Get("id").MustInt64())
elem.Set("type", fmt.Sprintf("Name: \"%s\", UID: \"%s\"", name, uid))
elem.Set("libraryPanel", map[string]interface{}{
"uid": uid,
"name": name,
})
continue
} }
// we have a match between what is stored in db and in dashboard json // we have a match between what is stored in db and in dashboard json
......
...@@ -650,7 +650,7 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) { ...@@ -650,7 +650,7 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) {
require.EqualError(t, err, errLibraryPanelHeaderUIDMissing.Error()) require.EqualError(t, err, errLibraryPanelHeaderUIDMissing.Error())
}) })
testScenario(t, "When an admin tries to load a dashboard with a library panel that is not connected, it should fail", testScenario(t, "When an admin tries to load a dashboard with a library panel that is not connected, it should set correct JSON and continue",
func(t *testing.T, sc scenarioContext) { func(t *testing.T, sc scenarioContext) {
command := getCreateCommand(1, "Text - Library Panel1") command := getCreateCommand(1, "Text - Library Panel1")
response := sc.service.createHandler(sc.reqContext, command) response := sc.service.createHandler(sc.reqContext, command)
...@@ -692,7 +692,38 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) { ...@@ -692,7 +692,38 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) {
} }
err = sc.service.LoadLibraryPanelsForDashboard(&dash) err = sc.service.LoadLibraryPanelsForDashboard(&dash)
require.EqualError(t, err, fmt.Errorf("found connection to library panel %q that isn't in database", existing.Result.UID).Error()) require.NoError(t, err)
expectedJSON := map[string]interface{}{
"panels": []interface{}{
map[string]interface{}{
"id": int64(1),
"gridPos": map[string]interface{}{
"h": 6,
"w": 6,
"x": 0,
"y": 0,
},
},
map[string]interface{}{
"id": int64(2),
"gridPos": map[string]interface{}{
"h": 6,
"w": 6,
"x": 6,
"y": 0,
},
"libraryPanel": map[string]interface{}{
"uid": existing.Result.UID,
"name": existing.Result.Name,
},
"type": fmt.Sprintf("Name: \"%s\", UID: \"%s\"", existing.Result.Name, existing.Result.UID),
},
},
}
expected := simplejson.NewFromAny(expectedJSON)
if diff := cmp.Diff(expected.Interface(), dash.Data.Interface(), getCompareOptions()...); diff != "" {
t.Fatalf("Result mismatch (-want +got):\n%s", diff)
}
}) })
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment