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

44 lines
1.7 KiB
Go

package schemaversion
import "context"
// V40 normalizes the dashboard refresh property to ensure consistent string typing.
//
// This migration addresses type inconsistencies in dashboard refresh configuration that could
// cause runtime errors or unexpected behavior. Over time, the refresh property has accumulated
// various data types (boolean, numeric, null, undefined) due to different dashboard creation
// methods, API usage patterns, and legacy imports.
//
// The migration works by:
// 1. Checking if the refresh property exists and is already a string type
// 2. Converting any non-string values (boolean true/false, numbers, null) to an empty string
// 3. Ensuring all dashboards have a consistent string-typed refresh property
//
// This normalization is critical because:
// - The frontend refresh logic expects string values for parsing time intervals
// - Non-string values can cause dashboard loading failures
// - Empty string is the standard representation for "no auto-refresh"
// - Consistent typing enables proper validation and UI behavior
//
// Example transformations:
//
// Before migration:
//
// refresh: true // boolean
// refresh: 30 // number (seconds)
// refresh: null // null value
// refresh: undefined // missing property
//
// After migration:
//
// refresh: "" // normalized to empty string
// refresh: "" // normalized to empty string
// refresh: "" // normalized to empty string
// refresh: "" // property added with empty string
func V40(_ context.Context, dash map[string]interface{}) error {
dash["schemaVersion"] = int(40)
// Ensure refresh is a string, set to empty string if missing or not a string
dash["refresh"] = GetStringValue(dash, "refresh")
return nil
}