Dashboard Migrations: v27 repeated panels and constant variable (#108644)

This commit is contained in:
Haris Rozajac
2025-08-06 10:22:11 -06:00
committed by GitHub
parent d0d8b8f82d
commit fdfe123305
8 changed files with 917 additions and 1 deletions
@@ -5,7 +5,7 @@ import (
)
const (
MIN_VERSION = 27
MIN_VERSION = 26
LATEST_VERSION = 41
)
@@ -38,6 +38,7 @@ type PanelPluginInfoProvider interface {
func GetMigrations(dsInfoProvider DataSourceInfoProvider, panelProvider PanelPluginInfoProvider) map[int]SchemaVersionMigrationFunc {
return map[int]SchemaVersionMigrationFunc{
27: V27,
28: V28(panelProvider),
29: V29,
30: V30,
@@ -0,0 +1,167 @@
package schemaversion
// 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(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 {
var 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)
if hide, ok := variable["hide"].(float64); ok {
if hide == 0 || hide == 1 {
variable["type"] = "textbox"
}
}
}
@@ -0,0 +1,172 @@
package schemaversion_test
import (
"testing"
"github.com/grafana/grafana/apps/dashboard/pkg/migration/schemaversion"
)
func TestV27(t *testing.T) {
tests := []migrationTestCase{
{
name: "remove repeated panels with repeatPanelId and repeatByRow",
input: map[string]interface{}{
"schemaVersion": 26,
"panels": []interface{}{
map[string]interface{}{
"id": 1,
"type": "graph",
"title": "Repeated Panel 1",
"repeatPanelId": "panel1",
},
map[string]interface{}{
"id": 2,
"type": "graph",
"title": "Repeated Panel 2",
"repeatByRow": true,
},
map[string]interface{}{
"id": 3,
"type": "graph",
"title": "Normal Panel",
},
},
},
expected: map[string]interface{}{
"schemaVersion": 27,
"panels": []interface{}{
map[string]interface{}{
"id": 3,
"type": "graph",
"title": "Normal Panel",
},
},
},
},
{
name: "filter repeated panels in row panels",
input: map[string]interface{}{
"schemaVersion": 26,
"panels": []interface{}{
map[string]interface{}{
"id": 1,
"type": "row",
"title": "Row with repeated panels",
"panels": []interface{}{
map[string]interface{}{
"id": 2,
"type": "graph",
"title": "Repeated nested panel",
"repeatPanelId": "nested_panel1",
},
map[string]interface{}{
"id": 3,
"type": "graph",
"title": "Normal nested panel",
},
},
},
map[string]interface{}{
"id": 4,
"type": "graph",
"title": "Normal panel outside row",
},
},
},
expected: map[string]interface{}{
"schemaVersion": 27,
"panels": []interface{}{
map[string]interface{}{
"id": 1,
"type": "row",
"title": "Row with repeated panels",
"panels": []interface{}{
map[string]interface{}{
"id": 3,
"type": "graph",
"title": "Normal nested panel",
},
},
},
map[string]interface{}{
"id": 4,
"type": "graph",
"title": "Normal panel outside row",
},
},
},
},
{
name: "migrate constant variable to textbox with hide=0",
input: map[string]interface{}{
"schemaVersion": 26,
"templating": map[string]interface{}{
"list": []interface{}{
map[string]interface{}{
"name": "constant_var",
"type": "constant",
"query": "default_value",
"hide": 0.0,
},
},
},
},
expected: map[string]interface{}{
"schemaVersion": 27,
"templating": map[string]interface{}{
"list": []interface{}{
map[string]interface{}{
"name": "constant_var",
"type": "textbox",
"query": "default_value",
"hide": 0.0,
"current": map[string]interface{}{
"selected": true,
"text": "default_value",
"value": "default_value",
},
"options": []interface{}{
map[string]interface{}{
"selected": true,
"text": "default_value",
"value": "default_value",
},
},
},
},
},
},
},
{
name: "do not migrate non-constant variable",
input: map[string]interface{}{
"schemaVersion": 26,
"templating": map[string]interface{}{
"list": []interface{}{
map[string]interface{}{
"name": "query_var",
"type": "query",
"query": "some_query",
"hide": 0.0,
},
},
},
},
expected: map[string]interface{}{
"schemaVersion": 27,
"templating": map[string]interface{}{
"list": []interface{}{
map[string]interface{}{
"name": "query_var",
"type": "query",
"query": "some_query",
"hide": 0.0,
},
},
},
},
},
}
runMigrationTests(t, tests, schemaversion.V27)
}