diff --git a/devenv/dev-dashboards/panel-library/panel-library.json b/devenv/dev-dashboards/panel-library/panel-library.json index caa684a6b39..e73259bda52 100644 --- a/devenv/dev-dashboards/panel-library/panel-library.json +++ b/devenv/dev-dashboards/panel-library/panel-library.json @@ -142,7 +142,8 @@ }, "id": 3, "libraryPanel": { - "uid": "MAnX2ifMk" + "uid": "MAnX2ifMk", + "name": "React Table" } }, { @@ -154,7 +155,8 @@ }, "id": 2, "libraryPanel": { - "uid": "g1sNpCaMz" + "uid": "g1sNpCaMz", + "name": "React Gauge" } } ], diff --git a/pkg/services/librarypanels/database.go b/pkg/services/librarypanels/database.go index b140b91e63c..b53a2b59c08 100644 --- a/pkg/services/librarypanels/database.go +++ b/pkg/services/librarypanels/database.go @@ -113,13 +113,20 @@ func (lps *LibraryPanelService) connectLibraryPanelsForDashboard(c *models.ReqCo // deleteLibraryPanel deletes a Library Panel. 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 { - 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 { 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 { return err } else if rowsAffected != 1 { diff --git a/pkg/services/librarypanels/librarypanels.go b/pkg/services/librarypanels/librarypanels.go index 171a5e51872..de6340b7099 100644 --- a/pkg/services/librarypanels/librarypanels.go +++ b/pkg/services/librarypanels/librarypanels.go @@ -71,7 +71,16 @@ func (lps *LibraryPanelService) LoadLibraryPanelsForDashboard(dash *models.Dashb libraryPanelInDB, ok := libraryPanels[uid] 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 diff --git a/pkg/services/librarypanels/librarypanels_test.go b/pkg/services/librarypanels/librarypanels_test.go index bd9eebc9442..4b398ea096e 100644 --- a/pkg/services/librarypanels/librarypanels_test.go +++ b/pkg/services/librarypanels/librarypanels_test.go @@ -650,7 +650,7 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) { 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) { command := getCreateCommand(1, "Text - Library Panel1") response := sc.service.createHandler(sc.reqContext, command) @@ -692,7 +692,38 @@ func TestLoadLibraryPanelsForDashboard(t *testing.T) { } 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) + } }) }