Skip to content

Commit fae16fd

Browse files
bundle: add env var to enable experimental record_deployment_history
Add DATABRICKS_BUNDLE_RECORD_DEPLOYMENT_HISTORY as an environment-variable way to opt into the experimental record_deployment_history setting, so the feature can be toggled per invocation without editing the bundle config. A new mutator in the initialize phase sets experimental.record_deployment_history when the env var is present. It never disables a setting the configuration already enabled. Co-authored-by: Isaac
1 parent 00b36e9 commit fae16fd

8 files changed

Lines changed: 147 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bundle:
2+
name: test-bundle

acceptance/bundle/experimental/record_deployment_history/out.test.toml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
=== record_deployment_history is unset by default
3+
>>> [CLI] bundle validate -o json
4+
null
5+
6+
=== DATABRICKS_BUNDLE_RECORD_DEPLOYMENT_HISTORY enables record_deployment_history
7+
>>> [CLI] bundle validate -o json
8+
true
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
title "record_deployment_history is unset by default"
2+
3+
trace $CLI bundle validate -o json | jq .experimental
4+
5+
title "DATABRICKS_BUNDLE_RECORD_DEPLOYMENT_HISTORY enables record_deployment_history"
6+
7+
export DATABRICKS_BUNDLE_RECORD_DEPLOYMENT_HISTORY=true
8+
9+
trace $CLI bundle validate -o json | jq .experimental.record_deployment_history
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package mutator
2+
3+
import (
4+
"context"
5+
6+
"github.com/databricks/cli/bundle"
7+
"github.com/databricks/cli/bundle/config"
8+
"github.com/databricks/cli/bundle/env"
9+
"github.com/databricks/cli/libs/diag"
10+
)
11+
12+
type applyRecordDeploymentHistoryEnv struct{}
13+
14+
// ApplyRecordDeploymentHistoryEnv enables the experimental record_deployment_history
15+
// setting when the DATABRICKS_BUNDLE_RECORD_DEPLOYMENT_HISTORY environment variable is
16+
// set. This lets the feature be toggled per invocation without editing the bundle
17+
// configuration; it never disables a setting that the configuration already enabled.
18+
func ApplyRecordDeploymentHistoryEnv() bundle.Mutator {
19+
return &applyRecordDeploymentHistoryEnv{}
20+
}
21+
22+
func (m *applyRecordDeploymentHistoryEnv) Name() string {
23+
return "ApplyRecordDeploymentHistoryEnv"
24+
}
25+
26+
func (m *applyRecordDeploymentHistoryEnv) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
27+
if _, ok := env.RecordDeploymentHistory(ctx); !ok {
28+
return nil
29+
}
30+
31+
if b.Config.Experimental == nil {
32+
b.Config.Experimental = &config.Experimental{}
33+
}
34+
b.Config.Experimental.RecordDeploymentHistory = true
35+
return nil
36+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package mutator_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/databricks/cli/bundle"
7+
"github.com/databricks/cli/bundle/config"
8+
"github.com/databricks/cli/bundle/config/mutator"
9+
"github.com/databricks/cli/libs/env"
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestApplyRecordDeploymentHistoryEnv(t *testing.T) {
15+
tests := []struct {
16+
name string
17+
envValue string
18+
envSet bool
19+
experimental *config.Experimental
20+
want bool
21+
}{
22+
{
23+
name: "env not set leaves config untouched",
24+
envSet: false,
25+
want: false,
26+
},
27+
{
28+
name: "env set enables the experimental setting",
29+
envValue: "true",
30+
envSet: true,
31+
want: true,
32+
},
33+
{
34+
name: "env set enables it even when experimental is already present",
35+
envValue: "true",
36+
envSet: true,
37+
experimental: &config.Experimental{PythonWheelWrapper: true},
38+
want: true,
39+
},
40+
}
41+
42+
for _, tc := range tests {
43+
t.Run(tc.name, func(t *testing.T) {
44+
ctx := t.Context()
45+
if tc.envSet {
46+
ctx = env.Set(ctx, "DATABRICKS_BUNDLE_RECORD_DEPLOYMENT_HISTORY", tc.envValue)
47+
}
48+
49+
b := &bundle.Bundle{
50+
Config: config.Root{
51+
Experimental: tc.experimental,
52+
},
53+
}
54+
55+
diags := bundle.Apply(ctx, b, mutator.ApplyRecordDeploymentHistoryEnv())
56+
require.NoError(t, diags.Error())
57+
58+
if tc.want {
59+
require.NotNil(t, b.Config.Experimental)
60+
assert.True(t, b.Config.Experimental.RecordDeploymentHistory)
61+
} else {
62+
// When the env var is unset the mutator must not allocate Experimental.
63+
assert.Nil(t, b.Config.Experimental)
64+
}
65+
})
66+
}
67+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package env
2+
3+
import "context"
4+
5+
// recordDeploymentHistoryVariable names the environment variable that opts the
6+
// bundle into recording deployment history. It is the environment-variable
7+
// equivalent of setting experimental.record_deployment_history in the bundle
8+
// configuration.
9+
const recordDeploymentHistoryVariable = "DATABRICKS_BUNDLE_RECORD_DEPLOYMENT_HISTORY"
10+
11+
// RecordDeploymentHistory returns the environment variable that opts the bundle
12+
// into recording deployment history.
13+
func RecordDeploymentHistory(ctx context.Context) (string, bool) {
14+
return get(ctx, []string{
15+
recordDeploymentHistoryVariable,
16+
})
17+
}

bundle/phases/initialize.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ func Initialize(ctx context.Context, b *bundle.Bundle) {
3434
validate.ValidateEngine(),
3535
validate.Scripts(),
3636

37+
// Updates (typed): b.Config.Experimental.RecordDeploymentHistory
38+
// Enables the experimental record_deployment_history setting from the
39+
// DATABRICKS_BUNDLE_RECORD_DEPLOYMENT_HISTORY environment variable.
40+
mutator.ApplyRecordDeploymentHistoryEnv(),
41+
3742
// Updates (dynamic): sync.{paths,include,exclude} (makes them relative to bundle root rather than to definition file)
3843
// Rewrites sync paths to be relative to the bundle root instead of the file they were defined in.
3944
mutator.RewriteSyncPaths(),

0 commit comments

Comments
 (0)