diff --git a/apps/dashboard/pkg/migration/migrate.go b/apps/dashboard/pkg/migration/migrate.go index 38def79b465..0f8992e6d2d 100644 --- a/apps/dashboard/pkg/migration/migrate.go +++ b/apps/dashboard/pkg/migration/migrate.go @@ -9,6 +9,12 @@ func Migrate(dash map[string]interface{}, targetVersion int) error { 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.NewMigrationError("schema version is too old", inputVersion, schemaversion.MIN_VERSION) + } + for nextVersion := inputVersion + 1; nextVersion <= targetVersion; nextVersion++ { if migration, ok := schemaversion.Migrations[nextVersion]; ok { if err := migration(dash); err != nil { diff --git a/apps/dashboard/pkg/migration/migrate_test.go b/apps/dashboard/pkg/migration/migrate_test.go index bc4080d43d3..ef2a3468ce0 100644 --- a/apps/dashboard/pkg/migration/migrate_test.go +++ b/apps/dashboard/pkg/migration/migrate_test.go @@ -22,6 +22,15 @@ func TestMigrate(t *testing.T) { files, err := os.ReadDir(INPUT_DIR) require.NoError(t, err) + t.Run("minimum version check", func(t *testing.T) { + err := migration.Migrate(map[string]interface{}{ + "schemaVersion": schemaversion.MIN_VERSION - 1, + }, schemaversion.MIN_VERSION) + + var minVersionErr = schemaversion.NewMigrationError("schema version is too old", schemaversion.MIN_VERSION-1, schemaversion.MIN_VERSION) + require.ErrorAs(t, err, &minVersionErr) + }) + for _, f := range files { if f.IsDir() { continue diff --git a/apps/dashboard/pkg/migration/schemaversion/errors.go b/apps/dashboard/pkg/migration/schemaversion/errors.go index f5bbbe7d1fa..ee98593219d 100644 --- a/apps/dashboard/pkg/migration/schemaversion/errors.go +++ b/apps/dashboard/pkg/migration/schemaversion/errors.go @@ -23,3 +23,17 @@ type MigrationError struct { func (e *MigrationError) Error() string { return fmt.Errorf("schema migration from version %d to %d failed: %v", e.currentVersion, e.targetVersion, e.msg).Error() } + +// MinimumVersionError is an error that is returned when the schema version is below the minimum version. +func NewMinimumVersionError(inputVersion int) *MinimumVersionError { + return &MinimumVersionError{inputVersion: inputVersion} +} + +// MinimumVersionError is an error type for minimum version errors. +type MinimumVersionError struct { + inputVersion int +} + +func (e *MinimumVersionError) Error() string { + return fmt.Errorf("input schema version is below minimum version. input: %d minimum: %d", e.inputVersion, MIN_VERSION).Error() +} diff --git a/apps/dashboard/pkg/migration/schemaversion/migrations.go b/apps/dashboard/pkg/migration/schemaversion/migrations.go index 3d83da73a3f..81bdd545dc6 100644 --- a/apps/dashboard/pkg/migration/schemaversion/migrations.go +++ b/apps/dashboard/pkg/migration/schemaversion/migrations.go @@ -4,7 +4,10 @@ import "strconv" type SchemaVersionMigrationFunc func(map[string]interface{}) error -const LATEST_VERSION = 41 +const ( + MIN_VERSION = 36 + LATEST_VERSION = 41 +) var Migrations = map[int]SchemaVersionMigrationFunc{ 37: V37,