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:
@@ -34,51 +34,29 @@ func GetDefaultDSInstanceSettings(datasources []DataSourceInfo) *DataSourceInfo
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetInstanceSettings looks up a datasource by name or uid reference
|
||||
func GetInstanceSettings(nameOrRef interface{}, datasources []DataSourceInfo) *DataSourceInfo {
|
||||
if nameOrRef == nil || nameOrRef == "default" {
|
||||
return GetDefaultDSInstanceSettings(datasources)
|
||||
}
|
||||
|
||||
// Check if it's a reference object
|
||||
if ref, ok := nameOrRef.(map[string]interface{}); ok {
|
||||
if _, hasUID := ref["uid"]; !hasUID {
|
||||
// Reference object without UID should return default
|
||||
return GetDefaultDSInstanceSettings(datasources)
|
||||
}
|
||||
// It's a reference object with UID, search for matching UID
|
||||
for _, ds := range datasources {
|
||||
if uid, hasUID := ref["uid"]; hasUID && uid == ds.UID {
|
||||
return &DataSourceInfo{
|
||||
UID: ds.UID,
|
||||
Type: ds.Type,
|
||||
Name: ds.Name,
|
||||
APIVersion: ds.APIVersion,
|
||||
}
|
||||
}
|
||||
}
|
||||
// Unknown UID-only reference should return nil (preserve it)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check if it's a string
|
||||
str, ok := nameOrRef.(string)
|
||||
// isDataSourceRef checks if the object is a valid DataSourceRef (has uid or type)
|
||||
// Matches the frontend isDataSourceRef function in datasource.ts
|
||||
func isDataSourceRef(ref interface{}) bool {
|
||||
dsRef, ok := ref.(map[string]interface{})
|
||||
if !ok {
|
||||
return GetDefaultDSInstanceSettings(datasources)
|
||||
return false
|
||||
}
|
||||
|
||||
// Search for matching name or UID
|
||||
for _, ds := range datasources {
|
||||
if str == ds.Name || str == ds.UID {
|
||||
return &DataSourceInfo{
|
||||
UID: ds.UID,
|
||||
Type: ds.Type,
|
||||
Name: ds.Name,
|
||||
APIVersion: ds.APIVersion,
|
||||
}
|
||||
hasUID := false
|
||||
if uid, exists := dsRef["uid"]; exists {
|
||||
if uidStr, ok := uid.(string); ok && uidStr != "" {
|
||||
hasUID = true
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
hasType := false
|
||||
if typ, exists := dsRef["type"]; exists {
|
||||
if typStr, ok := typ.(string); ok && typStr != "" {
|
||||
hasType = true
|
||||
}
|
||||
}
|
||||
|
||||
return hasUID || hasType
|
||||
}
|
||||
|
||||
// MigrateDatasourceNameToRef converts a datasource name/uid string to a reference object
|
||||
@@ -91,26 +69,42 @@ func MigrateDatasourceNameToRef(nameOrRef interface{}, options map[string]bool,
|
||||
return nil
|
||||
}
|
||||
|
||||
if dsRef, ok := nameOrRef.(map[string]interface{}); ok {
|
||||
if _, hasUID := dsRef["uid"]; hasUID {
|
||||
return dsRef
|
||||
// Frontend: if (isDataSourceRef(nameOrRef)) { return nameOrRef; }
|
||||
if isDataSourceRef(nameOrRef) {
|
||||
return nameOrRef.(map[string]interface{})
|
||||
}
|
||||
|
||||
// Look up datasource by name/UID
|
||||
if nameOrRef == nil || nameOrRef == "default" {
|
||||
ds := GetDefaultDSInstanceSettings(datasources)
|
||||
if ds != nil {
|
||||
return GetDataSourceRef(ds)
|
||||
}
|
||||
}
|
||||
|
||||
ds := GetInstanceSettings(nameOrRef, datasources)
|
||||
if ds != nil {
|
||||
return GetDataSourceRef(ds)
|
||||
}
|
||||
|
||||
// Handle string cases (including empty strings)
|
||||
if dsName, ok := nameOrRef.(string); ok {
|
||||
if dsName == "" {
|
||||
// Empty string should return empty object (frontend behavior)
|
||||
// Check if it's a string name/UID
|
||||
if str, ok := nameOrRef.(string); ok {
|
||||
// Handle empty string case
|
||||
if str == "" {
|
||||
// Empty string should return {} (frontend behavior)
|
||||
return map[string]interface{}{}
|
||||
}
|
||||
|
||||
// Search for matching datasource
|
||||
for _, ds := range datasources {
|
||||
if str == ds.Name || str == ds.UID {
|
||||
return GetDataSourceRef(&DataSourceInfo{
|
||||
UID: ds.UID,
|
||||
Type: ds.Type,
|
||||
Name: ds.Name,
|
||||
APIVersion: ds.APIVersion,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Unknown datasource name should be preserved as UID-only reference
|
||||
return map[string]interface{}{
|
||||
"uid": dsName,
|
||||
"uid": str,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user