2025-06-27 01:04:43 +08:00
|
|
|
package schemaversion
|
|
|
|
|
|
|
|
// Shared utility functions for datasource migrations across different schema versions.
|
|
|
|
// These functions handle the common logic for migrating datasource references from
|
|
|
|
// string names/UIDs to structured reference objects with uid, type, and apiVersion.
|
|
|
|
|
|
|
|
// GetDataSourceRef creates a datasource reference object with uid, type and optional apiVersion
|
|
|
|
func GetDataSourceRef(ds *DataSourceInfo) map[string]interface{} {
|
|
|
|
if ds == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ref := map[string]interface{}{
|
|
|
|
"uid": ds.UID,
|
|
|
|
"type": ds.Type,
|
|
|
|
}
|
|
|
|
if ds.APIVersion != "" {
|
|
|
|
ref["apiVersion"] = ds.APIVersion
|
|
|
|
}
|
|
|
|
return ref
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDefaultDSInstanceSettings returns the default datasource if one exists
|
|
|
|
func GetDefaultDSInstanceSettings(datasources []DataSourceInfo) *DataSourceInfo {
|
|
|
|
for _, ds := range datasources {
|
|
|
|
if ds.Default {
|
|
|
|
return &DataSourceInfo{
|
|
|
|
UID: ds.UID,
|
|
|
|
Type: ds.Type,
|
|
|
|
Name: ds.Name,
|
|
|
|
APIVersion: ds.APIVersion,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 18:20:25 +08:00
|
|
|
// 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 false
|
2025-06-27 01:04:43 +08:00
|
|
|
}
|
|
|
|
|
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 18:20:25 +08:00
|
|
|
hasUID := false
|
|
|
|
if uid, exists := dsRef["uid"]; exists {
|
|
|
|
if uidStr, ok := uid.(string); ok && uidStr != "" {
|
|
|
|
hasUID = true
|
2025-06-27 01:04:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 18:20:25 +08:00
|
|
|
hasType := false
|
|
|
|
if typ, exists := dsRef["type"]; exists {
|
|
|
|
if typStr, ok := typ.(string); ok && typStr != "" {
|
|
|
|
hasType = true
|
2025-06-27 01:04:43 +08:00
|
|
|
}
|
|
|
|
}
|
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 18:20:25 +08:00
|
|
|
|
|
|
|
return hasUID || hasType
|
2025-06-27 01:04:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// MigrateDatasourceNameToRef converts a datasource name/uid string to a reference object
|
|
|
|
// Matches the frontend migrateDatasourceNameToRef function in DashboardMigrator.ts
|
|
|
|
// Options:
|
|
|
|
// - returnDefaultAsNull: if true, returns nil for "default" datasources (used in V33)
|
|
|
|
// - returnDefaultAsNull: if false, returns reference for "default" datasources (used in V36)
|
|
|
|
func MigrateDatasourceNameToRef(nameOrRef interface{}, options map[string]bool, datasources []DataSourceInfo) map[string]interface{} {
|
|
|
|
if options["returnDefaultAsNull"] && (nameOrRef == nil || nameOrRef == "default") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 18:20:25 +08:00
|
|
|
// Frontend: if (isDataSourceRef(nameOrRef)) { return nameOrRef; }
|
|
|
|
if isDataSourceRef(nameOrRef) {
|
|
|
|
return nameOrRef.(map[string]interface{})
|
2025-06-27 01:04:43 +08:00
|
|
|
}
|
|
|
|
|
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 18:20:25 +08:00
|
|
|
// Look up datasource by name/UID
|
|
|
|
if nameOrRef == nil || nameOrRef == "default" {
|
|
|
|
ds := GetDefaultDSInstanceSettings(datasources)
|
|
|
|
if ds != nil {
|
|
|
|
return GetDataSourceRef(ds)
|
|
|
|
}
|
2025-06-27 01:04:43 +08:00
|
|
|
}
|
|
|
|
|
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 18:20:25 +08:00
|
|
|
// 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)
|
2025-07-03 18:23:51 +08:00
|
|
|
return map[string]interface{}{}
|
|
|
|
}
|
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 18:20:25 +08:00
|
|
|
|
|
|
|
// 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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-07-03 18:23:51 +08:00
|
|
|
// Unknown datasource name should be preserved as UID-only reference
|
2025-06-27 01:04:43 +08:00
|
|
|
return map[string]interface{}{
|
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 18:20:25 +08:00
|
|
|
"uid": str,
|
2025-06-27 01:04:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|