Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nextchanges/bundles/name-prefix-underscores.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Fix underscores in custom `presets.name_prefix` for schemas, registered models, and serving endpoints; existing deployments using such prefixes may recreate these resources, and schema recreation can delete data, so inspect `bundle plan` before deploying. ([#7287](https://github.com/databricks/cli/pull/7287))
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
bundle:
name: name-prefix-existing-schema

resources:
schemas:
qc:
name: qc
catalog_name: main

targets:
existing:
default: true
presets:
name_prefix: my_user
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cloud = false
EnvMatrix.DMS = ["", "true"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

>>> [CLI] bundle deploy
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/name-prefix-existing-schema/existing/files...
Created schemas.qc
Files: 1 uploaded, 0 deleted
Resources: 1 created, 0 changed, 0 deleted, 0 unchanged

>>> [CLI] bundle summary -o json
"main.my_userqc"

>>> [CLI] bundle plan
Plan: 0 to add, 0 to change, 0 to delete, 1 unchanged

=== A corrected prefix plans to recreate the existing schema
>>> update_file.py databricks.yml name_prefix: my_user name_prefix: my_user_

>>> [CLI] bundle plan
recreate schemas.qc

Plan: 1 to add, 0 to change, 1 to delete, 0 unchanged
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Older CLIs resolve my_user_ to my_userqc, the name created here.
trace $CLI bundle deploy
trace $CLI bundle summary -o json | jq .resources.schemas.qc.id
trace $CLI bundle plan

title "A corrected prefix plans to recreate the existing schema"
trace update_file.py databricks.yml "name_prefix: my_user" "name_prefix: my_user_"
trace $CLI bundle plan
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
bundle:
name: name_prefix_underscores

resources:
schemas:
qc:
name: qc
catalog_name: my_catalog
registered_models:
model:
name: model
model_serving_endpoints:
endpoint:
name: endpoint

targets:
trailing:
default: true
presets:
name_prefix: ${workspace.current_user.short_name}_
repeated:
presets:
name_prefix: team__${workspace.current_user.short_name}__
generated:
mode: development
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cloud = false
EnvMatrix.DMS = ["", "true"]
21 changes: 21 additions & 0 deletions acceptance/bundle/presets/name_prefix_underscores/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

>>> [CLI] bundle validate -o json -t trailing
{
"schema": "[USERNAME]_qc",
"model": "[USERNAME]_model",
"endpoint": "[USERNAME]_endpoint"
}

>>> [CLI] bundle validate -o json -t repeated
{
"schema": "team__[USERNAME]__qc",
"model": "team__[USERNAME]__model",
"endpoint": "team__[USERNAME]__endpoint"
}

>>> [CLI] bundle validate -o json -t generated
{
"schema": "dev_[USERNAME]_qc",
"model": "dev_[USERNAME]_model",
"endpoint": "dev_[USERNAME]_endpoint"
}
3 changes: 3 additions & 0 deletions acceptance/bundle/presets/name_prefix_underscores/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
trace $CLI bundle validate -o json -t trailing | jq '{schema: .resources.schemas.qc.name, model: .resources.registered_models.model.name, endpoint: .resources.model_serving_endpoints.endpoint.name}'
trace $CLI bundle validate -o json -t repeated | jq '{schema: .resources.schemas.qc.name, model: .resources.registered_models.model.name, endpoint: .resources.model_serving_endpoints.endpoint.name}'
trace $CLI bundle validate -o json -t generated | jq '{schema: .resources.schemas.qc.name, model: .resources.registered_models.model.name, endpoint: .resources.model_serving_endpoints.endpoint.name}'
20 changes: 11 additions & 9 deletions bundle/config/mutator/resourcemutator/apply_presets.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,18 +372,20 @@ func toTagArray(tags map[string]string) []Tag {
return tagArray
}

// normalizePrefix prefixes strings like '[dev lennart] ' to 'dev_lennart_'.
// We leave unicode letters and numbers but remove all "special characters."
// normalizePrefix normalizes special characters while preserving explicit underscores.
func normalizePrefix(prefix string) string {
prefix = strings.ReplaceAll(prefix, "[", "")
prefix = strings.Trim(prefix, " ")

// If the prefix ends with a ']', we add an underscore to the end.
// This makes sure that we get names like "dev_user_endpoint" instead of "dev_userendpoint"
suffix := ""
if strings.HasSuffix(prefix, "]") {
suffix = "_"
}
bracketed := strings.HasSuffix(prefix, "]")

return textutil.NormalizeString(prefix) + suffix
parts := strings.Split(prefix, "_")
for i := range parts {
parts[i] = textutil.NormalizeString(parts[i])
}
normalized := strings.Join(parts, "_")
if bracketed && !strings.HasSuffix(normalized, "_") {
normalized += "_"
}
return normalized
}
44 changes: 44 additions & 0 deletions bundle/config/mutator/resourcemutator/apply_presets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/bundle/config/resources"
"github.com/databricks/databricks-sdk-go/service/catalog"
"github.com/databricks/databricks-sdk-go/service/serving"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -38,3 +39,46 @@ func TestApplyPresetsSkipsNilQualityMonitors(t *testing.T) {
require.NoError(t, diags.Error())
assert.Nil(t, b.Config.Resources.QualityMonitors["monitor"].Schedule)
}

func TestApplyPresetsNormalizedPrefixPreservesUnderscores(t *testing.T) {
tests := []struct {
name string
prefix string
wantPrefix string
}{
{"trailing underscore", "my_user_", "my_user_"},
{"repeated underscores", "my__user__", "my__user__"},
{"bracketed underscores", "[qa__team] ", "qa__team_"},
{"bracketed trailing underscore", "[qa_] ", "qa_"},
{"bracketed repeated trailing underscores", "[qa__] ", "qa__"},
{"bracketed prefix", "[prod] ", "prod_"},
{"other separators", "a-b", "a_b"},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
b := &bundle.Bundle{
Config: config.Root{
Presets: config.Presets{NamePrefix: test.prefix},
Resources: config.Resources{
Schemas: map[string]*resources.Schema{
"qc": {CreateSchema: catalog.CreateSchema{CatalogName: "my_catalog", Name: "qc"}},
},
RegisteredModels: map[string]*resources.RegisteredModel{
"model": {CreateRegisteredModelRequest: catalog.CreateRegisteredModelRequest{Name: "model"}},
},
ModelServingEndpoints: map[string]*resources.ModelServingEndpoint{
"endpoint": {CreateServingEndpoint: serving.CreateServingEndpoint{Name: "endpoint"}},
},
},
},
}

diags := bundle.Apply(t.Context(), b, ApplyPresets())
require.NoError(t, diags.Error())
assert.Equal(t, test.wantPrefix+"qc", b.Config.Resources.Schemas["qc"].Name)
assert.Equal(t, test.wantPrefix+"model", b.Config.Resources.RegisteredModels["model"].Name)
assert.Equal(t, test.wantPrefix+"endpoint", b.Config.Resources.ModelServingEndpoints["endpoint"].Name)
})
}
}
Loading