Skip prefixes for schema names when catalog is already namespaced to current user

This commit is contained in:
Shreyas Goenka 2024-10-14 16:43:58 +02:00
parent f0e2981596
commit be2d802d13
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
3 changed files with 56 additions and 1 deletions

View File

@ -11,6 +11,7 @@ import (
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/cli/libs/dyn"
"github.com/databricks/cli/libs/log"
"github.com/databricks/cli/libs/textutil"
"github.com/databricks/databricks-sdk-go/service/catalog"
"github.com/databricks/databricks-sdk-go/service/jobs"
@ -188,6 +189,14 @@ func (m *applyPresets) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnos
diags = diags.Extend(diag.Errorf("schema %s is not defined", key))
continue
}
// If the catalog is already namespaced to the current user, we don't need
// to prefix the schema name since it already falls under the user's namespace.
if containsUserIdentity(s.CatalogName, b.Config.Workspace.CurrentUser) {
log.Debugf(ctx, "Skipping schema %s since catalog %s already contains the user's identity", s.Name, s.CatalogName)
continue
}
s.Name = normalizePrefix(prefix) + s.Name
// HTTP API for schemas doesn't yet support tags. It's only supported in
// the Databricks UI and via the SQL API.

View File

@ -9,6 +9,7 @@ import (
"github.com/databricks/cli/bundle/config/mutator"
"github.com/databricks/cli/bundle/config/resources"
"github.com/databricks/databricks-sdk-go/service/catalog"
"github.com/databricks/databricks-sdk-go/service/iam"
"github.com/databricks/databricks-sdk-go/service/jobs"
"github.com/stretchr/testify/require"
)
@ -96,12 +97,53 @@ func TestApplyPresetsPrefixForUcSchema(t *testing.T) {
},
want: "schema1",
},
{
name: "skip prefix because catalog contains short name",
prefix: "[prefix]",
schema: &resources.Schema{
CreateSchema: &catalog.CreateSchema{
Name: "schema1",
CatalogName: "dev_john_smith_test_catalog",
},
},
want: "schema1",
},
{
name: "skip prefix because catalog contains email",
prefix: "[prefix]",
schema: &resources.Schema{
CreateSchema: &catalog.CreateSchema{
Name: "schema1",
CatalogName: "dev_john.smith@databricks.com_test_catalog",
},
},
want: "schema1",
},
{
name: "add prefix because catalog is not namespaced to user",
prefix: "[prefix]",
schema: &resources.Schema{
CreateSchema: &catalog.CreateSchema{
Name: "schema1",
CatalogName: "test_catalog",
},
},
want: "prefix_schema1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := &bundle.Bundle{
Config: config.Root{
Workspace: config.Workspace{
CurrentUser: &config.User{
ShortName: "john_smith",
User: &iam.User{
UserName: "john.smith@databricks.com",
},
},
},
Resources: config.Resources{
Schemas: map[string]*resources.Schema{
"schema1": tt.schema,

View File

@ -63,6 +63,10 @@ func transformDevelopmentMode(ctx context.Context, b *bundle.Bundle) {
}
}
func containsUserIdentity(s string, u *config.User) bool {
return strings.Contains(s, u.ShortName) || strings.Contains(s, u.UserName)
}
func validateDevelopmentMode(b *bundle.Bundle) diag.Diagnostics {
var diags diag.Diagnostics
p := b.Config.Presets
@ -92,7 +96,7 @@ func validateDevelopmentMode(b *bundle.Bundle) diag.Diagnostics {
diags = diags.Extend(diag.Errorf("%s must start with '~/' or contain the current username to ensure uniqueness when using 'mode: development'", path))
}
}
if p.NamePrefix != "" && !strings.Contains(p.NamePrefix, u.ShortName) && !strings.Contains(p.NamePrefix, u.UserName) {
if p.NamePrefix != "" && !containsUserIdentity(p.NamePrefix, u) {
// Resources such as pipelines require a unique name, e.g. '[dev steve] my_pipeline'.
// For this reason we require the name prefix to contain the current username;
// it's a pitfall for users if they don't include it and later find out that