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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
bundle:
name: yaml-sync-empty-grants

resources:
schemas:
schema1:
name: myschema
catalog_name: main
grants: []
3 changes: 3 additions & 0 deletions acceptance/bundle/deploy/yaml-sync-empty-grants/out.test.toml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions acceptance/bundle/deploy/yaml-sync-empty-grants/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

>>> [CLI] bundle deploy
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/yaml-sync-empty-grants/default/files...
Deploying resources...
Updating deployment state...
Warn: Failed to create config snapshot: state conversion failed
Warn: Config snapshot: state entry not found for "resources.schemas.schema1.grants"
Deployment complete!
1 change: 1 addition & 0 deletions acceptance/bundle/deploy/yaml-sync-empty-grants/script
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
trace $CLI bundle deploy
6 changes: 6 additions & 0 deletions acceptance/bundle/deploy/yaml-sync-empty-grants/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# The YAML-sync state upload only runs for the terraform engine; no direct-engine variant.
[Env]
DATABRICKS_BUNDLE_ENABLE_EXPERIMENTAL_YAML_SYNC = "true"

[EnvMatrix]
DATABRICKS_BUNDLE_ENGINE = ["terraform"]
20 changes: 20 additions & 0 deletions bundle/statemgmt/upload_state_for_yaml_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ func (m *uploadStateForYamlSync) Apply(ctx context.Context, b *bundle.Bundle) di
return nil
}

// convertState reuses direct-engine code that reports failures via logdiag,
// and this mutator must not fail a deploy that already succeeded.
ctx = logdiag.IsolatedContext(ctx)
logdiag.SetCollect(ctx, true)
defer func() {
for _, d := range logdiag.FlushCollected(ctx) {
msg := d.Summary
if d.Detail != "" {
msg += ": " + d.Detail
}
log.Warnf(ctx, "Config snapshot: %s", msg)
}
}()

_, snapshotPath := b.StateFilenameConfigSnapshot(ctx)

created, err := m.convertState(ctx, b, snapshotPath)
Expand Down Expand Up @@ -202,6 +216,12 @@ func (m *uploadStateForYamlSync) convertState(ctx context.Context, b *bundle.Bun
return false, err
}

// Apply reports failures via logdiag instead of returning an error. Don't
// upload a snapshot that is missing entries for the failed resources.
if logdiag.HasError(ctx) {
return false, errors.New("state conversion failed")
}

return true, nil
}

Expand Down
6 changes: 6 additions & 0 deletions libs/logdiag/logdiag.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ func InitContext(ctx context.Context) context.Context {
if ok {
panic("internal error: must not call InitContext() twice")
}
return IsolatedContext(ctx)
}

// IsolatedContext returns a child context with a fresh diagnostics state;
// diagnostics logged through it do not affect the parent's.
func IsolatedContext(ctx context.Context) context.Context {
val := LogDiagData{
TargetSeverity: 255,
mu: &sync.Mutex{},
Expand Down
24 changes: 24 additions & 0 deletions libs/logdiag/logdiag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package logdiag_test

import (
"errors"
"testing"

"github.com/databricks/cli/libs/logdiag"
"github.com/stretchr/testify/assert"
)

func TestIsolatedContext(t *testing.T) {
ctx := logdiag.InitContext(t.Context())
logdiag.SetCollect(ctx, true)

isolated := logdiag.IsolatedContext(ctx)
logdiag.SetCollect(isolated, true)
logdiag.LogError(isolated, errors.New("inner failure"))

assert.True(t, logdiag.HasError(isolated))
assert.Len(t, logdiag.FlushCollected(isolated), 1)

assert.False(t, logdiag.HasError(ctx))
assert.Empty(t, logdiag.FlushCollected(ctx))
}
Loading