Files
grafana/apps/dashboard/pkg/migration/schemaversion/v42.go
Ivan Ortega Alba a72e02f88a Fix dashboard migration discrepancies between backend and frontend implementations (use toEqual) (#110268)
**Highlights**

* **Single-version migrations**: add `targetVersion` to migrator & model, separate outputs, enforce exact version.
* **Datasource fixes**: include `apiVersion` in tests, empty-string → `{}`, preserve `{}` refs, drop unwanted defaults.
* **Panel defaults & nesting**: only top-level panels get defaults; preserve empty `transformations` context-aware; filter repeated panels.

* **Migration parity**

  * V16: collapsed rows, grid height parsing (`px`).
  * V17: omit `maxPerRow` when `minSpan=1`.
  * V19–V20: cleanup defaults (`targetBlank`, style).
  * V23–V24: template vars + table panel consistency.
  * V28: full singlestat/stat parity, mappings & color.
  * V30–V36: threshold logic, empty refs, nested targets.
* **Save-model cleanup**: replicate frontend defaults/filtering, drop null IDs, metadata, unused props.
* **Testing**: unified suites, dev dashboards (v42), full unit coverage for major migrations.

Co-authored-by: Ivan Ortega [ivanortegaalba@gmail.com](mailto:ivanortegaalba@gmail.com)
Co-authored-by: Dominik Prokop [dominik.prokop@grafana.com](mailto:dominik.prokop@grafana.com)
2025-09-24 12:20:25 +02:00

103 lines
2.8 KiB
Go

package schemaversion
import "context"
// V42 ensures that when a field is hidden from visualization, it is also hidden from tooltips.
//
// This migration addresses the inconsistency where fields could be hidden from visualizations
// (hideFrom.viz = true) but would still appear in tooltips. To prevent user confusion and ensure
// consistent behavior, this migration automatically sets hideFrom.tooltip = true for any field
// configuration override that has hideFrom.viz = true.
//
// The migration specifically targets field configuration overrides, including the special
// __systemRef override, and updates the hideFrom object to include tooltip: true whenever
// viz: true is found.
//
// Example transformation:
//
// Before migration:
//
// fieldConfig: {
// overrides: [{
// properties: [{
// id: "custom.hideFrom",
// value: { viz: true }
// }]
// }]
// }
//
// After migration:
//
// fieldConfig: {
// overrides: [{
// properties: [{
// id: "custom.hideFrom",
// value: { viz: true, tooltip: true }
// }]
// }]
// }
func V42(_ context.Context, dash map[string]interface{}) error {
dash["schemaVersion"] = int(42)
// Get panels from dashboard
panels, ok := dash["panels"].([]interface{})
if !ok {
return nil
}
// Process each panel
for _, panelInterface := range panels {
panel, ok := panelInterface.(map[string]interface{})
if !ok {
continue
}
migrateHideFromForPanel(panel)
}
return nil
}
// migrateHideFromForPanel processes a single panel and its nested panels
func migrateHideFromForPanel(panel map[string]interface{}) {
// Process the panel's field config
if fieldConfig, ok := panel["fieldConfig"].(map[string]interface{}); ok {
if overrides, ok := fieldConfig["overrides"].([]interface{}); ok {
for _, overrideInterface := range overrides {
override, ok := overrideInterface.(map[string]interface{})
if !ok {
continue
}
if properties, ok := override["properties"].([]interface{}); ok {
for _, propertyInterface := range properties {
property, ok := propertyInterface.(map[string]interface{})
if !ok {
continue
}
// Check if this is a custom.hideFrom property
if id := GetStringValue(property, "id"); id == "custom.hideFrom" {
if value, ok := property["value"].(map[string]interface{}); ok {
// If viz is true, also set tooltip to true
if GetBoolValue(value, "viz") {
value["tooltip"] = true
}
}
}
}
}
}
}
}
// Process nested panels (for rows)
if nestedPanels, ok := panel["panels"].([]interface{}); ok {
for _, nestedPanelInterface := range nestedPanels {
if nestedPanel, ok := nestedPanelInterface.(map[string]interface{}); ok {
migrateHideFromForPanel(nestedPanel)
}
}
}
}