Schema: convert dashboards from v1beta1 to v2beta1 (#109037)

- Implement full conversion pipeline from v1beta1 → v2beta1
- Ensure frontend–backend parity for all dashboard serialization paths
- Add automatic data loss detection for conversions (panels, queries, annotations, links, variables)
- Extract atomic conversion functions for v0 → v1beta1 → v2alpha1 → v2beta1
- Introduce conversion metrics and detailed logging for loss tracking
- Normalize datasource resolution, defaults, and annotation processing
- Improve panel layout serialization and y-coordinate normalization
- Fix inconsistencies in nested panels and collapsed row behavior
- Refine variable handling:
  - Filter refId from variable query specs
  - Default variable refresh to 'never' (matches frontend)
  - Fix constant and interval variable handling for missing queries
- Unify schema defaults (enable, hide, iconColor, editable, liveNow)
- Fix pluginId usage (UID vs type) and datasource references
- Fix metrics.go bug swallowing errors (return nil → return err)
- Add tests for version-specific conversion error handling
- Add data loss detection tests using source/target version comparison
- Clean up lint issues, legacy code, and redundant files
- Update OpenAPI snapshots and migrated dashboards
- Improve backend migrator to reuse datasource provider and match frontend logic

Co-authored-by: Haris Rozajac <haris.rozajac12@gmail.com>
Co-authored-by: Oscar Kilhed <oscar.kilhed@grafana.com>
Co-authored-by: Stephanie Hingtgen <stephanie.hingtgen@grafana.com>
This commit is contained in:
Ivan Ortega Alba
2025-11-12 11:43:46 +01:00
committed by GitHub
co-authored by Haris Rozajac Oscar Kilhed Stephanie Hingtgen
parent 09942c08db
commit e463781077
273 changed files with 75003 additions and 588 deletions
@@ -0,0 +1,127 @@
package conversion
import (
"testing"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
dashv1 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v1beta1"
dashv2alpha1 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v2alpha1"
dashv2beta1 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v2beta1"
"github.com/grafana/grafana/apps/dashboard/pkg/migration"
migrationtestutil "github.com/grafana/grafana/apps/dashboard/pkg/migration/testutil"
common "github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1"
)
// TestV1ConversionErrorHandling tests that v1 conversion functions properly handle errors
func TestV1ConversionErrorHandling(t *testing.T) {
// Initialize the migrator with a test data source provider
dsProvider := migrationtestutil.NewDataSourceProvider(migrationtestutil.StandardTestConfig)
migration.Initialize(dsProvider)
t.Run("Convert_V1beta1_to_V2alpha1 sets status on conversion error", func(t *testing.T) {
// Create a dashboard that will cause conversion to fail
// We can use an invalid dashboard structure
source := &dashv1.Dashboard{
ObjectMeta: metav1.ObjectMeta{
Namespace: "org-1",
},
Spec: common.Unstructured{
Object: map[string]interface{}{
"title": "test dashboard",
// Missing required fields that might cause conversion to fail
},
},
}
target := &dashv2alpha1.Dashboard{}
err := Convert_V1beta1_to_V2alpha1(source, target, nil, dsProvider)
// Convert_V1beta1_to_V2alpha1 doesn't return error, just sets status
require.NoError(t, err, "Convert_V1beta1_to_V2alpha1 doesn't return error")
// Layout should always be set
require.NotNil(t, target.Spec.Layout.GridLayoutKind)
// If conversion failed, status should be set
if target.Status.Conversion != nil {
require.True(t, target.Status.Conversion.Failed)
require.NotNil(t, target.Status.Conversion.Error)
}
})
t.Run("Convert_V1beta1_to_V2beta1 returns error on first step failure", func(t *testing.T) {
source := &dashv1.Dashboard{
ObjectMeta: metav1.ObjectMeta{
Namespace: "org-1",
},
Spec: common.Unstructured{
Object: map[string]interface{}{
"title": "test dashboard",
// Missing required fields that might cause conversion to fail
},
},
}
target := &dashv2beta1.Dashboard{}
err := Convert_V1beta1_to_V2beta1(source, target, nil, dsProvider)
// May or may not error depending on dashboard content
// But if it does error on first step, status should be set with correct StoredVersion
if err != nil {
require.NotNil(t, target.Status.Conversion)
require.True(t, target.Status.Conversion.Failed)
require.NotNil(t, target.Status.Conversion.Error)
require.Equal(t, dashv1.VERSION, *target.Status.Conversion.StoredVersion)
}
})
t.Run("Convert_V1beta1_to_V2beta1 returns error on second step failure", func(t *testing.T) {
// Create a dashboard that will pass first step but might fail second step
source := &dashv1.Dashboard{
ObjectMeta: metav1.ObjectMeta{
Namespace: "org-1",
},
Spec: common.Unstructured{
Object: map[string]interface{}{
"title": "test dashboard",
"schemaVersion": 25, // Valid schema version
},
},
}
target := &dashv2beta1.Dashboard{}
err := Convert_V1beta1_to_V2beta1(source, target, nil, dsProvider)
// May or may not error depending on dashboard content
// But if it does error on second step, status should be set with correct StoredVersion
if err != nil {
require.NotNil(t, target.Status.Conversion)
require.True(t, target.Status.Conversion.Failed)
require.NotNil(t, target.Status.Conversion.Error)
require.Equal(t, dashv1.VERSION, *target.Status.Conversion.StoredVersion)
}
})
t.Run("Convert_V1beta1_to_V2beta1 success path returns nil", func(t *testing.T) {
source := &dashv1.Dashboard{
ObjectMeta: metav1.ObjectMeta{
Namespace: "org-1",
},
Spec: common.Unstructured{
Object: map[string]interface{}{
"title": "test dashboard",
"schemaVersion": 25, // Valid schema version
},
},
}
target := &dashv2beta1.Dashboard{}
err := Convert_V1beta1_to_V2beta1(source, target, nil, dsProvider)
// Should succeed if dashboard is valid
if err == nil {
// Success path covered
require.NoError(t, err)
}
})
}