From 7e9e91f49da48c285eb1b4a8db2219560926123a Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Wed, 21 Aug 2024 14:36:03 +0200 Subject: [PATCH 1/2] Fix prefix preset used for UC schemas --- bundle/config/mutator/apply_presets.go | 3 +- bundle/config/mutator/apply_presets_test.go | 57 +++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/bundle/config/mutator/apply_presets.go b/bundle/config/mutator/apply_presets.go index 42e6ab95f5c..28d015c1095 100644 --- a/bundle/config/mutator/apply_presets.go +++ b/bundle/config/mutator/apply_presets.go @@ -155,8 +155,7 @@ func (m *applyPresets) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnos // Schemas: Prefix for i := range r.Schemas { - prefix = "dev_" + b.Config.Workspace.CurrentUser.ShortName + "_" - r.Schemas[i].Name = prefix + r.Schemas[i].Name + r.Schemas[i].Name = normalizePrefix(prefix) + r.Schemas[i].Name // HTTP API for schemas doesn't yet support tags. It's only supported in // the Databricks UI and via the SQL API. } diff --git a/bundle/config/mutator/apply_presets_test.go b/bundle/config/mutator/apply_presets_test.go index 35dac1f7dc3..c16b5441a84 100644 --- a/bundle/config/mutator/apply_presets_test.go +++ b/bundle/config/mutator/apply_presets_test.go @@ -8,6 +8,7 @@ import ( "github.com/databricks/cli/bundle/config" "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/jobs" "github.com/stretchr/testify/require" ) @@ -68,6 +69,62 @@ func TestApplyPresetsPrefix(t *testing.T) { } } +func TestApplyPresetsPrefixForUcSchema(t *testing.T) { + tests := []struct { + name string + prefix string + schema *resources.Schema + want string + }{ + { + name: "add prefix to job", + prefix: "[prefix]", + schema: &resources.Schema{ + CreateSchema: &catalog.CreateSchema{ + Name: "schema1", + }, + }, + want: "prefix_schema1", + }, + { + name: "add empty prefix to job", + prefix: "", + schema: &resources.Schema{ + CreateSchema: &catalog.CreateSchema{ + Name: "schema1", + }, + }, + want: "schema1", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := &bundle.Bundle{ + Config: config.Root{ + Resources: config.Resources{ + Schemas: map[string]*resources.Schema{ + "schema1": tt.schema, + }, + }, + Presets: config.Presets{ + NamePrefix: tt.prefix, + }, + }, + } + + ctx := context.Background() + diag := bundle.Apply(ctx, b, mutator.ApplyPresets()) + + if diag.HasError() { + t.Fatalf("unexpected error: %v", diag) + } + + require.Equal(t, tt.want, b.Config.Resources.Schemas["schema1"].Name) + }) + } +} + func TestApplyPresetsTags(t *testing.T) { tests := []struct { name string From 70cbacd74afbec88e7bf4d4650473c6349fad518 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Wed, 21 Aug 2024 14:40:29 +0200 Subject: [PATCH 2/2] - --- bundle/config/mutator/apply_presets_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bundle/config/mutator/apply_presets_test.go b/bundle/config/mutator/apply_presets_test.go index c16b5441a84..ab2478aee03 100644 --- a/bundle/config/mutator/apply_presets_test.go +++ b/bundle/config/mutator/apply_presets_test.go @@ -77,7 +77,7 @@ func TestApplyPresetsPrefixForUcSchema(t *testing.T) { want string }{ { - name: "add prefix to job", + name: "add prefix to schema", prefix: "[prefix]", schema: &resources.Schema{ CreateSchema: &catalog.CreateSchema{ @@ -87,7 +87,7 @@ func TestApplyPresetsPrefixForUcSchema(t *testing.T) { want: "prefix_schema1", }, { - name: "add empty prefix to job", + name: "add empty prefix to schema", prefix: "", schema: &resources.Schema{ CreateSchema: &catalog.CreateSchema{