-
Notifications
You must be signed in to change notification settings - Fork 127
Patch references to UC schemas to capture dependencies automatically #1989
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8a7d5fd
Warn user to use ${resources.schemas...} syntax
shreyas-goenka 332a6b0
add comments'
shreyas-goenka 32ebc5c
Merge remote-tracking branch 'origin' into detect/schema-dep
shreyas-goenka 893118e
fix silently
shreyas-goenka 1d337c8
-
shreyas-goenka aa52b1d
-
shreyas-goenka f7d926e
Merge remote-tracking branch 'origin' into detect/schema-dep
shreyas-goenka fc8b5e7
address comments
shreyas-goenka ed59500
address comments
shreyas-goenka fcc47de
address comments
shreyas-goenka 4d09201
two functions
shreyas-goenka 69d477f
-
shreyas-goenka a5243d6
merge
shreyas-goenka 58aca2a
address comments
shreyas-goenka ba3a36e
fix test
shreyas-goenka 5eb7f4a
address comments
shreyas-goenka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package mutator | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/databricks/cli/bundle" | ||
| "github.com/databricks/cli/bundle/config/resources" | ||
| "github.com/databricks/cli/libs/diag" | ||
| ) | ||
|
|
||
| type captureSchemaDependency struct{} | ||
|
|
||
| // If a user defines a UC schema in the bundle, they can refer to it in DLT pipelines | ||
| // or UC Volumes using the `${resources.schemas.<schema_key>.name}` syntax. Using this | ||
| // syntax allows TF to capture the deploy time dependency this DLT pipeline or UC Volume | ||
| // has on the schema and deploy changes to the schema before deploying the pipeline or volume. | ||
| // | ||
| // This mutator translates any implicit schema references in DLT pipelines or UC Volumes | ||
| // to the explicit syntax. | ||
| func CaptureSchemaDependency() bundle.Mutator { | ||
| return &captureSchemaDependency{} | ||
| } | ||
|
|
||
| func (m *captureSchemaDependency) Name() string { | ||
| return "CaptureSchemaDependency" | ||
| } | ||
|
|
||
| func schemaNameRef(key string) string { | ||
| return fmt.Sprintf("${resources.schemas.%s.name}", key) | ||
| } | ||
|
|
||
| func findSchema(b *bundle.Bundle, catalogName, schemaName string) (string, *resources.Schema) { | ||
| if catalogName == "" || schemaName == "" { | ||
| return "", nil | ||
| } | ||
|
|
||
| for k, s := range b.Config.Resources.Schemas { | ||
| if s != nil && s.CreateSchema != nil && s.CatalogName == catalogName && s.Name == schemaName { | ||
| return k, s | ||
| } | ||
| } | ||
| return "", nil | ||
| } | ||
|
|
||
| func resolveVolume(v *resources.Volume, b *bundle.Bundle) { | ||
| if v == nil || v.CreateVolumeRequestContent == nil { | ||
| return | ||
| } | ||
| schemaK, schema := findSchema(b, v.CatalogName, v.SchemaName) | ||
| if schema == nil { | ||
| return | ||
| } | ||
|
|
||
| v.SchemaName = schemaNameRef(schemaK) | ||
| } | ||
|
|
||
| func resolvePipelineSchema(p *resources.Pipeline, b *bundle.Bundle) { | ||
| if p == nil || p.PipelineSpec == nil { | ||
| return | ||
| } | ||
| if p.Schema == "" { | ||
| return | ||
| } | ||
| schemaK, schema := findSchema(b, p.Catalog, p.Schema) | ||
| if schema == nil { | ||
| return | ||
| } | ||
|
|
||
| p.Schema = schemaNameRef(schemaK) | ||
| } | ||
|
|
||
| func resolvePipelineTarget(p *resources.Pipeline, b *bundle.Bundle) { | ||
| if p == nil || p.PipelineSpec == nil { | ||
| return | ||
| } | ||
shreyas-goenka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if p.Target == "" { | ||
| return | ||
| } | ||
| schemaK, schema := findSchema(b, p.Catalog, p.Target) | ||
| if schema == nil { | ||
| return | ||
| } | ||
| p.Target = schemaNameRef(schemaK) | ||
| } | ||
|
|
||
| func (m *captureSchemaDependency) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics { | ||
| for _, p := range b.Config.Resources.Pipelines { | ||
| // "schema" and "target" have the same semantics in the DLT API but are mutually | ||
| // exclusive i.e. only one can be set at a time. If schema is set, the pipeline | ||
| // is in direct publishing mode and can write tables to multiple schemas | ||
| // (vs target which is limited to a single schema). | ||
| resolvePipelineTarget(p, b) | ||
| resolvePipelineSchema(p, b) | ||
| } | ||
| for _, v := range b.Config.Resources.Volumes { | ||
| resolveVolume(v, b) | ||
| } | ||
| return nil | ||
| } | ||
277 changes: 277 additions & 0 deletions
277
bundle/config/mutator/capture_schema_dependency_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,277 @@ | ||
| package mutator | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/databricks/cli/bundle" | ||
| "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/pipelines" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestCaptureSchemaDependencyForVolume(t *testing.T) { | ||
| b := &bundle.Bundle{ | ||
| Config: config.Root{ | ||
| Resources: config.Resources{ | ||
| Schemas: map[string]*resources.Schema{ | ||
| "schema1": { | ||
| CreateSchema: &catalog.CreateSchema{ | ||
| CatalogName: "catalog1", | ||
| Name: "foobar", | ||
| }, | ||
| }, | ||
| "schema2": { | ||
| CreateSchema: &catalog.CreateSchema{ | ||
| CatalogName: "catalog2", | ||
| Name: "foobar", | ||
| }, | ||
| }, | ||
| "schema3": { | ||
| CreateSchema: &catalog.CreateSchema{ | ||
| CatalogName: "catalog1", | ||
| Name: "barfoo", | ||
| }, | ||
| }, | ||
| "nilschema": nil, | ||
| "emptyschema": {}, | ||
| }, | ||
| Volumes: map[string]*resources.Volume{ | ||
| "volume1": { | ||
| CreateVolumeRequestContent: &catalog.CreateVolumeRequestContent{ | ||
| CatalogName: "catalog1", | ||
| SchemaName: "foobar", | ||
| }, | ||
| }, | ||
| "volume2": { | ||
| CreateVolumeRequestContent: &catalog.CreateVolumeRequestContent{ | ||
| CatalogName: "catalog2", | ||
| SchemaName: "foobar", | ||
| }, | ||
| }, | ||
| "volume3": { | ||
| CreateVolumeRequestContent: &catalog.CreateVolumeRequestContent{ | ||
| CatalogName: "catalog1", | ||
| SchemaName: "barfoo", | ||
| }, | ||
| }, | ||
| "volume4": { | ||
| CreateVolumeRequestContent: &catalog.CreateVolumeRequestContent{ | ||
| CatalogName: "catalogX", | ||
| SchemaName: "foobar", | ||
| }, | ||
| }, | ||
| "volume5": { | ||
| CreateVolumeRequestContent: &catalog.CreateVolumeRequestContent{ | ||
| CatalogName: "catalog1", | ||
| SchemaName: "schemaX", | ||
| }, | ||
| }, | ||
| "nilVolume": nil, | ||
| "emptyVolume": {}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| d := bundle.Apply(context.Background(), b, CaptureSchemaDependency()) | ||
| require.Nil(t, d) | ||
|
|
||
| assert.Equal(t, "${resources.schemas.schema1.name}", b.Config.Resources.Volumes["volume1"].CreateVolumeRequestContent.SchemaName) | ||
| assert.Equal(t, "${resources.schemas.schema2.name}", b.Config.Resources.Volumes["volume2"].CreateVolumeRequestContent.SchemaName) | ||
| assert.Equal(t, "${resources.schemas.schema3.name}", b.Config.Resources.Volumes["volume3"].CreateVolumeRequestContent.SchemaName) | ||
| assert.Equal(t, "foobar", b.Config.Resources.Volumes["volume4"].CreateVolumeRequestContent.SchemaName) | ||
| assert.Equal(t, "schemaX", b.Config.Resources.Volumes["volume5"].CreateVolumeRequestContent.SchemaName) | ||
|
|
||
| assert.Nil(t, b.Config.Resources.Volumes["nilVolume"]) | ||
| assert.Nil(t, b.Config.Resources.Volumes["emptyVolume"].CreateVolumeRequestContent) | ||
| } | ||
|
|
||
| func TestCaptureSchemaDependencyForPipelinesWithTarget(t *testing.T) { | ||
| b := &bundle.Bundle{ | ||
| Config: config.Root{ | ||
| Resources: config.Resources{ | ||
| Schemas: map[string]*resources.Schema{ | ||
| "schema1": { | ||
| CreateSchema: &catalog.CreateSchema{ | ||
| CatalogName: "catalog1", | ||
| Name: "foobar", | ||
| }, | ||
| }, | ||
| "schema2": { | ||
| CreateSchema: &catalog.CreateSchema{ | ||
| CatalogName: "catalog2", | ||
| Name: "foobar", | ||
| }, | ||
| }, | ||
| "schema3": { | ||
| CreateSchema: &catalog.CreateSchema{ | ||
| CatalogName: "catalog1", | ||
| Name: "barfoo", | ||
| }, | ||
| }, | ||
| "nilschema": nil, | ||
| "emptyschema": {}, | ||
| }, | ||
| Pipelines: map[string]*resources.Pipeline{ | ||
| "pipeline1": { | ||
| PipelineSpec: &pipelines.PipelineSpec{ | ||
| Catalog: "catalog1", | ||
| Schema: "foobar", | ||
| }, | ||
| }, | ||
| "pipeline2": { | ||
| PipelineSpec: &pipelines.PipelineSpec{ | ||
| Catalog: "catalog2", | ||
| Schema: "foobar", | ||
| }, | ||
| }, | ||
| "pipeline3": { | ||
| PipelineSpec: &pipelines.PipelineSpec{ | ||
| Catalog: "catalog1", | ||
| Schema: "barfoo", | ||
| }, | ||
| }, | ||
| "pipeline4": { | ||
| PipelineSpec: &pipelines.PipelineSpec{ | ||
| Catalog: "catalogX", | ||
| Schema: "foobar", | ||
| }, | ||
| }, | ||
| "pipeline5": { | ||
| PipelineSpec: &pipelines.PipelineSpec{ | ||
| Catalog: "catalog1", | ||
| Schema: "schemaX", | ||
| }, | ||
| }, | ||
| "pipeline6": { | ||
| PipelineSpec: &pipelines.PipelineSpec{ | ||
| Catalog: "", | ||
| Schema: "foobar", | ||
| }, | ||
| }, | ||
| "pipeline7": { | ||
| PipelineSpec: &pipelines.PipelineSpec{ | ||
| Catalog: "", | ||
| Schema: "", | ||
| Name: "whatever", | ||
| }, | ||
| }, | ||
| "nilPipeline": nil, | ||
| "emptyPipeline": {}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| d := bundle.Apply(context.Background(), b, CaptureSchemaDependency()) | ||
| require.Nil(t, d) | ||
|
|
||
| assert.Equal(t, "${resources.schemas.schema1.name}", b.Config.Resources.Pipelines["pipeline1"].Schema) | ||
| assert.Equal(t, "${resources.schemas.schema2.name}", b.Config.Resources.Pipelines["pipeline2"].Schema) | ||
| assert.Equal(t, "${resources.schemas.schema3.name}", b.Config.Resources.Pipelines["pipeline3"].Schema) | ||
| assert.Equal(t, "foobar", b.Config.Resources.Pipelines["pipeline4"].Schema) | ||
| assert.Equal(t, "schemaX", b.Config.Resources.Pipelines["pipeline5"].Schema) | ||
| assert.Equal(t, "foobar", b.Config.Resources.Pipelines["pipeline6"].Schema) | ||
| assert.Equal(t, "", b.Config.Resources.Pipelines["pipeline7"].Schema) | ||
|
|
||
| assert.Nil(t, b.Config.Resources.Pipelines["nilPipeline"]) | ||
| assert.Nil(t, b.Config.Resources.Pipelines["emptyPipeline"].PipelineSpec) | ||
|
|
||
| for _, k := range []string{"pipeline1", "pipeline2", "pipeline3", "pipeline4", "pipeline5", "pipeline6", "pipeline7"} { | ||
| assert.Empty(t, b.Config.Resources.Pipelines[k].Target) | ||
| } | ||
| } | ||
|
|
||
| func TestCaptureSchemaDependencyForPipelinesWithSchema(t *testing.T) { | ||
| b := &bundle.Bundle{ | ||
| Config: config.Root{ | ||
| Resources: config.Resources{ | ||
| Schemas: map[string]*resources.Schema{ | ||
| "schema1": { | ||
| CreateSchema: &catalog.CreateSchema{ | ||
| CatalogName: "catalog1", | ||
| Name: "foobar", | ||
| }, | ||
| }, | ||
| "schema2": { | ||
| CreateSchema: &catalog.CreateSchema{ | ||
| CatalogName: "catalog2", | ||
| Name: "foobar", | ||
| }, | ||
| }, | ||
| "schema3": { | ||
| CreateSchema: &catalog.CreateSchema{ | ||
| CatalogName: "catalog1", | ||
| Name: "barfoo", | ||
| }, | ||
| }, | ||
| "nilschema": nil, | ||
| "emptyschema": {}, | ||
| }, | ||
| Pipelines: map[string]*resources.Pipeline{ | ||
| "pipeline1": { | ||
| PipelineSpec: &pipelines.PipelineSpec{ | ||
| Catalog: "catalog1", | ||
| Target: "foobar", | ||
| }, | ||
| }, | ||
| "pipeline2": { | ||
| PipelineSpec: &pipelines.PipelineSpec{ | ||
| Catalog: "catalog2", | ||
| Target: "foobar", | ||
| }, | ||
| }, | ||
| "pipeline3": { | ||
| PipelineSpec: &pipelines.PipelineSpec{ | ||
| Catalog: "catalog1", | ||
| Target: "barfoo", | ||
| }, | ||
| }, | ||
| "pipeline4": { | ||
| PipelineSpec: &pipelines.PipelineSpec{ | ||
| Catalog: "catalogX", | ||
| Target: "foobar", | ||
| }, | ||
| }, | ||
| "pipeline5": { | ||
| PipelineSpec: &pipelines.PipelineSpec{ | ||
| Catalog: "catalog1", | ||
| Target: "schemaX", | ||
| }, | ||
| }, | ||
| "pipeline6": { | ||
| PipelineSpec: &pipelines.PipelineSpec{ | ||
| Catalog: "", | ||
| Target: "foobar", | ||
| }, | ||
| }, | ||
| "pipeline7": { | ||
| PipelineSpec: &pipelines.PipelineSpec{ | ||
| Catalog: "", | ||
| Target: "", | ||
| Name: "whatever", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| d := bundle.Apply(context.Background(), b, CaptureSchemaDependency()) | ||
| require.Nil(t, d) | ||
| assert.Equal(t, "${resources.schemas.schema1.name}", b.Config.Resources.Pipelines["pipeline1"].Target) | ||
| assert.Equal(t, "${resources.schemas.schema2.name}", b.Config.Resources.Pipelines["pipeline2"].Target) | ||
| assert.Equal(t, "${resources.schemas.schema3.name}", b.Config.Resources.Pipelines["pipeline3"].Target) | ||
| assert.Equal(t, "foobar", b.Config.Resources.Pipelines["pipeline4"].Target) | ||
| assert.Equal(t, "schemaX", b.Config.Resources.Pipelines["pipeline5"].Target) | ||
| assert.Equal(t, "foobar", b.Config.Resources.Pipelines["pipeline6"].Target) | ||
| assert.Equal(t, "", b.Config.Resources.Pipelines["pipeline7"].Target) | ||
|
|
||
| for _, k := range []string{"pipeline1", "pipeline2", "pipeline3", "pipeline4", "pipeline5", "pipeline6", "pipeline7"} { | ||
| assert.Empty(t, b.Config.Resources.Pipelines[k].Schema) | ||
| } | ||
| } | ||
shreyas-goenka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.