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

152 lines
3.4 KiB
Go

package schemaversion
import (
"context"
)
// V23 migrates multi variables to ensure their current property is aligned with their multi property.
// This migration ensures that variables with multi=true have current.value and current.text as arrays,
// and variables with multi=false have current.value and current.text as single values.
//
// Example before migration:
//
// "templating": {
// "list": [
// { "type": "query", "multi": true, "current": { "value": "A", "text": "A" } },
// { "type": "query", "multi": false, "current": { "value": ["B"], "text": ["B"] } }
// ]
// }
//
// Example after migration:
//
// "templating": {
// "list": [
// { "type": "query", "multi": true, "current": { "value": ["A"], "text": ["A"] } },
// { "type": "query", "multi": false, "current": { "value": "B", "text": "B" } }
// ]
// }
func V23(_ context.Context, dashboard map[string]interface{}) error {
dashboard["schemaVersion"] = 23
templating, ok := dashboard["templating"].(map[string]interface{})
if !ok {
return nil
}
list, ok := templating["list"].([]interface{})
if !ok {
return nil
}
for _, v := range list {
variable, ok := v.(map[string]interface{})
if !ok {
continue
}
if !isMulti(variable) {
continue
}
current, ok := variable["current"].(map[string]interface{})
if !ok {
continue
}
if isEmptyObject(current) {
continue
}
multi, ok := variable["multi"].(bool)
if !ok {
continue
}
variable["current"] = alignCurrentWithMulti(current, multi)
}
return nil
}
// isMulti checks if a variable supports multi-selection
func isMulti(variable map[string]interface{}) bool {
_, hasMulti := variable["multi"]
return hasMulti
}
func isEmptyObject(value interface{}) bool {
if value == nil {
return true
}
obj, ok := value.(map[string]interface{})
if !ok {
return false
}
return len(obj) == 0
}
// alignCurrentWithMulti aligns the current property with the multi property
// This matches the frontend's alignCurrentWithMulti function behavior
func alignCurrentWithMulti(current map[string]interface{}, multi bool) map[string]interface{} {
if current == nil {
return current
}
result := make(map[string]interface{})
for k, v := range current {
result[k] = v
}
if multi {
convertToArrays(result)
} else {
convertToSingleValues(result)
}
return result
}
// convertToArrays converts single values to arrays (match frontend behavior)
// Frontend only converts when current.value is NOT an array
func convertToArrays(result map[string]interface{}) {
value, hasValue := result["value"]
if !hasValue || IsArray(value) {
return
}
// Convert value to array
result["value"] = []interface{}{value}
// Only convert text to array when we're converting value
if text, ok := result["text"]; ok && !IsArray(text) {
result["text"] = []interface{}{text}
}
}
// convertToSingleValues converts arrays to single values (both value and text must be single values)
func convertToSingleValues(result map[string]interface{}) {
convertArrayToSingle(result, "value")
convertArrayToSingle(result, "text")
}
// convertArrayToSingle converts an array field to a single value
func convertArrayToSingle(result map[string]interface{}, key string) {
value, ok := result[key]
if !ok || !IsArray(value) {
return
}
arr, ok := value.([]interface{})
if !ok {
result[key] = ""
return
}
if len(arr) > 0 {
result[key] = arr[0]
} else {
result[key] = ""
}
}