diff --git a/apps/dashboard/pkg/migration/schemaversion/migrations.go b/apps/dashboard/pkg/migration/schemaversion/migrations.go index 1b34855a5a8..cccd27dbc21 100644 --- a/apps/dashboard/pkg/migration/schemaversion/migrations.go +++ b/apps/dashboard/pkg/migration/schemaversion/migrations.go @@ -5,7 +5,7 @@ import ( ) const ( - MIN_VERSION = 22 + MIN_VERSION = 21 LATEST_VERSION = 41 ) @@ -38,6 +38,7 @@ type PanelPluginInfoProvider interface { func GetMigrations(dsInfoProvider DataSourceInfoProvider, panelProvider PanelPluginInfoProvider) map[int]SchemaVersionMigrationFunc { return map[int]SchemaVersionMigrationFunc{ + 22: V22, 23: V23, 24: V24(panelProvider), 25: V25, diff --git a/apps/dashboard/pkg/migration/schemaversion/v22.go b/apps/dashboard/pkg/migration/schemaversion/v22.go new file mode 100644 index 00000000000..e6e2c2f5175 --- /dev/null +++ b/apps/dashboard/pkg/migration/schemaversion/v22.go @@ -0,0 +1,66 @@ +package schemaversion + +// V22 migrates table panel styles to set align property to 'auto'. +// This migration ensures that all table panel styles have their align property +// set to 'auto' for consistent alignment behavior. +// +// Example before migration: +// +// "panels": [ +// { +// "type": "table", +// "styles": [ +// { "type": "number", "pattern": "Time", "align": "left" }, +// { "type": "string", "pattern": "Value", "align": "right" } +// ] +// } +// ] +// +// Example after migration: +// +// "panels": [ +// { +// "type": "table", +// "styles": [ +// { "type": "number", "pattern": "Time", "align": "auto" }, +// { "type": "string", "pattern": "Value", "align": "auto" } +// ] +// } +// ] +func V22(dashboard map[string]interface{}) error { + dashboard["schemaVersion"] = 22 + + panels, ok := dashboard["panels"].([]interface{}) + if !ok { + return nil + } + + for _, p := range panels { + panel, ok := p.(map[string]interface{}) + if !ok { + continue + } + + // Only process table panels + panelType, ok := panel["type"].(string) + if !ok || panelType != "table" { + continue + } + + styles, ok := panel["styles"].([]interface{}) + if !ok { + continue + } + + // Update each style to set align to 'auto' + for _, s := range styles { + style, ok := s.(map[string]interface{}) + if !ok { + continue + } + style["align"] = "auto" + } + } + + return nil +} diff --git a/apps/dashboard/pkg/migration/schemaversion/v22_test.go b/apps/dashboard/pkg/migration/schemaversion/v22_test.go new file mode 100644 index 00000000000..6da11aadad1 --- /dev/null +++ b/apps/dashboard/pkg/migration/schemaversion/v22_test.go @@ -0,0 +1,123 @@ +package schemaversion_test + +import ( + "testing" + + "github.com/grafana/grafana/apps/dashboard/pkg/migration/schemaversion" +) + +func TestV22(t *testing.T) { + tests := []migrationTestCase{ + { + name: "table panel styles align is set to auto", + input: map[string]interface{}{ + "title": "V22 Table Panel Styles Test", + "schemaVersion": 21, + "panels": []interface{}{ + map[string]interface{}{ + "id": 1, + "type": "table", + "styles": []interface{}{ + map[string]interface{}{ + "type": "number", + "pattern": "Time", + "align": "left", + }, + map[string]interface{}{ + "type": "string", + "pattern": "Value", + "align": "right", + }, + }, + }, + }, + }, + expected: map[string]interface{}{ + "title": "V22 Table Panel Styles Test", + "schemaVersion": 22, + "panels": []interface{}{ + map[string]interface{}{ + "id": 1, + "type": "table", + "styles": []interface{}{ + map[string]interface{}{ + "type": "number", + "pattern": "Time", + "align": "auto", + }, + map[string]interface{}{ + "type": "string", + "pattern": "Value", + "align": "auto", + }, + }, + }, + }, + }, + }, + { + name: "non-table panel is unchanged except schemaVersion", + input: map[string]interface{}{ + "title": "V22 Non-Table Panel Test", + "schemaVersion": 21, + "panels": []interface{}{ + map[string]interface{}{ + "id": 2, + "type": "graph", + "styles": []interface{}{ + map[string]interface{}{ + "type": "number", + "pattern": "Time", + "align": "left", + }, + }, + }, + }, + }, + expected: map[string]interface{}{ + "title": "V22 Non-Table Panel Test", + "schemaVersion": 22, + "panels": []interface{}{ + map[string]interface{}{ + "id": 2, + "type": "graph", + "styles": []interface{}{ + map[string]interface{}{ + "type": "number", + "pattern": "Time", + "align": "left", + }, + }, + }, + }, + }, + }, + { + name: "table panel with no styles is unchanged except schemaVersion", + input: map[string]interface{}{ + "title": "V22 Table Panel No Styles Test", + "schemaVersion": 21, + "panels": []interface{}{ + map[string]interface{}{ + "id": 3, + "type": "table", + "styles": []interface{}{}, + }, + }, + }, + expected: map[string]interface{}{ + "title": "V22 Table Panel No Styles Test", + "schemaVersion": 22, + "panels": []interface{}{ + map[string]interface{}{ + "id": 3, + "type": "table", + "styles": []interface{}{}, + }, + }, + }, + }, + } + + runMigrationTests(t, tests, schemaversion.V22) +} diff --git a/apps/dashboard/pkg/migration/testdata/input/v22.table_panel_align.json b/apps/dashboard/pkg/migration/testdata/input/v22.table_panel_align.json new file mode 100644 index 00000000000..c376f546ddf --- /dev/null +++ b/apps/dashboard/pkg/migration/testdata/input/v22.table_panel_align.json @@ -0,0 +1,14 @@ +{ + "title": "V22 Table Panel Styles Test", + "schemaVersion": 21, + "panels": [ + { + "id": 1, + "type": "table", + "styles": [ + { "type": "number", "pattern": "Time", "align": "left" }, + { "type": "string", "pattern": "Value", "align": "right" } + ] + } + ] +} \ No newline at end of file diff --git a/apps/dashboard/pkg/migration/testdata/output/v22.table_panel_align.json b/apps/dashboard/pkg/migration/testdata/output/v22.table_panel_align.json new file mode 100644 index 00000000000..cc0ffedf895 --- /dev/null +++ b/apps/dashboard/pkg/migration/testdata/output/v22.table_panel_align.json @@ -0,0 +1,78 @@ +{ + "panels": [ + { + "datasource": { + "apiVersion": "v1", + "type": "prometheus", + "uid": "default-ds-uid" + }, + "fieldConfig": { + "defaults": { + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "inspect": false + }, + "mappings": [] + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "Time" + }, + "properties": [ + { + "id": "custom.align", + "value": "" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Value" + }, + "properties": [ + { + "id": "custom.align", + "value": "" + } + ] + } + ] + }, + "id": 1, + "options": { + "cellHeight": "sm", + "footer": { + "countRows": false, + "fields": "", + "reducer": [ + "sum" + ], + "show": false + }, + "showHeader": true + }, + "pluginVersion": "1.0.0", + "targets": [ + { + "datasource": { + "apiVersion": "v1", + "type": "prometheus", + "uid": "default-ds-uid" + }, + "refId": "A" + } + ], + "transformations": [], + "type": "table" + } + ], + "refresh": "", + "schemaVersion": 41, + "title": "V22 Table Panel Styles Test" +} \ No newline at end of file