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

88 lines
2.6 KiB
Go

package schemaversion
import "context"
// V35 ensures x-axis visibility in timeseries panels to prevent dashboard breakage.
//
// This migration addresses a specific issue where timeseries panels with all axes
// configured as hidden (axisPlacement: "hidden") would result in completely unusable
// visualizations, as users would lose the ability to see time progression along the x-axis.
//
// The migration works by:
// 1. Identifying timeseries panels where the default axis placement is set to "hidden"
// 2. Adding a field override that specifically targets time-type fields (x-axis)
// 3. Setting the axis placement for time fields to "auto" to ensure x-axis visibility
//
// This preserves the original intent of hiding other axes while maintaining the critical
// time axis that makes timeseries data comprehensible. The override uses field matching
// by type ("time") to selectively restore visibility only for temporal data.
//
// Example transformation:
//
// Before migration:
//
// fieldConfig: {
// defaults: { custom: { axisPlacement: "hidden" } },
// overrides: []
// }
//
// After migration:
//
// fieldConfig: {
// defaults: { custom: { axisPlacement: "hidden" } },
// overrides: [{
// matcher: { id: "byType", options: "time" },
// properties: [{ id: "custom.axisPlacement", value: "auto" }]
// }]
// }
func V35(_ context.Context, dashboard map[string]interface{}) error {
dashboard["schemaVersion"] = int(35)
panels, ok := dashboard["panels"].([]interface{})
if !ok {
return nil
}
for _, panel := range panels {
p, ok := panel.(map[string]interface{})
if !ok || p["type"] != "timeseries" {
continue
}
applyXAxisVisibilityOverride(p)
}
return nil
}
// applyXAxisVisibilityOverride adds a field override to ensure x-axis visibility
// when the panel's default axis placement is set to hidden.
func applyXAxisVisibilityOverride(panel map[string]interface{}) {
fieldConfig, ok := panel["fieldConfig"].(map[string]interface{})
if !ok {
// Only process panels that already have fieldConfig (matches frontend behavior)
return
}
defaults, _ := fieldConfig["defaults"].(map[string]interface{})
custom, _ := defaults["custom"].(map[string]interface{})
if custom["axisPlacement"] != "hidden" {
return
}
overrides, _ := fieldConfig["overrides"].([]interface{})
fieldConfig["overrides"] = append(overrides, map[string]interface{}{
"matcher": map[string]interface{}{
"id": "byType",
"options": "time",
},
"properties": []interface{}{
map[string]interface{}{
"id": "custom.axisPlacement",
"value": "auto",
},
},
})
}