Skip to content
Draft
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
33 changes: 19 additions & 14 deletions bundle/phases/dms.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,19 +266,31 @@ func logDeploymentVersion(ctx context.Context, b *bundle.Bundle) {
cmdio.LogString(ctx, "Current Deployment Version: "+workspaceurls.DeploymentURL(*baseURL, deploymentID, version))
}

// deploymentMetadata describes the bundle this deploy came from and where it
// landed, mirroring what bundle/deploy/metadata computes for the metadata file.
func deploymentMetadata(b *bundle.Bundle) (dms.Metadata, error) {
// bundleMetadata describes the bundle and workspace without deployed file metadata.
func bundleMetadata(b *bundle.Bundle) dms.Metadata {
p := dms.Metadata{
DisplayName: b.Config.Bundle.Name,
TargetName: b.Config.Bundle.Target,
Mode: deploymentModeToSDK(b.Config.Bundle.Mode),
Workspace: &bundledeployments.WorkspaceInfo{
RootPath: b.Config.Workspace.RootPath,
},
}

ws := &bundledeployments.WorkspaceInfo{
RootPath: b.Config.Workspace.RootPath,
FilePath: b.Config.Workspace.FilePath,
// Only a deploy from a Databricks Git folder has one; a local worktree does not.
// bundle_root_path is relative to it, so the service requires both or neither.
if b.WorktreeRoot != nil && strings.HasPrefix(b.WorktreeRoot.Native(), "/Workspace/") {
p.Workspace.GitFolderPath = b.WorktreeRoot.Native()
p.Workspace.BundleRootPath = b.Config.Bundle.Git.BundleRootPath
}
return p
}

// deploymentMetadata describes the bundle this deploy came from and where it
// landed, mirroring what bundle/deploy/metadata computes for the metadata file.
func deploymentMetadata(b *bundle.Bundle) (dms.Metadata, error) {
p := bundleMetadata(b)
ws := p.Workspace
ws.FilePath = b.Config.Workspace.FilePath
// With an immutable folder, file_path is a reference to the snapshot, which only
// resolves once the snapshot is uploaded. Its path is already known from the staged zip.
if b.IsImmutableFolder() {
Expand All @@ -294,13 +306,6 @@ func deploymentMetadata(b *bundle.Bundle) (dms.Metadata, error) {
ws.FilePath = b.SyncRootPath
ws.SourceLinked = true
}
// Only a deploy from a Databricks Git folder has one; a local worktree does not.
// bundle_root_path is relative to it, so the service requires both or neither.
if b.WorktreeRoot != nil && strings.HasPrefix(b.WorktreeRoot.Native(), "/Workspace/") {
ws.GitFolderPath = b.WorktreeRoot.Native()
ws.BundleRootPath = b.Config.Bundle.Git.BundleRootPath
}
p.Workspace = ws
return p, nil
}

Expand Down
27 changes: 27 additions & 0 deletions bundle/phases/dms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,39 @@ package phases
import (
"testing"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/bundle/deployplan"
"github.com/databricks/databricks-sdk-go/service/bundledeployments"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestBundleMetadataWithoutDeployedFiles(t *testing.T) {
for _, mode := range []config.SyncMode{config.SyncModeImmutable, config.SyncModeInPlace} {
t.Run(string(mode), func(t *testing.T) {
b := &bundle.Bundle{
Config: config.Root{
Bundle: config.Bundle{Name: "test", Target: "prod", Mode: config.Production},
Workspace: config.Workspace{
RootPath: "/Workspace/bundle",
FilePath: "${resources.snapshots.files.full_path}/files",
SyncMode: mode,
},
},
SyncRootPath: "/Workspace/source",
}

assert.Equal(t, bundledeployments.Deployment{
DisplayName: "test",
TargetName: "prod",
DeploymentMode: bundledeployments.DeploymentModeDeploymentModeProduction,
WorkspaceInfo: &bundledeployments.WorkspaceInfo{RootPath: "/Workspace/bundle"},
}, bundleMetadata(b).Deployment())
})
}
}

func TestStagedOperationsCoversEveryTouchedResource(t *testing.T) {
// The version fixes its operation set, so anything the apply will write has to appear
// here. Keys go out in the service's form, without the CLI's "resources." prefix.
Expand Down
20 changes: 3 additions & 17 deletions cmd/bundle/utils/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"path/filepath"
"slices"
"strconv"
"time"

"github.com/databricks/cli/bundle"
Expand Down Expand Up @@ -417,7 +416,7 @@ func ProcessBundleRet(cmd *cobra.Command, opts ProcessOptions) (b *bundle.Bundle
// the plan carries them. version_id is always known (last recorded + 1); deployment_id
// does not exist until a first deploy creates it, so it is left off here and the deploy
// phase stamps the created id.
lastVersionID, err := parseLastVersionID(dmsDeployment)
lastVersionID, err := dms.ParseLastVersionID(dmsDeployment)
if err != nil {
logdiag.LogError(ctx, err)
return b, stateDesc, root.ErrAlreadyPrinted
Expand Down Expand Up @@ -629,19 +628,6 @@ func ValidateEngineSetting(ctx context.Context, b *bundle.Bundle) error {
return nil
}

// parseLastVersionID parses the deployment's last recorded version, which the service reports
// as a string. It returns 0 when the deployment does not exist yet or has no recorded version.
func parseLastVersionID(dmsDeployment *bundledeployments.Deployment) (int, error) {
if dmsDeployment == nil || dmsDeployment.LastVersionId == "" {
return 0, nil
}
v, err := strconv.Atoi(dmsDeployment.LastVersionId)
if err != nil {
return 0, fmt.Errorf("failed to parse last_version_id %q: %w", dmsDeployment.LastVersionId, err)
}
return v, nil
}

// OpenDirectStateForRead opens the direct-engine state database read-only. When the bundle
// records deployment history the local state file is only a tombstone, so the resources are
// read from the deployment metadata service instead.
Expand All @@ -658,7 +644,7 @@ func OpenDirectStateForRead(ctx context.Context, b *bundle.Bundle, stateDesc *st
if err != nil {
return err
}
lastVersionID, err := parseLastVersionID(dmsDeployment)
lastVersionID, err := dms.ParseLastVersionID(dmsDeployment)
if err != nil {
return err
}
Expand All @@ -678,7 +664,7 @@ func prepareDeploymentHistoryShadow(ctx context.Context, b *bundle.Bundle) (*bun
if err != nil {
return nil, err
}
lastVersion, err := parseLastVersionID(deployment)
lastVersion, err := dms.ParseLastVersionID(deployment)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/bundle/utils/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"testing"

"github.com/databricks/cli/libs/dms"
"github.com/databricks/databricks-sdk-go/service/bundledeployments"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -61,7 +62,7 @@ func TestParseLastVersionID(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseLastVersionID(tt.deployment)
got, err := dms.ParseLastVersionID(tt.deployment)
if tt.wantErr {
require.Error(t, err)
return
Expand Down
13 changes: 13 additions & 0 deletions libs/dms/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ func (m Metadata) StaleFields(current *bundledeployments.Deployment) string {
return strings.Join(stale, ",")
}

// ParseLastVersionID parses the deployment's last recorded version, which the service reports
// as a string. It returns 0 when the deployment does not exist yet or has no recorded version.
func ParseLastVersionID(dmsDeployment *bundledeployments.Deployment) (int, error) {
if dmsDeployment == nil || dmsDeployment.LastVersionId == "" {
return 0, nil
}
v, err := strconv.Atoi(dmsDeployment.LastVersionId)
if err != nil {
return 0, fmt.Errorf("failed to parse last_version_id %q: %w", dmsDeployment.LastVersionId, err)
}
return v, nil
}

// NextVersion is the version number this run will create, given the deployment's most recent
// one. lastVersionID is empty before the deployment has any version.
func NextVersion(lastVersionID string) (int, error) {
Expand Down
Loading