Files
grafana/apps/dashboard/pkg/migration/schemaversion/v37.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

131 lines
3.0 KiB
Go

package schemaversion
import "context"
// V37 normalizes legend configuration to use `showLegend` property consistently.
//
// This migration addresses inconsistencies in how legend visibility was handled.
// There were two ways to hide the legend:
// 1. Using displayMode: "hidden"
// 2. Using showLegend: false
//
// The migration normalizes both approaches to use showLegend consistently:
// - If displayMode is "hidden" OR showLegend is false, set displayMode to "list" and showLegend to false
// - For all other existing legend objects, ensure showLegend is true
//
// Note: This migration only processes legend configurations that already exist as objects.
// Boolean legend values are not processed by this migration.
//
// Example transformations:
//
// Before migration (hidden displayMode):
//
// options: {
// legend: {
// displayMode: "hidden",
// placement: "bottom"
// }
// }
//
// After migration:
//
// options: {
// legend: {
// displayMode: "list",
// showLegend: false,
// placement: "bottom"
// }
// }
//
// Before migration (showLegend false):
//
// options: {
// legend: {
// displayMode: "table",
// showLegend: false
// }
// }
//
// After migration:
//
// options: {
// legend: {
// displayMode: "list",
// showLegend: false
// }
// }
//
// Before migration (visible legend):
//
// options: {
// legend: {
// displayMode: "table",
// placement: "bottom"
// }
// }
//
// After migration:
//
// options: {
// legend: {
// displayMode: "table",
// placement: "bottom",
// showLegend: true
// }
// }
func V37(_ context.Context, dashboard map[string]interface{}) error {
dashboard["schemaVersion"] = int(37)
panels, ok := dashboard["panels"].([]interface{})
if !ok {
return nil
}
// Process all panels, including nested ones
processPanelsV37(panels)
return nil
}
// processPanelsV37 recursively processes panels, including nested panels within rows
func processPanelsV37(panels []interface{}) {
for _, panel := range panels {
p, ok := panel.(map[string]interface{})
if !ok {
continue
}
// Process nested panels if this is a row panel
if p["type"] == "row" {
if nestedPanels, ok := p["panels"].([]interface{}); ok {
processPanelsV37(nestedPanels)
}
continue
}
options, ok := p["options"].(map[string]interface{})
if !ok {
continue
}
// Only process legend if it exists and is an object (not boolean)
legendValue := options["legend"]
legend, ok := legendValue.(map[string]interface{})
if !ok || legend == nil {
continue
}
displayMode := GetStringValue(legend, "displayMode")
showLegend, hasShowLegend := legend["showLegend"].(bool)
// If displayMode is "hidden" OR showLegend is explicitly false, normalize to hidden legend
if displayMode == "hidden" || (hasShowLegend && !showLegend) {
legend["displayMode"] = "list"
legend["showLegend"] = false
} else {
// For all other cases, ensure showLegend is true
legend["showLegend"] = true
}
}
}