From 1f1ed6db53fd5d7424e951e1fc1675b2553c0f62 Mon Sep 17 00:00:00 2001 From: shreyas-goenka <88374338+shreyas-goenka@users.noreply.github.com> Date: Thu, 30 Nov 2023 15:28:51 +0100 Subject: [PATCH] Add versioning for bundle templates (#972) ## Changes This PR adds versioning for bundle templates. Right now there's only logic for the maximum version of templates supported. At some point in the future if we make a breaking template change we can also include a minimum version of template supported by the CLI. ## Tests Unit tests. --- libs/jsonschema/extension.go | 4 ++++ libs/template/config.go | 6 ++++++ libs/template/config_test.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/libs/jsonschema/extension.go b/libs/jsonschema/extension.go index ffb77bd8..9127a0d6 100644 --- a/libs/jsonschema/extension.go +++ b/libs/jsonschema/extension.go @@ -26,4 +26,8 @@ type Extension struct { // If the CLI version is less than this value, then validation for this // schema will fail. MinDatabricksCliVersion string `json:"min_databricks_cli_version,omitempty"` + + // Version of the schema. This is used to determine if the schema is + // compatible with the current CLI version. + Version *int `json:"version,omitempty"` } diff --git a/libs/template/config.go b/libs/template/config.go index 51283e03..508e7736 100644 --- a/libs/template/config.go +++ b/libs/template/config.go @@ -9,6 +9,9 @@ import ( "golang.org/x/exp/maps" ) +// The latest template schema version supported by the CLI +const latestSchemaVersion = 1 + type config struct { ctx context.Context values map[string]any @@ -49,6 +52,9 @@ func validateSchema(schema *jsonschema.Schema) error { return fmt.Errorf("property type %s is not supported by bundle templates", v.Type) } } + if schema.Version != nil && *schema.Version > latestSchemaVersion { + return fmt.Errorf("template schema version %d is not supported by this version of the CLI. Please upgrade your CLI to the latest version", *schema.Version) + } return nil } diff --git a/libs/template/config_test.go b/libs/template/config_test.go index 69e7054f..d76952dc 100644 --- a/libs/template/config_test.go +++ b/libs/template/config_test.go @@ -2,6 +2,7 @@ package template import ( "context" + "fmt" "testing" "github.com/databricks/cli/cmd/root" @@ -150,6 +151,40 @@ func TestTemplateValidateSchema(t *testing.T) { assert.EqualError(t, err, "property type array is not supported by bundle templates") } +func TestTemplateValidateSchemaVersion(t *testing.T) { + version := latestSchemaVersion + schema := jsonschema.Schema{ + Extension: jsonschema.Extension{ + Version: &version, + }, + } + assert.NoError(t, validateSchema(&schema)) + + version = latestSchemaVersion + 1 + schema = jsonschema.Schema{ + Extension: jsonschema.Extension{ + Version: &version, + }, + } + assert.EqualError(t, validateSchema(&schema), fmt.Sprintf("template schema version %d is not supported by this version of the CLI. Please upgrade your CLI to the latest version", version)) + + version = 5000 + schema = jsonschema.Schema{ + Extension: jsonschema.Extension{ + Version: &version, + }, + } + assert.EqualError(t, validateSchema(&schema), "template schema version 5000 is not supported by this version of the CLI. Please upgrade your CLI to the latest version") + + version = 0 + schema = jsonschema.Schema{ + Extension: jsonschema.Extension{ + Version: &version, + }, + } + assert.NoError(t, validateSchema(&schema)) +} + func TestTemplateEnumValidation(t *testing.T) { schema := jsonschema.Schema{ Properties: map[string]*jsonschema.Schema{