Dashboard V0->V1 Migration: Schema migration v36 (#100757)

---------

Co-authored-by: Todd Treece <360020+toddtreece@users.noreply.github.com>
Co-authored-by: Haris Rozajac <haris.rozajac12@gmail.com>
Co-authored-by: Stephanie Hingtgen <stephanie.hingtgen@grafana.com>
This commit is contained in:
Ivan Ortega Alba
2025-06-16 15:53:41 +02:00
committed by GitHub
co-authored by Todd Treece Haris Rozajac Stephanie Hingtgen
parent 6767adb4a6
commit 59c2f15433
17 changed files with 2977 additions and 15 deletions
+44 -5
View File
@@ -1,11 +1,50 @@
package migration
import "github.com/grafana/grafana/apps/dashboard/pkg/migration/schemaversion"
import (
"sync"
func Migrate(dash map[string]any, targetVersion int) error {
if dash == nil {
dash = map[string]any{}
"github.com/grafana/grafana/apps/dashboard/pkg/migration/schemaversion"
)
// Initialize provides the migrator singleton with required dependencies and builds the map of migrations.
func Initialize(dsInfoProvider schemaversion.DataSourceInfoProvider) {
migratorInstance.init(dsInfoProvider)
}
// Migrate migrates the given dashboard to the target version.
// This will block until the migrator is initialized.
func Migrate(dash map[string]interface{}, targetVersion int) error {
return migratorInstance.migrate(dash, targetVersion)
}
var (
migratorInstance = &migrator{
migrations: map[int]schemaversion.SchemaVersionMigrationFunc{},
ready: make(chan struct{}),
}
initOnce sync.Once
)
type migrator struct {
ready chan struct{}
migrations map[int]schemaversion.SchemaVersionMigrationFunc
}
func (m *migrator) init(dsInfoProvider schemaversion.DataSourceInfoProvider) {
initOnce.Do(func() {
m.migrations = schemaversion.GetMigrations(dsInfoProvider)
close(m.ready)
})
}
func (m *migrator) migrate(dash map[string]interface{}, targetVersion int) error {
if dash == nil {
return schemaversion.NewMigrationError("dashboard is nil", 0, targetVersion)
}
// wait for the migrator to be initialized
<-m.ready
inputVersion := schemaversion.GetSchemaVersion(dash)
dash["schemaVersion"] = inputVersion
@@ -16,7 +55,7 @@ func Migrate(dash map[string]any, targetVersion int) error {
}
for nextVersion := inputVersion + 1; nextVersion <= targetVersion; nextVersion++ {
if migration, ok := schemaversion.Migrations[nextVersion]; ok {
if migration, ok := m.migrations[nextVersion]; ok {
if err := migration(dash); err != nil {
return schemaversion.NewMigrationError("migration failed", inputVersion, nextVersion)
}