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

169 lines
4.0 KiB
Go

package schemaversion
import "context"
// V27 migrates repeated panels and constant variables.
//
// The migration performs two main tasks:
// 1. Removes repeated panel leftovers by filtering out panels with repeatPanelId or repeatByRow
// 2. Migrates constant variables to textbox variables with proper current/options structure
//
// The migration includes comprehensive logic from the frontend:
// - Panel filtering to remove repeated panels
// - Constant variable migration with proper current/options structure
// - Support for both constant and textbox variable types
//
// Example before migration:
//
// "panels": [
// {
// "id": 1,
// "type": "graph",
// "repeatPanelId": "panel1"
// },
// {
// "id": 2,
// "type": "row",
// "panels": [
// {
// "id": 3,
// "type": "graph",
// "repeatPanelId": "panel2"
// }
// ]
// }
// ],
// "templating": {
// "list": [
// {
// "name": "constant_var",
// "type": "constant",
// "query": "default_value",
// "hide": 0
// }
// ]
// }
//
// Example after migration:
//
// "panels": [
// {
// "id": 2,
// "type": "row",
// "panels": []
// }
// ],
// "templating": {
// "list": [
// {
// "name": "constant_var",
// "type": "textbox",
// "query": "default_value",
// "current": {
// "selected": true,
// "text": "default_value",
// "value": "default_value"
// },
// "options": [
// {
// "selected": true,
// "text": "default_value",
// "value": "default_value"
// }
// ],
// "hide": 0
// }
// ]
// }
func V27(_ context.Context, dashboard map[string]interface{}) error {
dashboard["schemaVersion"] = 27
// Remove repeated panels
if panels, ok := dashboard["panels"].([]interface{}); ok {
dashboard["panels"] = removeRepeatedPanels(panels)
}
// Migrate constant variables
if templating, ok := dashboard["templating"].(map[string]interface{}); ok {
if list, ok := templating["list"].([]interface{}); ok {
for _, v := range list {
if variable, ok := v.(map[string]interface{}); ok {
migrateConstantVariable(variable)
}
}
}
}
return nil
}
// removeRepeatedPanels filters out panels with repeatPanelId or repeatByRow properties
// and cleans up repeated panels in collapsed rows
func removeRepeatedPanels(panels []interface{}) []interface{} {
newPanels := []interface{}{}
for _, panel := range panels {
p, ok := panel.(map[string]interface{})
if !ok {
continue
}
// Skip panels with repeatPanelId or repeatByRow
if _, hasRepeatPanelId := p["repeatPanelId"]; hasRepeatPanelId {
continue
}
if _, hasRepeatByRow := p["repeatByRow"]; hasRepeatByRow {
continue
}
// Filter out repeats in collapsed rows
if p["type"] == "row" {
if nestedPanels, ok := p["panels"].([]interface{}); ok {
filteredNestedPanels := []interface{}{}
for _, nestedPanel := range nestedPanels {
if np, ok := nestedPanel.(map[string]interface{}); ok {
if _, hasRepeatPanelId := np["repeatPanelId"]; !hasRepeatPanelId {
filteredNestedPanels = append(filteredNestedPanels, nestedPanel)
}
}
}
p["panels"] = filteredNestedPanels
}
}
newPanels = append(newPanels, panel)
}
return newPanels
}
// migrateConstantVariable converts constant variables to textbox variables with proper current/options structure
func migrateConstantVariable(variable map[string]interface{}) {
if variableType, ok := variable["type"].(string); !ok || variableType != "constant" {
return
}
query := ""
if queryVal, ok := variable["query"].(string); ok {
query = queryVal
}
variable["query"] = query
current := map[string]interface{}{
"selected": true,
"text": query,
"value": query,
}
options := []interface{}{current}
variable["current"] = current
variable["options"] = options
// Convert to textbox if hide is 0 (dontHide) or 1 (hideLabel)
hide := GetIntValue(variable, "hide", -1)
if hide == 0 || hide == 1 {
variable["type"] = "textbox"
}
}