Files
grafana/apps/dashboard/pkg/migration/schemaversion/v29.go
T
Haris Rozajac 2f8ec01c6c Dashboard Migrations: V29 query variable refresh property and options (#108088)
* Dashboard Migrations: V31 LabelsToFields-Merge Migration

* Dashboard Migrations: V32 No-op migration

* simplify

* Refactor to reduce nesting

* Dashboard Migrations: V30 value mappings and tooltip options

* Do not automigrate since graph is migrated in v27

* Refactor to reduce nesting

* Add test case for invalid mapping

* migrate to v29

---------

Co-authored-by: Ivan Ortega <ivanortegaalba@gmail.com>
2025-07-15 17:27:04 +00:00

60 lines
1.5 KiB
Go

package schemaversion
// V29 migrates query variables to ensure their refresh property is set to 1 (on dashboard load)
// if it is not 1 or 2, and clears their options array if present.
//
// Example before migration:
//
// "templating": {
// "list": [
// { "type": "query", "refresh": 0, "options": [{ "text": "A", "value": "A" }] },
// { "type": "query", "refresh": 2, "options": [{ "text": "B", "value": "B" }] },
// { "type": "query", "options": [{ "text": "C", "value": "C" }] }
// ]
// }
//
// Example after migration:
//
// "templating": {
// "list": [
// { "type": "query", "refresh": 1, "options": [] },
// { "type": "query", "refresh": 2, "options": [] },
// { "type": "query", "refresh": 1, "options": [] }
// ]
// }
func V29(dashboard map[string]interface{}) error {
dashboard["schemaVersion"] = 29
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 variable["type"] != "query" {
continue
}
// Set refresh to 1 if not 1 or 2
refresh, hasRefresh := variable["refresh"]
refreshInt := 0
if r, ok := refresh.(int); ok {
refreshInt = r
}
if !hasRefresh || (refreshInt != 1 && refreshInt != 2) {
variable["refresh"] = 1
}
// Clear options if present
if _, hasOptions := variable["options"]; hasOptions {
variable["options"] = []interface{}{}
}
}
return nil
}