Skip to content

Commit

Permalink
Add versioning for bundle templates (#972)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
shreyas-goenka committed Nov 30, 2023
1 parent 677926b commit 1f1ed6d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions libs/jsonschema/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
6 changes: 6 additions & 0 deletions libs/template/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
35 changes: 35 additions & 0 deletions libs/template/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package template

import (
"context"
"fmt"
"testing"

"github.com/databricks/cli/cmd/root"
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 1f1ed6d

Please sign in to comment.