diff --git a/.nextchanges/bundles/name-prefix-underscores.md b/.nextchanges/bundles/name-prefix-underscores.md new file mode 100644 index 00000000000..497c65882fc --- /dev/null +++ b/.nextchanges/bundles/name-prefix-underscores.md @@ -0,0 +1 @@ +* Fix underscores in custom `presets.name_prefix` for schemas, registered models, and serving endpoints; affected schemas can rename in place, while registered models and serving endpoints can be recreated, so inspect `bundle plan` and references before deploying. ([#7287](https://github.com/databricks/cli/pull/7287)) diff --git a/acceptance/bundle/presets/name_prefix_existing_schema/databricks.yml b/acceptance/bundle/presets/name_prefix_existing_schema/databricks.yml new file mode 100644 index 00000000000..eeeca9fa637 --- /dev/null +++ b/acceptance/bundle/presets/name_prefix_existing_schema/databricks.yml @@ -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 diff --git a/acceptance/bundle/presets/name_prefix_existing_schema/out.test.toml b/acceptance/bundle/presets/name_prefix_existing_schema/out.test.toml new file mode 100644 index 00000000000..a927a5fbc06 --- /dev/null +++ b/acceptance/bundle/presets/name_prefix_existing_schema/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/presets/name_prefix_existing_schema/output.txt b/acceptance/bundle/presets/name_prefix_existing_schema/output.txt new file mode 100644 index 00000000000..ad13a9b8723 --- /dev/null +++ b/acceptance/bundle/presets/name_prefix_existing_schema/output.txt @@ -0,0 +1,36 @@ + +>>> [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 rename the existing schema + +>>> update_file.py databricks.yml name_prefix: my_user name_prefix: my_user_ + +>>> [CLI] bundle plan +update schemas.qc + +Plan: 0 to add, 1 to change, 0 to delete, 0 unchanged + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/name-prefix-existing-schema/existing/files... +Updated schemas.qc +Files: 1 uploaded, 0 deleted +Resources: 0 created, 1 changed, 0 deleted, 0 unchanged + +>>> [CLI] schemas get main.my_user_qc +{ + "full_name": "main.my_user_qc", + "schema_id": "[SCHEMA_ID]" +} + +>>> [CLI] bundle plan +Plan: 0 to add, 0 to change, 0 to delete, 1 unchanged diff --git a/acceptance/bundle/presets/name_prefix_existing_schema/script b/acceptance/bundle/presets/name_prefix_existing_schema/script new file mode 100644 index 00000000000..8b12904936e --- /dev/null +++ b/acceptance/bundle/presets/name_prefix_existing_schema/script @@ -0,0 +1,12 @@ +# 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 +add_repl "$($CLI schemas get main.my_userqc | jq -r .schema_id)" SCHEMA_ID +trace $CLI bundle plan + +title "A corrected prefix plans to rename the existing schema" +trace update_file.py databricks.yml "name_prefix: my_user" "name_prefix: my_user_" +trace $CLI bundle plan +trace $CLI bundle deploy +trace $CLI schemas get main.my_user_qc | jq '{full_name, schema_id}' +trace $CLI bundle plan diff --git a/acceptance/bundle/presets/name_prefix_underscores/databricks.yml b/acceptance/bundle/presets/name_prefix_underscores/databricks.yml new file mode 100644 index 00000000000..933a4077a69 --- /dev/null +++ b/acceptance/bundle/presets/name_prefix_underscores/databricks.yml @@ -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 diff --git a/acceptance/bundle/presets/name_prefix_underscores/out.test.toml b/acceptance/bundle/presets/name_prefix_underscores/out.test.toml new file mode 100644 index 00000000000..a927a5fbc06 --- /dev/null +++ b/acceptance/bundle/presets/name_prefix_underscores/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/presets/name_prefix_underscores/output.txt b/acceptance/bundle/presets/name_prefix_underscores/output.txt new file mode 100644 index 00000000000..6f96c626e97 --- /dev/null +++ b/acceptance/bundle/presets/name_prefix_underscores/output.txt @@ -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" +} diff --git a/acceptance/bundle/presets/name_prefix_underscores/script b/acceptance/bundle/presets/name_prefix_underscores/script new file mode 100644 index 00000000000..4d20aece393 --- /dev/null +++ b/acceptance/bundle/presets/name_prefix_underscores/script @@ -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}' diff --git a/bundle/config/mutator/resourcemutator/apply_presets.go b/bundle/config/mutator/resourcemutator/apply_presets.go index d808daf82fb..8e3dec6a6ed 100644 --- a/bundle/config/mutator/resourcemutator/apply_presets.go +++ b/bundle/config/mutator/resourcemutator/apply_presets.go @@ -389,18 +389,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 } diff --git a/bundle/config/mutator/resourcemutator/apply_presets_test.go b/bundle/config/mutator/resourcemutator/apply_presets_test.go index c97db137b26..3e01495914e 100644 --- a/bundle/config/mutator/resourcemutator/apply_presets_test.go +++ b/bundle/config/mutator/resourcemutator/apply_presets_test.go @@ -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" ) @@ -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) + }) + } +}