diff --git a/bundle/phases/dms.go b/bundle/phases/dms.go index bd5d846420d..f6f29b833a0 100644 --- a/bundle/phases/dms.go +++ b/bundle/phases/dms.go @@ -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() { @@ -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 } diff --git a/bundle/phases/dms_test.go b/bundle/phases/dms_test.go index 6b045f863ae..cbb98f9fe63 100644 --- a/bundle/phases/dms_test.go +++ b/bundle/phases/dms_test.go @@ -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. diff --git a/cmd/bundle/utils/process.go b/cmd/bundle/utils/process.go index 423c41f35ae..4de2b8db671 100644 --- a/cmd/bundle/utils/process.go +++ b/cmd/bundle/utils/process.go @@ -6,7 +6,6 @@ import ( "fmt" "path/filepath" "slices" - "strconv" "time" "github.com/databricks/cli/bundle" @@ -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 @@ -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. @@ -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 } @@ -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 } diff --git a/cmd/bundle/utils/process_test.go b/cmd/bundle/utils/process_test.go index 21910039344..b05fb9b0a84 100644 --- a/cmd/bundle/utils/process_test.go +++ b/cmd/bundle/utils/process_test.go @@ -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" @@ -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 diff --git a/libs/dms/metadata.go b/libs/dms/metadata.go index f9e23a67db0..a44450612e9 100644 --- a/libs/dms/metadata.go +++ b/libs/dms/metadata.go @@ -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) {