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)
This commit is contained in:
Ivan Ortega Alba
2025-09-24 12:20:25 +02:00
committed by GitHub
parent 98fd3e8fe9
commit a72e02f88a
298 changed files with 158321 additions and 25235 deletions
@@ -123,122 +123,6 @@ func TestGetDefaultDSInstanceSettings(t *testing.T) {
}
}
func TestGetInstanceSettings(t *testing.T) {
datasources := []schemaversion.DataSourceInfo{
{UID: "default-ds-uid", Type: "prometheus", Name: "Default Test Datasource Name", Default: true, APIVersion: "v1"},
{UID: "existing-target-uid", Type: "elasticsearch", Name: "Existing Target Name", Default: false, APIVersion: "v2"},
{UID: "existing-ref-uid", Type: "prometheus", Name: "Existing Ref Name", Default: false, APIVersion: "v1"},
}
tests := []struct {
name string
nameOrRef interface{}
expected *schemaversion.DataSourceInfo
}{
{
name: "nil should return default",
nameOrRef: nil,
expected: &schemaversion.DataSourceInfo{
UID: "default-ds-uid",
Type: "prometheus",
Name: "Default Test Datasource Name",
APIVersion: "v1",
},
},
{
name: "default string should return default",
nameOrRef: "default",
expected: &schemaversion.DataSourceInfo{
UID: "default-ds-uid",
Type: "prometheus",
Name: "Default Test Datasource Name",
APIVersion: "v1",
},
},
{
name: "lookup by UID",
nameOrRef: "existing-target-uid",
expected: &schemaversion.DataSourceInfo{
UID: "existing-target-uid",
Type: "elasticsearch",
Name: "Existing Target Name",
APIVersion: "v2",
},
},
{
name: "lookup by name",
nameOrRef: "Existing Target Name",
expected: &schemaversion.DataSourceInfo{
UID: "existing-target-uid",
Type: "elasticsearch",
Name: "Existing Target Name",
APIVersion: "v2",
},
},
{
name: "lookup by UID without apiVersion",
nameOrRef: "existing-ref-uid",
expected: &schemaversion.DataSourceInfo{
UID: "existing-ref-uid",
Type: "prometheus",
Name: "Existing Ref Name",
APIVersion: "v1",
},
},
{
name: "lookup by reference object with UID",
nameOrRef: map[string]interface{}{
"uid": "existing-target-uid",
},
expected: &schemaversion.DataSourceInfo{
UID: "existing-target-uid",
Type: "elasticsearch",
Name: "Existing Target Name",
APIVersion: "v2",
},
},
{
name: "lookup by reference object without UID",
nameOrRef: map[string]interface{}{
"type": "prometheus",
},
expected: &schemaversion.DataSourceInfo{
UID: "default-ds-uid",
Type: "prometheus",
Name: "Default Test Datasource Name",
APIVersion: "v1",
},
},
{
name: "unknown datasource should return nil",
nameOrRef: "unknown-ds",
expected: nil,
},
{
name: "empty string should return nil",
nameOrRef: "",
expected: nil,
},
{
name: "unsupported input type should return default",
nameOrRef: 123,
expected: &schemaversion.DataSourceInfo{
UID: "default-ds-uid",
Type: "prometheus",
Name: "Default Test Datasource Name",
APIVersion: "v1",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := schemaversion.GetInstanceSettings(tt.nameOrRef, datasources)
assert.Equal(t, tt.expected, result)
})
}
}
func TestMigrateDatasourceNameToRef(t *testing.T) {
datasources := []schemaversion.DataSourceInfo{
{UID: "default-ds-uid", Type: "prometheus", Name: "Default Test Datasource Name", Default: true, APIVersion: "v1"},
@@ -386,26 +270,20 @@ func TestMigrateDatasourceNameToRef(t *testing.T) {
t.Run("edge cases", func(t *testing.T) {
options := map[string]bool{"returnDefaultAsNull": false}
t.Run("reference without uid should lookup default", func(t *testing.T) {
t.Run("reference without uid should be preserved as-is", func(t *testing.T) {
nameOrRef := map[string]interface{}{
"type": "prometheus",
}
result := schemaversion.MigrateDatasourceNameToRef(nameOrRef, options, datasources)
expected := map[string]interface{}{
"uid": "default-ds-uid",
"type": "prometheus",
"apiVersion": "v1",
"type": "prometheus",
}
assert.Equal(t, expected, result)
})
t.Run("integer input should return default reference", func(t *testing.T) {
t.Run("integer input should return nil", func(t *testing.T) {
result := schemaversion.MigrateDatasourceNameToRef(123, options, datasources)
expected := map[string]interface{}{
"uid": "default-ds-uid",
"type": "prometheus",
"apiVersion": "v1",
}
expected := map[string]interface{}(nil)
assert.Equal(t, expected, result)
})