Files
grafana/apps/dashboard/pkg/migration/migrate.go
T
Stephanie Hingtgen ed408985fa [release-12.0.1] K8s: Dashboards: Mark as v1beta1 (#104422)
Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
2025-04-23 19:21:59 +01:00

33 lines
1.0 KiB
Go

package migration
import "github.com/grafana/grafana/apps/dashboard/pkg/migration/schemaversion"
func Migrate(dash map[string]any, targetVersion int) error {
if dash == nil {
dash = map[string]any{}
}
inputVersion := schemaversion.GetSchemaVersion(dash)
dash["schemaVersion"] = inputVersion
// If the schema version is older than the minimum version, with migration support,
// we don't migrate the dashboard.
if inputVersion < schemaversion.MIN_VERSION {
return schemaversion.NewMinimumVersionError(inputVersion)
}
for nextVersion := inputVersion + 1; nextVersion <= targetVersion; nextVersion++ {
if migration, ok := schemaversion.Migrations[nextVersion]; ok {
if err := migration(dash); err != nil {
return schemaversion.NewMigrationError("migration failed", inputVersion, nextVersion)
}
dash["schemaVersion"] = nextVersion
}
}
if schemaversion.GetSchemaVersion(dash) != targetVersion {
return schemaversion.NewMigrationError("schema version not migrated to target version", inputVersion, targetVersion)
}
return nil
}