From 0e9694c672b96419ebae3eefda92a2c484f47ff3 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Nov 2026 07:52:41 +0000 Subject: [PATCH 01/29] Record deployment history for bundle resource bindings --- .nextchanges/bundles/record-bind-history.md | 1 + .../recreation/out.state_after_bind.json | 1 - .../bind/dashboard/recreation/script | 2 +- .../bind/feature/out.bind.false.txt | 4 + .../deployment/bind/feature/out.bind.true.txt | 5 ++ .../deployment/bind/feature/out.test.toml | 2 +- .../bundle/deployment/bind/feature/output.txt | 4 - .../bundle/deployment/bind/feature/script | 2 +- .../bundle/deployment/bind/feature/test.toml | 2 + .../deployment/bind/pipelines/recreate/script | 2 +- .../deployment/bind/pipelines/update/script | 2 +- .../bind-history-failures/databricks.yml.tmpl | 13 ++++ .../dms/bind-history-failures/out.test.toml | 2 + .../dms/bind-history-failures/output.txt | 73 ++++++++++++++++++ .../bundle/dms/bind-history-failures/script | 33 ++++++++ .../dms/bind-history-failures/test.toml | 1 + acceptance/bundle/dms/bind-unbind/output.txt | 45 ++++++++++- acceptance/bundle/dms/bind-unbind/script | 16 +++- acceptance/bundle/dms/bind-unbind/test.toml | 1 - bundle/phases/bind.go | 76 +++++++++++++++++++ bundle/phases/dms.go | 38 ++++++++++ cmd/bundle/utils/process.go | 46 +---------- 22 files changed, 312 insertions(+), 59 deletions(-) create mode 100644 .nextchanges/bundles/record-bind-history.md create mode 100644 acceptance/bundle/deployment/bind/feature/out.bind.false.txt create mode 100644 acceptance/bundle/deployment/bind/feature/out.bind.true.txt create mode 100644 acceptance/bundle/dms/bind-history-failures/databricks.yml.tmpl create mode 100644 acceptance/bundle/dms/bind-history-failures/out.test.toml create mode 100644 acceptance/bundle/dms/bind-history-failures/output.txt create mode 100644 acceptance/bundle/dms/bind-history-failures/script create mode 100644 acceptance/bundle/dms/bind-history-failures/test.toml diff --git a/.nextchanges/bundles/record-bind-history.md b/.nextchanges/bundles/record-bind-history.md new file mode 100644 index 00000000000..7fce080e9fc --- /dev/null +++ b/.nextchanges/bundles/record-bind-history.md @@ -0,0 +1 @@ +* Record deployment history when binding existing resources with `bundle deployment bind`. ([#7400](https://github.com/databricks/cli/pull/7400)) diff --git a/acceptance/bundle/deployment/bind/dashboard/recreation/out.state_after_bind.json b/acceptance/bundle/deployment/bind/dashboard/recreation/out.state_after_bind.json index 05614d798bf..4ba78767d69 100644 --- a/acceptance/bundle/deployment/bind/dashboard/recreation/out.state_after_bind.json +++ b/acceptance/bundle/deployment/bind/dashboard/recreation/out.state_after_bind.json @@ -2,7 +2,6 @@ "state_version": 3, "cli_version": "[CLI_VERSION]", "lineage": "[UUID]", - "serial": 2, "state": { "resources.dashboards.dashboard1": { "__id__": "[DASHBOARD_ID]", diff --git a/acceptance/bundle/deployment/bind/dashboard/recreation/script b/acceptance/bundle/deployment/bind/dashboard/recreation/script index c9656879b7e..1ef7b6071a5 100644 --- a/acceptance/bundle/deployment/bind/dashboard/recreation/script +++ b/acceptance/bundle/deployment/bind/dashboard/recreation/script @@ -13,7 +13,7 @@ cleanupRemoveDashboard() { trap cleanupRemoveDashboard EXIT trace $CLI bundle deployment bind dashboard1 "${DASHBOARD_ID}" --auto-approve -trace print_state.py --no-dms > out.state_after_bind.json +trace print_state.py --no-dms | jq 'del(.serial, .features)' | nostamp.py --indent 1 > out.state_after_bind.json ETAG=$(jq '.state["resources.dashboards.dashboard1"].state.etag // .resources[0].instances[0].attributes.etag // "no-etag"' < out.state_after_bind.json) echo $ETAG add_repl $ETAG ETAG diff --git a/acceptance/bundle/deployment/bind/feature/out.bind.false.txt b/acceptance/bundle/deployment/bind/feature/out.bind.false.txt new file mode 100644 index 00000000000..2155184a00f --- /dev/null +++ b/acceptance/bundle/deployment/bind/feature/out.bind.false.txt @@ -0,0 +1,4 @@ + +>>> [CLI] bundle deployment bind feature1 main.myschema.boundfeature --auto-approve +Successfully bound feature with an id 'main.myschema.boundfeature' +Run 'bundle deploy' to deploy changes to your workspace diff --git a/acceptance/bundle/deployment/bind/feature/out.bind.true.txt b/acceptance/bundle/deployment/bind/feature/out.bind.true.txt new file mode 100644 index 00000000000..e91d2b70ab7 --- /dev/null +++ b/acceptance/bundle/deployment/bind/feature/out.bind.true.txt @@ -0,0 +1,5 @@ + +>>> [CLI] bundle deployment bind feature1 main.myschema.boundfeature --auto-approve +Warn: Failed to record bind history: failed to create deployment version: operations.resource_key must have a known resource type prefix (e.g. 'jobs.', 'pipelines.'): features.feature1 +Successfully bound feature with an id 'main.myschema.boundfeature' +Run 'bundle deploy' to deploy changes to your workspace diff --git a/acceptance/bundle/deployment/bind/feature/out.test.toml b/acceptance/bundle/deployment/bind/feature/out.test.toml index a927a5fbc06..943f1dc5575 100644 --- a/acceptance/bundle/deployment/bind/feature/out.test.toml +++ b/acceptance/bundle/deployment/bind/feature/out.test.toml @@ -1,2 +1,2 @@ Cloud = false -EnvMatrix.DMS = ["", "true"] +EnvMatrix.DMS = ["false", "true"] diff --git a/acceptance/bundle/deployment/bind/feature/output.txt b/acceptance/bundle/deployment/bind/feature/output.txt index d4d47f6a660..8466bb0eaa5 100644 --- a/acceptance/bundle/deployment/bind/feature/output.txt +++ b/acceptance/bundle/deployment/bind/feature/output.txt @@ -1,8 +1,4 @@ ->>> [CLI] bundle deployment bind feature1 main.myschema.boundfeature --auto-approve -Successfully bound feature with an id 'main.myschema.boundfeature' -Run 'bundle deploy' to deploy changes to your workspace - >>> [CLI] bundle summary Name: test-bundle Target: default diff --git a/acceptance/bundle/deployment/bind/feature/script b/acceptance/bundle/deployment/bind/feature/script index 8e27f69056c..debd1951c68 100644 --- a/acceptance/bundle/deployment/bind/feature/script +++ b/acceptance/bundle/deployment/bind/feature/script @@ -1,4 +1,4 @@ -trace $CLI bundle deployment bind feature1 main.myschema.boundfeature --auto-approve +trace $CLI bundle deployment bind feature1 main.myschema.boundfeature --auto-approve 2> out.bind.$DMS.txt trace $CLI bundle summary trace $CLI bundle deployment unbind feature1 diff --git a/acceptance/bundle/deployment/bind/feature/test.toml b/acceptance/bundle/deployment/bind/feature/test.toml index 84e8bf9b8f4..5bb3b67ab85 100644 --- a/acceptance/bundle/deployment/bind/feature/test.toml +++ b/acceptance/bundle/deployment/bind/feature/test.toml @@ -1,3 +1,5 @@ +EnvMatrix.DMS = ["false", "true"] +EnvVaryOutput = "DMS" Cloud = false Ignore = [ diff --git a/acceptance/bundle/deployment/bind/pipelines/recreate/script b/acceptance/bundle/deployment/bind/pipelines/recreate/script index a9aa3403a35..0796fa0e9e6 100644 --- a/acceptance/bundle/deployment/bind/pipelines/recreate/script +++ b/acceptance/bundle/deployment/bind/pipelines/recreate/script @@ -12,7 +12,7 @@ rm -f "$OUT_REQUESTS" trace $CLI bundle deployment bind foo $NEW_PIPELINE_ID --auto-approve &> out.bind-success.txt trace print_requests.py '^//import-file/' '^//workspace/' -trace $CLI bundle summary -o json | jq .resources > out.summary.json +trace $CLI bundle summary -o json | jq .resources | nostamp.py > out.summary.json trace $CLI bundle plan trace musterr $CLI bundle deploy diff --git a/acceptance/bundle/deployment/bind/pipelines/update/script b/acceptance/bundle/deployment/bind/pipelines/update/script index 6bafa319765..d1a67c9b960 100644 --- a/acceptance/bundle/deployment/bind/pipelines/update/script +++ b/acceptance/bundle/deployment/bind/pipelines/update/script @@ -9,7 +9,7 @@ rm -f "$OUT_REQUESTS" trace $CLI bundle deployment bind foo $NEW_PIPELINE_ID --auto-approve &> out.bind-success.txt print_requests.py '^//import-file/' '^//workspace/delete' -trace $CLI bundle summary -o json > out.summary.json +trace $CLI bundle summary -o json | jq 'del(.bundle.deployment)' | nostamp.py > out.summary.json trace $CLI bundle plan rm -f "$OUT_REQUESTS" diff --git a/acceptance/bundle/dms/bind-history-failures/databricks.yml.tmpl b/acceptance/bundle/dms/bind-history-failures/databricks.yml.tmpl new file mode 100644 index 00000000000..6f6a7fba6a3 --- /dev/null +++ b/acceptance/bundle/dms/bind-history-failures/databricks.yml.tmpl @@ -0,0 +1,13 @@ +bundle: + name: dms-bind-history-failures-$UNIQUE_NAME + +resources: + jobs: + create_failed: + name: create_failed + record_failed: + name: record_failed + complete_failed: + name: complete_failed + upload_failed: + name: upload_failed diff --git a/acceptance/bundle/dms/bind-history-failures/out.test.toml b/acceptance/bundle/dms/bind-history-failures/out.test.toml new file mode 100644 index 00000000000..88e6fbd847c --- /dev/null +++ b/acceptance/bundle/dms/bind-history-failures/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +EnvMatrix.DMS = ["true"] diff --git a/acceptance/bundle/dms/bind-history-failures/output.txt b/acceptance/bundle/dms/bind-history-failures/output.txt new file mode 100644 index 00000000000..47cc75bae27 --- /dev/null +++ b/acceptance/bundle/dms/bind-history-failures/output.txt @@ -0,0 +1,73 @@ + +=== A version creation failure does not prevent binding +>>> fault.py POST /api/2.0/bundle/deployments/*/versions 400 0 1 + +>>> [CLI] bundle deployment bind create_failed [JOB_ID] --auto-approve +Warn: Failed to record bind history: failed to create deployment version: Fault injected by test. +Successfully bound job with an id '[JOB_ID]' +Run 'bundle deploy' to deploy changes to your workspace + +=== An operation recording failure does not prevent binding +>>> fault.py PATCH /api/2.0/bundle/** 400 0 1 + +>>> [CLI] bundle deployment bind record_failed [JOB_ID] --auto-approve +Warn: Failed to record deployment history: recording operation for resources.jobs.record_failed: Fault injected by test. +Successfully bound job with an id '[JOB_ID]' +Run 'bundle deploy' to deploy changes to your workspace + +=== A version completion failure does not prevent binding +>>> fault.py POST /api/2.0/bundle/deployments/*/versions/*/complete 400 0 1 + +>>> [CLI] bundle deployment bind complete_failed [JOB_ID] --auto-approve +Warn: Failed to complete deployment history version: Fault injected by test. +Successfully bound job with an id '[JOB_ID]' +Run 'bundle deploy' to deploy changes to your workspace + +=== All bindings survived the remote state round trips +>>> jq .state | map_values(.__id__) .databricks/bundle/default/resources.json +{ + "resources.jobs.complete_failed": "[JOB_ID]", + "resources.jobs.create_failed": "[JOB_ID]", + "resources.jobs.record_failed": "[JOB_ID]" +} + +>>> [CLI] jobs get [JOB_ID] +{ + "job_id": [JOB_ID], + "name": "bind-target-[UNIQUE_NAME]" +} + +>>> print_requests.py --dms //versions --oneline +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "3"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.create_failed"}], "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "6"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.record_failed"}], "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/6/operations/jobs.record_failed", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-history-failures-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"record_failed\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/6/complete", "body": {"completion_reason": "VERSION_COMPLETE_FAILURE"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "9"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.complete_failed"}], "previous_version_id": "6", "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/9/operations/jobs.complete_failed", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-history-failures-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"complete_failed\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/9/complete", "body": {"completion_reason": "VERSION_COMPLETE_SUCCESS"}} + +=== A remote state upload failure fails the bind version +>>> fault.py POST /api/2.0/workspace-files/import-file/**resources.json 400 0 1 + +>>> musterr [CLI] bundle deployment bind upload_failed [JOB_ID] --auto-approve +Error: Fault injected by test. (400 INJECTED) + +Endpoint: POST [DATABRICKS_URL]/api/2.0/workspace-files/import-file/Workspace%2FUsers%2F[USERNAME]%2F.bundle%2Fdms-bind-history-failures-[UNIQUE_NAME]%2Fdefault%2Fstate%2Fresources.json?overwrite=true +HTTP Status: 400 Bad Request +API error_code: INJECTED +API message: Fault injected by test. + + +>>> print_requests.py --dms //versions --oneline +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "12"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.upload_failed"}], "previous_version_id": "9", "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/12/operations/jobs.upload_failed", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-history-failures-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"upload_failed\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/12/complete", "body": {"completion_reason": "VERSION_COMPLETE_FAILURE"}} + +=== Remote state still contains only the successful bindings +>>> [CLI] bundle summary -o json +{ + "complete_failed": "[JOB_ID]", + "create_failed": "[JOB_ID]", + "record_failed": "[JOB_ID]", + "upload_failed": null +} diff --git a/acceptance/bundle/dms/bind-history-failures/script b/acceptance/bundle/dms/bind-history-failures/script new file mode 100644 index 00000000000..eecbabcd3e2 --- /dev/null +++ b/acceptance/bundle/dms/bind-history-failures/script @@ -0,0 +1,33 @@ +envsubst < databricks.yml.tmpl > databricks.yml +job_id=$($CLI jobs create --json '{"name": "bind-target-'$UNIQUE_NAME'"}' | jq -r '.job_id') +add_repl "$job_id" JOB_ID + +title "A version creation failure does not prevent binding" +trace fault.py "POST /api/2.0/bundle/deployments/*/versions" 400 0 1 +trace $CLI bundle deployment bind create_failed "$job_id" --auto-approve +rm -rf .databricks + +title "An operation recording failure does not prevent binding" +trace fault.py "PATCH /api/2.0/bundle/**" 400 0 1 +trace $CLI bundle deployment bind record_failed "$job_id" --auto-approve +rm -rf .databricks + +title "A version completion failure does not prevent binding" +trace fault.py "POST /api/2.0/bundle/deployments/*/versions/*/complete" 400 0 1 +trace $CLI bundle deployment bind complete_failed "$job_id" --auto-approve + +title "All bindings survived the remote state round trips" +trace jq '.state | map_values(.__id__)' .databricks/bundle/default/resources.json +trace $CLI jobs get "$job_id" | jq '{job_id, name: .settings.name}' +trace print_requests.py --dms //versions --oneline + + +title "A remote state upload failure fails the bind version" +trace fault.py "POST /api/2.0/workspace-files/import-file/**resources.json" 400 0 1 +trace musterr $CLI bundle deployment bind upload_failed "$job_id" --auto-approve +trace print_requests.py --dms //versions --oneline + +rm -rf .databricks +title "Remote state still contains only the successful bindings" +trace $CLI bundle summary -o json | jq '.resources.jobs | map_values(.id // null)' +rm -f "$OUT_REQUESTS" diff --git a/acceptance/bundle/dms/bind-history-failures/test.toml b/acceptance/bundle/dms/bind-history-failures/test.toml new file mode 100644 index 00000000000..0e4c9337799 --- /dev/null +++ b/acceptance/bundle/dms/bind-history-failures/test.toml @@ -0,0 +1 @@ +Cloud = false # Inject failures into the deployment history service. diff --git a/acceptance/bundle/dms/bind-unbind/output.txt b/acceptance/bundle/dms/bind-unbind/output.txt index daaa7345a94..a5fa3b20dbe 100644 --- a/acceptance/bundle/dms/bind-unbind/output.txt +++ b/acceptance/bundle/dms/bind-unbind/output.txt @@ -1,12 +1,38 @@ === Bind an existing job with deployment history enabled +>>> musterr [CLI] bundle deployment bind foo [JOB_ID] +Plan: update resources.jobs.foo + +Changes detected: + ~ deployment: null -> {"kind":"BUNDLE","metadata_file_path":"/Workspace/Users/[USERNAME]/.bundle/dms-bind-unbind-[UNIQUE_NAME]/default/state/metadata.json"} + ~ edit_mode: null -> "UI_LOCKED" + ~ format: null -> "MULTI_TASK" + ~ max_concurrent_runs: null -> 1 + ~ name: "bind-target-[UNIQUE_NAME]" -> "foo" + ~ queue: null -> {"enabled":true} + +Error: this bind operation requires user confirmation, but the current console does not support prompting. +To proceed, use --auto-approve after reviewing the plan above. + + +>>> print_requests.py --dms //api/2.0/bundle >>> [CLI] bundle deployment bind foo [JOB_ID] --auto-approve Successfully bound job with an id '[JOB_ID]' Run 'bundle deploy' to deploy changes to your workspace -=== Deploy keeps the bound job ID and enables shadow history +>>> print_requests.py --dms //versions --oneline +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "3"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.foo"}], "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/3/operations/jobs.foo", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-unbind-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"foo\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/3/complete", "body": {"completion_reason": "VERSION_COMPLETE_SUCCESS"}} + +>>> [CLI] jobs get [JOB_ID] +{ + "job_id": [JOB_ID], + "name": "bind-target-[UNIQUE_NAME]" +} +=== Deploy keeps the bound job ID with recorded history >>> [CLI] bundle deploy Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-bind-unbind-[UNIQUE_NAME]/default/files... Updated jobs.foo @@ -22,7 +48,6 @@ Resources: 0 created, 1 changed, 0 deleted, 0 unchanged } === Unbind preserves the shadow history marker and leaves the job intact - >>> [CLI] bundle deployment unbind foo >>> jq {features, state} .databricks/bundle/default/resources.json @@ -39,7 +64,6 @@ Resources: 0 created, 1 changed, 0 deleted, 0 unchanged } === Rebind preserves the existing shadow history marker - >>> [CLI] bundle deployment bind foo [JOB_ID] --auto-approve Successfully bound job with an id '[JOB_ID]' Run 'bundle deploy' to deploy changes to your workspace @@ -52,8 +76,12 @@ Run 'bundle deploy' to deploy changes to your workspace "resource_id": "[JOB_ID]" } -=== A subsequent deploy continues managing the same job +>>> print_requests.py --dms //versions --oneline +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "8"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.foo"}], "previous_version_id": "4", "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/8/operations/jobs.foo", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-unbind-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"foo\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/8/complete", "body": {"completion_reason": "VERSION_COMPLETE_SUCCESS"}} +=== A subsequent deploy continues managing the same job >>> [CLI] bundle deploy Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-bind-unbind-[UNIQUE_NAME]/default/files... Files: 0 uploaded, 0 deleted @@ -66,3 +94,12 @@ Resources: 0 created, 0 changed, 0 deleted, 1 unchanged }, "resource_id": "[JOB_ID]" } + +=== A rejected duplicate bind creates no history version +>>> musterr [CLI] bundle deployment bind foo [JOB_ID] --auto-approve +Error: Resource already managed + +The bundle is already managing resource for resources.jobs.foo and it is bound to the requested ID [JOB_ID]. + + +>>> print_requests.py --dms //versions diff --git a/acceptance/bundle/dms/bind-unbind/script b/acceptance/bundle/dms/bind-unbind/script index 32fab5aa0e8..f52e37e6d07 100644 --- a/acceptance/bundle/dms/bind-unbind/script +++ b/acceptance/bundle/dms/bind-unbind/script @@ -3,9 +3,13 @@ envsubst < databricks.yml.tmpl > databricks.yml title "Bind an existing job with deployment history enabled" job_id=$($CLI jobs create --json '{"name": "bind-target-'$UNIQUE_NAME'"}' | jq -r '.job_id') add_repl "$job_id" JOB_ID +trace musterr $CLI bundle deployment bind foo "$job_id" +trace print_requests.py --dms //api/2.0/bundle trace $CLI bundle deployment bind foo "$job_id" --auto-approve +trace print_requests.py --dms //versions --oneline +trace $CLI jobs get "$job_id" | jq '{job_id, name: .settings.name}' -title "Deploy keeps the bound job ID and enables shadow history" +title "Deploy keeps the bound job ID with recorded history" trace $CLI bundle deploy trace jq '{features, resource_id: .state["resources.jobs.foo"].__id__}' .databricks/bundle/default/resources.json @@ -14,10 +18,20 @@ trace $CLI bundle deployment unbind foo trace jq '{features, state}' .databricks/bundle/default/resources.json trace $CLI jobs get "$job_id" | jq '{job_id}' +rm -f "$OUT_REQUESTS" + title "Rebind preserves the existing shadow history marker" trace $CLI bundle deployment bind foo "$job_id" --auto-approve trace jq '{features, resource_id: .state["resources.jobs.foo"].__id__}' .databricks/bundle/default/resources.json +trace print_requests.py --dms //versions --oneline + title "A subsequent deploy continues managing the same job" trace $CLI bundle deploy trace jq '{features, resource_id: .state["resources.jobs.foo"].__id__}' .databricks/bundle/default/resources.json + +rm -f "$OUT_REQUESTS" + +title "A rejected duplicate bind creates no history version" +trace musterr $CLI bundle deployment bind foo "$job_id" --auto-approve +trace print_requests.py --dms //versions diff --git a/acceptance/bundle/dms/bind-unbind/test.toml b/acceptance/bundle/dms/bind-unbind/test.toml index f620acda97e..353bd89e0a9 100644 --- a/acceptance/bundle/dms/bind-unbind/test.toml +++ b/acceptance/bundle/dms/bind-unbind/test.toml @@ -1,3 +1,2 @@ -RecordRequests = false # Verify shadow history markers and resource IDs across bind, deploy, unbind, and rebind. Cloud = false diff --git a/bundle/phases/bind.go b/bundle/phases/bind.go index 8f19f67a7fa..d612cb6fe8b 100644 --- a/bundle/phases/bind.go +++ b/bundle/phases/bind.go @@ -7,17 +7,23 @@ import ( "fmt" "maps" "slices" + "strings" "github.com/databricks/cli/bundle" "github.com/databricks/cli/bundle/config/engine" "github.com/databricks/cli/bundle/deploy/lock" "github.com/databricks/cli/bundle/deploy/snapshot" "github.com/databricks/cli/bundle/deployplan" + "github.com/databricks/cli/bundle/direct/dstate" + bundleenv "github.com/databricks/cli/bundle/env" "github.com/databricks/cli/bundle/statemgmt" "github.com/databricks/cli/libs/agent" + "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/dms" "github.com/databricks/cli/libs/log" "github.com/databricks/cli/libs/logdiag" + "github.com/databricks/databricks-sdk-go/service/bundledeployments" ) type BindOptions struct { @@ -37,7 +43,14 @@ func Bind(ctx context.Context, b *bundle.Bundle, opts *BindOptions, stateDesc *s return } + historySucceeded := true defer func() { + if b.DeploymentBundle.StateDB.RecordsHistory() { + completionCtx := cmdctx.SetWorkspaceClient(ctx, b.WorkspaceClient(ctx)) + if _, err := b.DeploymentBundle.StateDB.CompleteVersion(completionCtx, historySucceeded && !logdiag.HasError(ctx)); err != nil { + log.Warnf(ctx, "Failed to complete deployment history version: %s", err) + } + } bundle.ApplyContext(ctx, b, lock.Release(lock.GoalBind)) }() @@ -114,6 +127,11 @@ func Bind(ctx context.Context, b *bundle.Bundle, opts *BindOptions, stateDesc *s return } + if err := recordBindHistory(ctx, b, resourceKey); err != nil { + historySucceeded = false + log.Warnf(ctx, "Failed to record bind history: %s", err) + } + statemgmt.PushResourcesState(ctx, b) } @@ -155,3 +173,61 @@ func Unbind(ctx context.Context, b *bundle.Bundle, group, resourceKey string, en statemgmt.PushResourcesState(ctx, b) } + +// recordBindHistory records the committed binding without applying the resource's plan. +func recordBindHistory(ctx context.Context, b *bundle.Bundle, resourceKey string) (err error) { + db := &b.DeploymentBundle.StateDB + _, statePath := b.StateFilenameDirect(ctx) + if err := db.Open(ctx, statePath, dstate.ReadingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{}); err != nil { + return err + } + defer func() { + _, finalizeErr := db.Finalize(ctx) + err = errors.Join(err, finalizeErr) + }() + + var configured *bool + if b.Config.Experimental != nil { + configured = b.Config.Experimental.DeploymentHistory + } + if !db.RecordsHistory() && !bundleenv.RecordsDeploymentHistory(ctx, configured) { + return nil + } + + deploymentID, current, err := LookupDeployment(ctx, b.WorkspaceClient(ctx), b.Config.Workspace.StatePath) + if err != nil { + return err + } + db.DeploymentID = deploymentID + if current != nil { + next, err := dms.NextVersion(current.LastVersionId) + if err != nil { + return err + } + db.VersionID = next - 1 + } + if db.Data.Features == nil { + db.Data.Features = make(map[string]struct{}) + } + db.Data.Features[dstate.FeatureDeploymentHistoryShadow] = struct{}{} + if err := db.UpgradeToWrite(); err != nil { + return err + } + if !cmdctx.HasWorkspaceClient(ctx) { + ctx = cmdctx.SetWorkspaceClient(ctx, b.WorkspaceClient(ctx)) + } + if err := createOrUpdateDeployment(ctx, b, current); err != nil { + return err + } + if err := startVersion(ctx, b, dms.VersionTypeDeploy, []bundledeployments.StagedOperation{{ + ResourceKey: strings.TrimPrefix(resourceKey, dms.StatePrefix), + ActionType: bundledeployments.OperationActionTypeOperationActionTypeBind, + }}); err != nil { + return err + } + entry, ok := db.GetResourceEntry(resourceKey) + if !ok { + return fmt.Errorf("bound resource %s is missing from deployment state", resourceKey) + } + return db.SaveState(ctx, resourceKey, entry.ID, entry.State, entry.DependsOn) +} diff --git a/bundle/phases/dms.go b/bundle/phases/dms.go index 38210913ce8..fd9c117cc25 100644 --- a/bundle/phases/dms.go +++ b/bundle/phases/dms.go @@ -2,6 +2,7 @@ package phases import ( "context" + "errors" "fmt" "net/url" "path" @@ -22,6 +23,7 @@ import ( "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/common/types/fieldmask" "github.com/databricks/databricks-sdk-go/service/bundledeployments" + "github.com/databricks/databricks-sdk-go/service/workspace" ) // stagedOperations lists the resources the plan will touch, for CreateVersion to stage an @@ -292,3 +294,39 @@ func deploymentModeToSDK(mode config.Mode) bundledeployments.DeploymentMode { return "" } } + +// LookupDeployment reads the deployment metadata for a bundle. +// +// Lookup and return the deployment object from ${workspace.state_path}/resources.deployment.json +// +// TODO: a deployment is only usable when both the node and the service's record exist, and a +// half-created one - node present, record missing - blocks the bundle here even though +// CreateDeployment already recovers from it. Move this behind a dms.ReadDeployment(ctx, statePath) +// that returns an empty id and version unless both halves are there, leaving the caller to call +// CreateDeployment to create or finalize it. +// +// TODO: ask the service for a lookup by state path, so this is one round trip rather than two - a +// workspace lookup to turn the node into an id, then a get by that id. +func LookupDeployment(ctx context.Context, w *databricks.WorkspaceClient, statePath string) (string, *bundledeployments.Deployment, error) { + nodePath := path.Join(statePath, dms.DeploymentNodeName) + + obj, err := retry.With(ctx, retry.IsTransientRead, func() (*workspace.ObjectInfo, error) { + return w.Workspace.GetStatusByPath(ctx, nodePath) + }) + if errors.Is(err, apierr.ErrNotFound) || errors.Is(err, apierr.ErrResourceDoesNotExist) { + return "", nil, nil + } + if err != nil { + return "", nil, fmt.Errorf("looking up deployment at %s: %w", nodePath, err) + } + deploymentID := strconv.FormatInt(obj.ObjectId, 10) + deployment, err := retry.With(ctx, retry.IsTransientRead, func() (*bundledeployments.Deployment, error) { + return w.BundleDeployments.GetDeployment(ctx, bundledeployments.GetDeploymentRequest{ + Name: dms.DeploymentName(deploymentID), + }) + }) + if err != nil { + return "", nil, err + } + return deploymentID, deployment, nil +} diff --git a/cmd/bundle/utils/process.go b/cmd/bundle/utils/process.go index 68798a4bb01..9bdad2c6e04 100644 --- a/cmd/bundle/utils/process.go +++ b/cmd/bundle/utils/process.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "path" "path/filepath" "slices" "strconv" @@ -28,18 +27,13 @@ import ( "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/diag" - "github.com/databricks/cli/libs/dms" "github.com/databricks/cli/libs/dyn" "github.com/databricks/cli/libs/log" "github.com/databricks/cli/libs/logdiag" - "github.com/databricks/cli/libs/retry" "github.com/databricks/cli/libs/sync" "github.com/databricks/cli/libs/telemetry" "github.com/databricks/cli/libs/telemetry/protos" - "github.com/databricks/databricks-sdk-go" - "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/service/bundledeployments" - "github.com/databricks/databricks-sdk-go/service/workspace" "github.com/databricks/databricks-sdk-go/useragent" "github.com/spf13/cobra" "golang.org/x/mod/semver" @@ -411,7 +405,7 @@ func ProcessBundleRet(cmd *cobra.Command, opts ProcessOptions) (b *bundle.Bundle if needDirectState && stateMode == dstate.WritingDeploymentHistoryOnly { var err error - dmsDeploymentID, dmsDeployment, err = fetchDeploymentFromStatePath(ctx, b.WorkspaceClient(ctx), b.Config.Workspace.StatePath) + dmsDeploymentID, dmsDeployment, err = phases.LookupDeployment(ctx, b.WorkspaceClient(ctx), b.Config.Workspace.StatePath) if err != nil { logdiag.LogError(ctx, err) return b, stateDesc, root.ErrAlreadyPrinted @@ -637,40 +631,6 @@ func ValidateEngineSetting(ctx context.Context, b *bundle.Bundle) error { return nil } -// Lookup and return the deployment object from ${workspace.state_path}/resources.deployment.json -// -// TODO: a deployment is only usable when both the node and the service's record exist, and a -// half-created one - node present, record missing - blocks the bundle here even though -// CreateDeployment already recovers from it. Move this behind a dms.ReadDeployment(ctx, statePath) -// that returns an empty id and version unless both halves are there, leaving the caller to call -// CreateDeployment to create or finalize it. -// -// TODO: ask the service for a lookup by state path, so this is one round trip rather than two - a -// workspace lookup to turn the node into an id, then a get by that id. -func fetchDeploymentFromStatePath(ctx context.Context, w *databricks.WorkspaceClient, statePath string) (string, *bundledeployments.Deployment, error) { - nodePath := path.Join(statePath, dms.DeploymentNodeName) - - obj, err := retry.With(ctx, retry.IsTransientRead, func() (*workspace.ObjectInfo, error) { - return w.Workspace.GetStatusByPath(ctx, nodePath) - }) - if errors.Is(err, apierr.ErrNotFound) || errors.Is(err, apierr.ErrResourceDoesNotExist) { - return "", nil, nil - } - if err != nil { - return "", nil, fmt.Errorf("looking up deployment at %s: %w", nodePath, err) - } - deploymentID := strconv.FormatInt(obj.ObjectId, 10) - deployment, err := retry.With(ctx, retry.IsTransientRead, func() (*bundledeployments.Deployment, error) { - return w.BundleDeployments.GetDeployment(ctx, bundledeployments.GetDeploymentRequest{ - Name: dms.DeploymentName(deploymentID), - }) - }) - if err != nil { - return "", nil, err - } - return deploymentID, deployment, 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) { @@ -696,7 +656,7 @@ func OpenDirectStateForRead(ctx context.Context, b *bundle.Bundle, stateDesc *st return nil } - dmsDeploymentID, dmsDeployment, err := fetchDeploymentFromStatePath(ctx, b.WorkspaceClient(ctx), b.Config.Workspace.StatePath) + dmsDeploymentID, dmsDeployment, err := phases.LookupDeployment(ctx, b.WorkspaceClient(ctx), b.Config.Workspace.StatePath) if err != nil { return err } @@ -716,7 +676,7 @@ func OpenDirectStateForRead(ctx context.Context, b *bundle.Bundle, stateDesc *st // prepareDeploymentHistoryShadow reads version metadata, never resource state, from DMS. func prepareDeploymentHistoryShadow(ctx context.Context, b *bundle.Bundle) (*bundledeployments.Deployment, error) { - id, deployment, err := fetchDeploymentFromStatePath(ctx, b.WorkspaceClient(ctx), b.Config.Workspace.StatePath) + id, deployment, err := phases.LookupDeployment(ctx, b.WorkspaceClient(ctx), b.Config.Workspace.StatePath) if err != nil { return nil, err } From 5cc96b169e14101a0b59ead661e5a8c13841e48f Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Nov 2026 09:01:10 +0000 Subject: [PATCH 02/29] Move shared deployment lookup into DMS library --- bundle/phases/bind.go | 2 +- bundle/phases/dms.go | 38 -------------------------------- cmd/bundle/utils/process.go | 7 +++--- libs/dms/metadata.go | 43 +++++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 42 deletions(-) diff --git a/bundle/phases/bind.go b/bundle/phases/bind.go index d612cb6fe8b..341b55a7a87 100644 --- a/bundle/phases/bind.go +++ b/bundle/phases/bind.go @@ -194,7 +194,7 @@ func recordBindHistory(ctx context.Context, b *bundle.Bundle, resourceKey string return nil } - deploymentID, current, err := LookupDeployment(ctx, b.WorkspaceClient(ctx), b.Config.Workspace.StatePath) + deploymentID, current, err := dms.LookupDeployment(ctx, b.WorkspaceClient(ctx), b.Config.Workspace.StatePath) if err != nil { return err } diff --git a/bundle/phases/dms.go b/bundle/phases/dms.go index fd9c117cc25..38210913ce8 100644 --- a/bundle/phases/dms.go +++ b/bundle/phases/dms.go @@ -2,7 +2,6 @@ package phases import ( "context" - "errors" "fmt" "net/url" "path" @@ -23,7 +22,6 @@ import ( "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/common/types/fieldmask" "github.com/databricks/databricks-sdk-go/service/bundledeployments" - "github.com/databricks/databricks-sdk-go/service/workspace" ) // stagedOperations lists the resources the plan will touch, for CreateVersion to stage an @@ -294,39 +292,3 @@ func deploymentModeToSDK(mode config.Mode) bundledeployments.DeploymentMode { return "" } } - -// LookupDeployment reads the deployment metadata for a bundle. -// -// Lookup and return the deployment object from ${workspace.state_path}/resources.deployment.json -// -// TODO: a deployment is only usable when both the node and the service's record exist, and a -// half-created one - node present, record missing - blocks the bundle here even though -// CreateDeployment already recovers from it. Move this behind a dms.ReadDeployment(ctx, statePath) -// that returns an empty id and version unless both halves are there, leaving the caller to call -// CreateDeployment to create or finalize it. -// -// TODO: ask the service for a lookup by state path, so this is one round trip rather than two - a -// workspace lookup to turn the node into an id, then a get by that id. -func LookupDeployment(ctx context.Context, w *databricks.WorkspaceClient, statePath string) (string, *bundledeployments.Deployment, error) { - nodePath := path.Join(statePath, dms.DeploymentNodeName) - - obj, err := retry.With(ctx, retry.IsTransientRead, func() (*workspace.ObjectInfo, error) { - return w.Workspace.GetStatusByPath(ctx, nodePath) - }) - if errors.Is(err, apierr.ErrNotFound) || errors.Is(err, apierr.ErrResourceDoesNotExist) { - return "", nil, nil - } - if err != nil { - return "", nil, fmt.Errorf("looking up deployment at %s: %w", nodePath, err) - } - deploymentID := strconv.FormatInt(obj.ObjectId, 10) - deployment, err := retry.With(ctx, retry.IsTransientRead, func() (*bundledeployments.Deployment, error) { - return w.BundleDeployments.GetDeployment(ctx, bundledeployments.GetDeploymentRequest{ - Name: dms.DeploymentName(deploymentID), - }) - }) - if err != nil { - return "", nil, err - } - return deploymentID, deployment, nil -} diff --git a/cmd/bundle/utils/process.go b/cmd/bundle/utils/process.go index 9bdad2c6e04..33d53a1fed1 100644 --- a/cmd/bundle/utils/process.go +++ b/cmd/bundle/utils/process.go @@ -27,6 +27,7 @@ import ( "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/diag" + "github.com/databricks/cli/libs/dms" "github.com/databricks/cli/libs/dyn" "github.com/databricks/cli/libs/log" "github.com/databricks/cli/libs/logdiag" @@ -405,7 +406,7 @@ func ProcessBundleRet(cmd *cobra.Command, opts ProcessOptions) (b *bundle.Bundle if needDirectState && stateMode == dstate.WritingDeploymentHistoryOnly { var err error - dmsDeploymentID, dmsDeployment, err = phases.LookupDeployment(ctx, b.WorkspaceClient(ctx), b.Config.Workspace.StatePath) + dmsDeploymentID, dmsDeployment, err = dms.LookupDeployment(ctx, b.WorkspaceClient(ctx), b.Config.Workspace.StatePath) if err != nil { logdiag.LogError(ctx, err) return b, stateDesc, root.ErrAlreadyPrinted @@ -656,7 +657,7 @@ func OpenDirectStateForRead(ctx context.Context, b *bundle.Bundle, stateDesc *st return nil } - dmsDeploymentID, dmsDeployment, err := phases.LookupDeployment(ctx, b.WorkspaceClient(ctx), b.Config.Workspace.StatePath) + dmsDeploymentID, dmsDeployment, err := dms.LookupDeployment(ctx, b.WorkspaceClient(ctx), b.Config.Workspace.StatePath) if err != nil { return err } @@ -676,7 +677,7 @@ func OpenDirectStateForRead(ctx context.Context, b *bundle.Bundle, stateDesc *st // prepareDeploymentHistoryShadow reads version metadata, never resource state, from DMS. func prepareDeploymentHistoryShadow(ctx context.Context, b *bundle.Bundle) (*bundledeployments.Deployment, error) { - id, deployment, err := phases.LookupDeployment(ctx, b.WorkspaceClient(ctx), b.Config.Workspace.StatePath) + id, deployment, err := dms.LookupDeployment(ctx, b.WorkspaceClient(ctx), b.Config.Workspace.StatePath) if err != nil { return nil, err } diff --git a/libs/dms/metadata.go b/libs/dms/metadata.go index f9e23a67db0..3a5dc5158d7 100644 --- a/libs/dms/metadata.go +++ b/libs/dms/metadata.go @@ -1,12 +1,19 @@ package dms import ( + "context" + "errors" "fmt" + "path" "strconv" "strings" + "github.com/databricks/cli/libs/retry" "github.com/databricks/cli/libs/structs/structdiff" + "github.com/databricks/databricks-sdk-go" + "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/service/bundledeployments" + "github.com/databricks/databricks-sdk-go/service/workspace" ) // VersionType identifies the kind of deployment a version records. @@ -132,3 +139,39 @@ func DeploymentUpdate(metadata Metadata, mask string) bundledeployments.Deployme } return dep } + +// LookupDeployment reads the deployment metadata for a bundle. +// +// Lookup and return the deployment object from ${workspace.state_path}/resources.deployment.json +// +// TODO: a deployment is only usable when both the node and the service's record exist, and a +// half-created one - node present, record missing - blocks the bundle here even though +// CreateDeployment already recovers from it. Move this behind a dms.ReadDeployment(ctx, statePath) +// that returns an empty id and version unless both halves are there, leaving the caller to call +// CreateDeployment to create or finalize it. +// +// TODO: ask the service for a lookup by state path, so this is one round trip rather than two - a +// workspace lookup to turn the node into an id, then a get by that id. +func LookupDeployment(ctx context.Context, w *databricks.WorkspaceClient, statePath string) (string, *bundledeployments.Deployment, error) { + nodePath := path.Join(statePath, DeploymentNodeName) + + obj, err := retry.With(ctx, retry.IsTransientRead, func() (*workspace.ObjectInfo, error) { + return w.Workspace.GetStatusByPath(ctx, nodePath) + }) + if errors.Is(err, apierr.ErrNotFound) || errors.Is(err, apierr.ErrResourceDoesNotExist) { + return "", nil, nil + } + if err != nil { + return "", nil, fmt.Errorf("looking up deployment at %s: %w", nodePath, err) + } + deploymentID := strconv.FormatInt(obj.ObjectId, 10) + deployment, err := retry.With(ctx, retry.IsTransientRead, func() (*bundledeployments.Deployment, error) { + return w.BundleDeployments.GetDeployment(ctx, bundledeployments.GetDeploymentRequest{ + Name: DeploymentName(deploymentID), + }) + }) + if err != nil { + return "", nil, err + } + return deploymentID, deployment, nil +} From a1f0527c9f463a84796b3a04b9ef78310c93b936 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Nov 2026 09:14:04 +0000 Subject: [PATCH 03/29] Address bind history self-review findings --- .nextchanges/bundles/record-bind-history.md | 2 +- ...{out.bind.false.txt => out.bind.plain.txt} | 0 ...ut.bind.true.txt => out.bind.recorded.txt} | 0 .../deployment/bind/feature/out.test.toml | 3 +- .../bundle/deployment/bind/feature/script | 2 +- .../bundle/deployment/bind/feature/test.toml | 8 +++- .../dms/bind-immutable/databricks.yml.tmpl | 9 ++++ .../bundle/dms/bind-immutable/extra.yml | 4 ++ .../bundle/dms/bind-immutable/out.test.toml | 2 + .../bundle/dms/bind-immutable/output.txt | 35 ++++++++++++++ acceptance/bundle/dms/bind-immutable/script | 21 +++++++++ .../bundle/dms/bind-immutable/test.toml | 1 + .../bundle/dms/bind-unbind/out.test.toml | 2 +- acceptance/bundle/dms/bind-unbind/output.txt | 9 ++++ acceptance/bundle/dms/bind-unbind/script | 9 ++++ acceptance/bundle/dms/bind-unbind/test.toml | 2 +- bundle/phases/bind.go | 22 +++++---- bundle/phases/dms.go | 47 +++++++++++++++---- libs/dms/metadata.go | 3 +- 19 files changed, 156 insertions(+), 25 deletions(-) rename acceptance/bundle/deployment/bind/feature/{out.bind.false.txt => out.bind.plain.txt} (100%) rename acceptance/bundle/deployment/bind/feature/{out.bind.true.txt => out.bind.recorded.txt} (100%) create mode 100644 acceptance/bundle/dms/bind-immutable/databricks.yml.tmpl create mode 100644 acceptance/bundle/dms/bind-immutable/extra.yml create mode 100644 acceptance/bundle/dms/bind-immutable/out.test.toml create mode 100644 acceptance/bundle/dms/bind-immutable/output.txt create mode 100644 acceptance/bundle/dms/bind-immutable/script create mode 100644 acceptance/bundle/dms/bind-immutable/test.toml diff --git a/.nextchanges/bundles/record-bind-history.md b/.nextchanges/bundles/record-bind-history.md index 7fce080e9fc..f1ae02d88be 100644 --- a/.nextchanges/bundles/record-bind-history.md +++ b/.nextchanges/bundles/record-bind-history.md @@ -1 +1 @@ -* Record deployment history when binding existing resources with `bundle deployment bind`. ([#7400](https://github.com/databricks/cli/pull/7400)) +* Record deployment history when binding existing resources with `bundle deployment bind` when deployment history is enabled. ([#7400](https://github.com/databricks/cli/pull/7400)) diff --git a/acceptance/bundle/deployment/bind/feature/out.bind.false.txt b/acceptance/bundle/deployment/bind/feature/out.bind.plain.txt similarity index 100% rename from acceptance/bundle/deployment/bind/feature/out.bind.false.txt rename to acceptance/bundle/deployment/bind/feature/out.bind.plain.txt diff --git a/acceptance/bundle/deployment/bind/feature/out.bind.true.txt b/acceptance/bundle/deployment/bind/feature/out.bind.recorded.txt similarity index 100% rename from acceptance/bundle/deployment/bind/feature/out.bind.true.txt rename to acceptance/bundle/deployment/bind/feature/out.bind.recorded.txt diff --git a/acceptance/bundle/deployment/bind/feature/out.test.toml b/acceptance/bundle/deployment/bind/feature/out.test.toml index 943f1dc5575..8a45520420b 100644 --- a/acceptance/bundle/deployment/bind/feature/out.test.toml +++ b/acceptance/bundle/deployment/bind/feature/out.test.toml @@ -1,2 +1,3 @@ Cloud = false -EnvMatrix.DMS = ["false", "true"] +EnvMatrix.DMS = ["", "true"] +EnvMatrix.OUTPUT_MODE = ["plain", "recorded"] diff --git a/acceptance/bundle/deployment/bind/feature/script b/acceptance/bundle/deployment/bind/feature/script index debd1951c68..ed49b8938a5 100644 --- a/acceptance/bundle/deployment/bind/feature/script +++ b/acceptance/bundle/deployment/bind/feature/script @@ -1,4 +1,4 @@ -trace $CLI bundle deployment bind feature1 main.myschema.boundfeature --auto-approve 2> out.bind.$DMS.txt +trace $CLI bundle deployment bind feature1 main.myschema.boundfeature --auto-approve 2> out.bind.$OUTPUT_MODE.txt trace $CLI bundle summary trace $CLI bundle deployment unbind feature1 diff --git a/acceptance/bundle/deployment/bind/feature/test.toml b/acceptance/bundle/deployment/bind/feature/test.toml index 5bb3b67ab85..97da8412e45 100644 --- a/acceptance/bundle/deployment/bind/feature/test.toml +++ b/acceptance/bundle/deployment/bind/feature/test.toml @@ -1,5 +1,9 @@ -EnvMatrix.DMS = ["false", "true"] -EnvVaryOutput = "DMS" +# Keep CI's DMS= variant while using non-empty labels for per-variant output files. +EnvMatrix.OUTPUT_MODE = ["plain", "recorded"] +EnvMatrixExclude.plain_with_history = ["DMS=true", "OUTPUT_MODE=plain"] +EnvMatrixExclude.recorded_without_history = ["DMS=", "OUTPUT_MODE=recorded"] +EnvVaryOutput = "OUTPUT_MODE" +EnvRepl.OUTPUT_MODE = false Cloud = false Ignore = [ diff --git a/acceptance/bundle/dms/bind-immutable/databricks.yml.tmpl b/acceptance/bundle/dms/bind-immutable/databricks.yml.tmpl new file mode 100644 index 00000000000..300a5d3a8e6 --- /dev/null +++ b/acceptance/bundle/dms/bind-immutable/databricks.yml.tmpl @@ -0,0 +1,9 @@ +bundle: + name: dms-bind-immutable-$UNIQUE_NAME +include: [] +workspace: + sync_mode: immutable +resources: + jobs: + foo: + name: foo diff --git a/acceptance/bundle/dms/bind-immutable/extra.yml b/acceptance/bundle/dms/bind-immutable/extra.yml new file mode 100644 index 00000000000..cd35a4be7e8 --- /dev/null +++ b/acceptance/bundle/dms/bind-immutable/extra.yml @@ -0,0 +1,4 @@ +resources: + jobs: + bar: + name: bar diff --git a/acceptance/bundle/dms/bind-immutable/out.test.toml b/acceptance/bundle/dms/bind-immutable/out.test.toml new file mode 100644 index 00000000000..88e6fbd847c --- /dev/null +++ b/acceptance/bundle/dms/bind-immutable/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +EnvMatrix.DMS = ["true"] diff --git a/acceptance/bundle/dms/bind-immutable/output.txt b/acceptance/bundle/dms/bind-immutable/output.txt new file mode 100644 index 00000000000..debd86616cf --- /dev/null +++ b/acceptance/bundle/dms/bind-immutable/output.txt @@ -0,0 +1,35 @@ + +=== An immutable bundle can record its first bind before any snapshot exists +>>> [CLI] bundle deployment bind foo [JOB_ID] --auto-approve +Successfully bound job with an id '[JOB_ID]' +Run 'bundle deploy' to deploy changes to your workspace + +>>> print_requests.py --dms //versions --oneline +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "3"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.foo"}], "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/3/operations/jobs.foo", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-immutable-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"foo\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/3/complete", "body": {"completion_reason": "VERSION_COMPLETE_SUCCESS"}} + +=== Deploy creates the immutable snapshot +>>> [CLI] bundle deploy +Created internal_immutable_snapshots.immutable +Updated jobs.foo +Files: 0 uploaded, 0 deleted +Resources: 1 created, 1 changed, 0 deleted, 0 unchanged + +=== Binding another resource preserves the last deployed snapshot metadata +>>> update_file.py databricks.yml include: [] include: [extra.yml] + +>>> [CLI] bundle deployment bind bar [JOB_ID] --auto-approve +Successfully bound job with an id '[JOB_ID]' +Run 'bundle deploy' to deploy changes to your workspace + +>>> print_requests.py --dms //api/2.0/bundle --oneline +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "7"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.bar"}], "previous_version_id": "4", "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/7/operations/jobs.bar", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-immutable-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"bar\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/7/complete", "body": {"completion_reason": "VERSION_COMPLETE_SUCCESS"}} + +>>> MSYS_NO_PATHCONV=1 [CLI] api get /api/2.0/bundle/deployments/[DEPLOYMENT_ID] +{ + "file_path": "[DEPLOYED_FILE_PATH]", + "preserved": true +} diff --git a/acceptance/bundle/dms/bind-immutable/script b/acceptance/bundle/dms/bind-immutable/script new file mode 100644 index 00000000000..cfb1102a4fc --- /dev/null +++ b/acceptance/bundle/dms/bind-immutable/script @@ -0,0 +1,21 @@ +envsubst < databricks.yml.tmpl > databricks.yml +job_id=$($CLI jobs create --json '{"name": "bind-target-'$UNIQUE_NAME'"}' | jq -r '.job_id') +add_repl "$job_id" JOB_ID + +title "An immutable bundle can record its first bind before any snapshot exists" +trace $CLI bundle deployment bind foo "$job_id" --auto-approve +trace print_requests.py --dms //versions --oneline | contains.py OPERATION_ACTION_TYPE_BIND VERSION_COMPLETE_SUCCESS + +title "Deploy creates the immutable snapshot" +trace $CLI bundle deploy +deployment_id=$(MSYS_NO_PATHCONV=1 $CLI workspace get-status "/Workspace/Users/${CURRENT_USER_NAME}/.bundle/dms-bind-immutable-${UNIQUE_NAME}/default/state/resources.deployment.json" -o json | jq -r '.object_id') +file_path=$(MSYS_NO_PATHCONV=1 $CLI api get "/api/2.0/bundle/deployments/${deployment_id}" | jq -r '.workspace_info.file_path') +add_repl "$file_path" DEPLOYED_FILE_PATH +rm -f "$OUT_REQUESTS" + +title "Binding another resource preserves the last deployed snapshot metadata" +trace update_file.py databricks.yml 'include: []' 'include: [extra.yml]' +trace $CLI bundle deployment bind bar "$job_id" --auto-approve +trace print_requests.py --dms //api/2.0/bundle --oneline | contains.py OPERATION_ACTION_TYPE_BIND VERSION_COMPLETE_SUCCESS +trace MSYS_NO_PATHCONV=1 $CLI api get "/api/2.0/bundle/deployments/${deployment_id}" | jq --arg expected "$file_path" '{file_path: .workspace_info.file_path, preserved: (.workspace_info.file_path == $expected)}' +rm -f "$OUT_REQUESTS" diff --git a/acceptance/bundle/dms/bind-immutable/test.toml b/acceptance/bundle/dms/bind-immutable/test.toml new file mode 100644 index 00000000000..b7e0c59ed1b --- /dev/null +++ b/acceptance/bundle/dms/bind-immutable/test.toml @@ -0,0 +1 @@ +Cloud = false # Assert history metadata without requiring snapshot permissions on a real workspace. diff --git a/acceptance/bundle/dms/bind-unbind/out.test.toml b/acceptance/bundle/dms/bind-unbind/out.test.toml index 88e6fbd847c..84b22ba6735 100644 --- a/acceptance/bundle/dms/bind-unbind/out.test.toml +++ b/acceptance/bundle/dms/bind-unbind/out.test.toml @@ -1,2 +1,2 @@ -Cloud = false +Cloud = true EnvMatrix.DMS = ["true"] diff --git a/acceptance/bundle/dms/bind-unbind/output.txt b/acceptance/bundle/dms/bind-unbind/output.txt index a5fa3b20dbe..e07ca28f859 100644 --- a/acceptance/bundle/dms/bind-unbind/output.txt +++ b/acceptance/bundle/dms/bind-unbind/output.txt @@ -32,6 +32,15 @@ Run 'bundle deploy' to deploy changes to your workspace "name": "bind-target-[UNIQUE_NAME]" } +>>> MSYS_NO_PATHCONV=1 [CLI] bundle-deployments list-resources deployments/[DEPLOYMENT_ID] -o json +[ + { + "last_action_type": "OPERATION_ACTION_TYPE_BIND", + "resource_id": "[JOB_ID]", + "resource_key": "jobs.foo" + } +] + === Deploy keeps the bound job ID with recorded history >>> [CLI] bundle deploy Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-bind-unbind-[UNIQUE_NAME]/default/files... diff --git a/acceptance/bundle/dms/bind-unbind/script b/acceptance/bundle/dms/bind-unbind/script index f52e37e6d07..36c97bf8c3d 100644 --- a/acceptance/bundle/dms/bind-unbind/script +++ b/acceptance/bundle/dms/bind-unbind/script @@ -3,11 +3,20 @@ envsubst < databricks.yml.tmpl > databricks.yml title "Bind an existing job with deployment history enabled" job_id=$($CLI jobs create --json '{"name": "bind-target-'$UNIQUE_NAME'"}' | jq -r '.job_id') add_repl "$job_id" JOB_ID +cleanup_bind_target() { + errcode $CLI bundle destroy --auto-approve >> LOG.cleanup 2>&1 + errcode $CLI jobs delete "$job_id" >> LOG.cleanup 2>&1 +} +trap cleanup_bind_target EXIT + trace musterr $CLI bundle deployment bind foo "$job_id" trace print_requests.py --dms //api/2.0/bundle trace $CLI bundle deployment bind foo "$job_id" --auto-approve trace print_requests.py --dms //versions --oneline trace $CLI jobs get "$job_id" | jq '{job_id, name: .settings.name}' +deployment_id=$(MSYS_NO_PATHCONV=1 $CLI workspace get-status "/Workspace/Users/${CURRENT_USER_NAME}/.bundle/dms-bind-unbind-${UNIQUE_NAME}/default/state/resources.deployment.json" -o json | jq -r '.object_id') +trace MSYS_NO_PATHCONV=1 $CLI bundle-deployments list-resources "deployments/${deployment_id}" -o json | jq -S 'map({resource_key, resource_id, last_action_type})' + title "Deploy keeps the bound job ID with recorded history" trace $CLI bundle deploy diff --git a/acceptance/bundle/dms/bind-unbind/test.toml b/acceptance/bundle/dms/bind-unbind/test.toml index 353bd89e0a9..b7cf7fdd9a0 100644 --- a/acceptance/bundle/dms/bind-unbind/test.toml +++ b/acceptance/bundle/dms/bind-unbind/test.toml @@ -1,2 +1,2 @@ # Verify shadow history markers and resource IDs across bind, deploy, unbind, and rebind. -Cloud = false +Cloud = true diff --git a/bundle/phases/bind.go b/bundle/phases/bind.go index 341b55a7a87..0bb079e5fb8 100644 --- a/bundle/phases/bind.go +++ b/bundle/phases/bind.go @@ -45,12 +45,7 @@ func Bind(ctx context.Context, b *bundle.Bundle, opts *BindOptions, stateDesc *s historySucceeded := true defer func() { - if b.DeploymentBundle.StateDB.RecordsHistory() { - completionCtx := cmdctx.SetWorkspaceClient(ctx, b.WorkspaceClient(ctx)) - if _, err := b.DeploymentBundle.StateDB.CompleteVersion(completionCtx, historySucceeded && !logdiag.HasError(ctx)); err != nil { - log.Warnf(ctx, "Failed to complete deployment history version: %s", err) - } - } + completeBindingHistory(ctx, b, historySucceeded && !logdiag.HasError(ctx)) bundle.ApplyContext(ctx, b, lock.Release(lock.GoalBind)) }() @@ -135,6 +130,17 @@ func Bind(ctx context.Context, b *bundle.Bundle, opts *BindOptions, stateDesc *s statemgmt.PushResourcesState(ctx, b) } +// completeBindingHistory completes the recorded version after the remote state upload. +func completeBindingHistory(ctx context.Context, b *bundle.Bundle, success bool) { + if !b.DeploymentBundle.StateDB.RecordsHistory() { + return + } + completionCtx := cmdctx.SetWorkspaceClient(ctx, b.WorkspaceClient(ctx)) + if _, err := b.DeploymentBundle.StateDB.CompleteVersion(completionCtx, success); err != nil { + log.Warnf(ctx, "Failed to complete deployment history version: %s", err) + } +} + func jsonDump(ctx context.Context, v any, field string) string { b, err := json.Marshal(v) if err != nil { @@ -174,7 +180,7 @@ func Unbind(ctx context.Context, b *bundle.Bundle, group, resourceKey string, en statemgmt.PushResourcesState(ctx, b) } -// recordBindHistory records the committed binding without applying the resource's plan. +// recordBindHistory records a committed binding's saved state when history recording is enabled. func recordBindHistory(ctx context.Context, b *bundle.Bundle, resourceKey string) (err error) { db := &b.DeploymentBundle.StateDB _, statePath := b.StateFilenameDirect(ctx) @@ -216,7 +222,7 @@ func recordBindHistory(ctx context.Context, b *bundle.Bundle, resourceKey string if !cmdctx.HasWorkspaceClient(ctx) { ctx = cmdctx.SetWorkspaceClient(ctx, b.WorkspaceClient(ctx)) } - if err := createOrUpdateDeployment(ctx, b, current); err != nil { + if err := createBindingDeployment(ctx, b, current); err != nil { return err } if err := startVersion(ctx, b, dms.VersionTypeDeploy, []bundledeployments.StagedOperation{{ diff --git a/bundle/phases/dms.go b/bundle/phases/dms.go index 38210913ce8..f70f249c079 100644 --- a/bundle/phases/dms.go +++ b/bundle/phases/dms.go @@ -81,16 +81,9 @@ func createOrUpdateDeployment(ctx context.Context, b *bundle.Bundle, current *bu } deploymentID := db.StateDB.DeploymentID if deploymentID == "" { - dep := metadata.Deployment() - dep.InitialParentPath = b.Config.Workspace.StatePath - created, err := w.BundleDeployments.CreateDeployment(ctx, bundledeployments.CreateDeploymentRequest{Deployment: dep}) + deploymentID, err = createDeployment(ctx, b, metadata) if err != nil { - return fmt.Errorf("failed to create deployment: %w", err) - } - // The id is the workspace node the server makes under the parent path. - deploymentID, err = dms.DeploymentIDFromName(created.Name) - if err != nil { - return fmt.Errorf("failed to create deployment: %w", err) + return err } db.StateDB.DeploymentID = deploymentID } else if mask := metadata.StaleFields(current); mask != "" { @@ -113,6 +106,42 @@ func createOrUpdateDeployment(ctx context.Context, b *bundle.Bundle, current *bu return nil } +// createDeployment registers metadata under the bundle's state path and returns its server-minted ID. +func createDeployment(ctx context.Context, b *bundle.Bundle, metadata dms.Metadata) (string, error) { + dep := metadata.Deployment() + dep.InitialParentPath = b.Config.Workspace.StatePath + created, err := b.WorkspaceClient(ctx).BundleDeployments.CreateDeployment(ctx, bundledeployments.CreateDeploymentRequest{Deployment: dep}) + if err != nil { + return "", fmt.Errorf("failed to create deployment: %w", err) + } + // The id is the workspace node the server makes under the parent path. + deploymentID, err := dms.DeploymentIDFromName(created.Name) + if err != nil { + return "", fmt.Errorf("failed to create deployment: %w", err) + } + return deploymentID, nil +} + +// createBindingDeployment registers a first binding without changing metadata from an existing deploy. +func createBindingDeployment(ctx context.Context, b *bundle.Bundle, current *bundledeployments.Deployment) error { + if current != nil { + return nil + } + // Bind does not upload files or stage an immutable snapshot, so it cannot report a deployed file path. + metadata := 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}, + } + deploymentID, err := createDeployment(ctx, b, metadata) + if err != nil { + return err + } + b.DeploymentBundle.StateDB.DeploymentID = deploymentID + return nil +} + // deleteDeployment deletes the deployment record. A retry that finds it missing succeeds: the // earlier attempt may have deleted it before its response was lost. func deleteDeployment(ctx context.Context, w *databricks.WorkspaceClient, deploymentID string) error { diff --git a/libs/dms/metadata.go b/libs/dms/metadata.go index 3a5dc5158d7..240c85a35c3 100644 --- a/libs/dms/metadata.go +++ b/libs/dms/metadata.go @@ -140,7 +140,8 @@ func DeploymentUpdate(metadata Metadata, mask string) bundledeployments.Deployme return dep } -// LookupDeployment reads the deployment metadata for a bundle. +// LookupDeployment resolves the deployment node under statePath and reads its DMS record. +// A missing workspace node returns no deployment. // // Lookup and return the deployment object from ${workspace.state_path}/resources.deployment.json // From f0ad92309067b0d0a89deeec88aa615bb31edea5 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Nov 2026 10:05:12 +0000 Subject: [PATCH 04/29] Keep binding registration separate from existing deploy code --- bundle/phases/dms.go | 47 +++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/bundle/phases/dms.go b/bundle/phases/dms.go index f70f249c079..53407c843f5 100644 --- a/bundle/phases/dms.go +++ b/bundle/phases/dms.go @@ -81,9 +81,16 @@ func createOrUpdateDeployment(ctx context.Context, b *bundle.Bundle, current *bu } deploymentID := db.StateDB.DeploymentID if deploymentID == "" { - deploymentID, err = createDeployment(ctx, b, metadata) + dep := metadata.Deployment() + dep.InitialParentPath = b.Config.Workspace.StatePath + created, err := w.BundleDeployments.CreateDeployment(ctx, bundledeployments.CreateDeploymentRequest{Deployment: dep}) if err != nil { - return err + return fmt.Errorf("failed to create deployment: %w", err) + } + // The id is the workspace node the server makes under the parent path. + deploymentID, err = dms.DeploymentIDFromName(created.Name) + if err != nil { + return fmt.Errorf("failed to create deployment: %w", err) } db.StateDB.DeploymentID = deploymentID } else if mask := metadata.StaleFields(current); mask != "" { @@ -106,37 +113,27 @@ func createOrUpdateDeployment(ctx context.Context, b *bundle.Bundle, current *bu return nil } -// createDeployment registers metadata under the bundle's state path and returns its server-minted ID. -func createDeployment(ctx context.Context, b *bundle.Bundle, metadata dms.Metadata) (string, error) { - dep := metadata.Deployment() - dep.InitialParentPath = b.Config.Workspace.StatePath - created, err := b.WorkspaceClient(ctx).BundleDeployments.CreateDeployment(ctx, bundledeployments.CreateDeploymentRequest{Deployment: dep}) - if err != nil { - return "", fmt.Errorf("failed to create deployment: %w", err) - } - // The id is the workspace node the server makes under the parent path. - deploymentID, err := dms.DeploymentIDFromName(created.Name) - if err != nil { - return "", fmt.Errorf("failed to create deployment: %w", err) - } - return deploymentID, nil -} - // createBindingDeployment registers a first binding without changing metadata from an existing deploy. func createBindingDeployment(ctx context.Context, b *bundle.Bundle, current *bundledeployments.Deployment) error { if current != nil { return nil } // Bind does not upload files or stage an immutable snapshot, so it cannot report a deployed file path. - metadata := 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}, + created, err := b.WorkspaceClient(ctx).BundleDeployments.CreateDeployment(ctx, bundledeployments.CreateDeploymentRequest{ + Deployment: bundledeployments.Deployment{ + DisplayName: b.Config.Bundle.Name, + TargetName: b.Config.Bundle.Target, + DeploymentMode: deploymentModeToSDK(b.Config.Bundle.Mode), + WorkspaceInfo: &bundledeployments.WorkspaceInfo{RootPath: b.Config.Workspace.RootPath}, + InitialParentPath: b.Config.Workspace.StatePath, + }, + }) + if err != nil { + return fmt.Errorf("failed to create deployment: %w", err) } - deploymentID, err := createDeployment(ctx, b, metadata) + deploymentID, err := dms.DeploymentIDFromName(created.Name) if err != nil { - return err + return fmt.Errorf("failed to create deployment: %w", err) } b.DeploymentBundle.StateDB.DeploymentID = deploymentID return nil From 786a2bd283b055da59f44cf38ce203d01d7b5316 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Nov 2026 10:22:10 +0000 Subject: [PATCH 05/29] Move binding deployment registration into bind phase --- bundle/phases/bind.go | 26 ++++++++++++++++++++++++++ bundle/phases/dms.go | 26 -------------------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/bundle/phases/bind.go b/bundle/phases/bind.go index 0bb079e5fb8..0aee639b437 100644 --- a/bundle/phases/bind.go +++ b/bundle/phases/bind.go @@ -34,6 +34,32 @@ type BindOptions struct { ResourceId string } +// createBindingDeployment registers a first binding without changing metadata from an existing deploy. +func createBindingDeployment(ctx context.Context, b *bundle.Bundle, current *bundledeployments.Deployment) error { + if current != nil { + return nil + } + // Bind does not upload files or stage an immutable snapshot, so it cannot report a deployed file path. + created, err := b.WorkspaceClient(ctx).BundleDeployments.CreateDeployment(ctx, bundledeployments.CreateDeploymentRequest{ + Deployment: bundledeployments.Deployment{ + DisplayName: b.Config.Bundle.Name, + TargetName: b.Config.Bundle.Target, + DeploymentMode: deploymentModeToSDK(b.Config.Bundle.Mode), + WorkspaceInfo: &bundledeployments.WorkspaceInfo{RootPath: b.Config.Workspace.RootPath}, + InitialParentPath: b.Config.Workspace.StatePath, + }, + }) + if err != nil { + return fmt.Errorf("failed to create deployment: %w", err) + } + deploymentID, err := dms.DeploymentIDFromName(created.Name) + if err != nil { + return fmt.Errorf("failed to create deployment: %w", err) + } + b.DeploymentBundle.StateDB.DeploymentID = deploymentID + return nil +} + func Bind(ctx context.Context, b *bundle.Bundle, opts *BindOptions, stateDesc *statemgmt.StateDesc) { log.Info(ctx, "Phase: bind") engine := stateDesc.Engine diff --git a/bundle/phases/dms.go b/bundle/phases/dms.go index 53407c843f5..38210913ce8 100644 --- a/bundle/phases/dms.go +++ b/bundle/phases/dms.go @@ -113,32 +113,6 @@ func createOrUpdateDeployment(ctx context.Context, b *bundle.Bundle, current *bu return nil } -// createBindingDeployment registers a first binding without changing metadata from an existing deploy. -func createBindingDeployment(ctx context.Context, b *bundle.Bundle, current *bundledeployments.Deployment) error { - if current != nil { - return nil - } - // Bind does not upload files or stage an immutable snapshot, so it cannot report a deployed file path. - created, err := b.WorkspaceClient(ctx).BundleDeployments.CreateDeployment(ctx, bundledeployments.CreateDeploymentRequest{ - Deployment: bundledeployments.Deployment{ - DisplayName: b.Config.Bundle.Name, - TargetName: b.Config.Bundle.Target, - DeploymentMode: deploymentModeToSDK(b.Config.Bundle.Mode), - WorkspaceInfo: &bundledeployments.WorkspaceInfo{RootPath: b.Config.Workspace.RootPath}, - InitialParentPath: b.Config.Workspace.StatePath, - }, - }) - if err != nil { - return fmt.Errorf("failed to create deployment: %w", err) - } - deploymentID, err := dms.DeploymentIDFromName(created.Name) - if err != nil { - return fmt.Errorf("failed to create deployment: %w", err) - } - b.DeploymentBundle.StateDB.DeploymentID = deploymentID - return nil -} - // deleteDeployment deletes the deployment record. A retry that finds it missing succeeds: the // earlier attempt may have deleted it before its response was lost. func deleteDeployment(ctx context.Context, w *databricks.WorkspaceClient, deploymentID string) error { From 11f62f7cecc470533cd6d938f00dcf7aec97ebd9 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Nov 2026 10:50:16 +0000 Subject: [PATCH 06/29] Explain the immutable binding regression test --- acceptance/bundle/dms/bind-immutable/test.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/acceptance/bundle/dms/bind-immutable/test.toml b/acceptance/bundle/dms/bind-immutable/test.toml index b7e0c59ed1b..ee7c307c0a3 100644 --- a/acceptance/bundle/dms/bind-immutable/test.toml +++ b/acceptance/bundle/dms/bind-immutable/test.toml @@ -1 +1,4 @@ +# Dedicated regression: bind previews diffs without staging an immutable snapshot. +# History must still be recorded before a snapshot exists, and preserve the last +# deployed snapshot's file path when one already exists. Cloud = false # Assert history metadata without requiring snapshot permissions on a real workspace. From 43303ca2a8a8297c379d654df3a6873c4eba705c Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Nov 2026 10:52:08 +0000 Subject: [PATCH 07/29] Simplify the immutable binding test comment --- acceptance/bundle/dms/bind-immutable/test.toml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/acceptance/bundle/dms/bind-immutable/test.toml b/acceptance/bundle/dms/bind-immutable/test.toml index ee7c307c0a3..86e459c7c2b 100644 --- a/acceptance/bundle/dms/bind-immutable/test.toml +++ b/acceptance/bundle/dms/bind-immutable/test.toml @@ -1,4 +1,2 @@ -# Dedicated regression: bind previews diffs without staging an immutable snapshot. -# History must still be recorded before a snapshot exists, and preserve the last -# deployed snapshot's file path when one already exists. +# Reusing bundle deploy code breaks immutable snapshots because bind does not upload or stage files. Cloud = false # Assert history metadata without requiring snapshot permissions on a real workspace. From e22b9a1bf4bb887ae15787fe0f9432872a700245 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Nov 2026 10:53:46 +0000 Subject: [PATCH 08/29] Clarify immutable binding test coverage --- acceptance/bundle/dms/bind-immutable/test.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/acceptance/bundle/dms/bind-immutable/test.toml b/acceptance/bundle/dms/bind-immutable/test.toml index 86e459c7c2b..6ed9b96e388 100644 --- a/acceptance/bundle/dms/bind-immutable/test.toml +++ b/acceptance/bundle/dms/bind-immutable/test.toml @@ -1,2 +1,3 @@ -# Reusing bundle deploy code breaks immutable snapshots because bind does not upload or stage files. +# Ensure bind works with immutable folders and deployment history enabled. +# Reusing bundle deploy code fails here because bind does not upload or stage files. Cloud = false # Assert history metadata without requiring snapshot permissions on a real workspace. From fddb933d1cf72cf38e20a9ae986d3cd7c9dfe5e2 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Nov 2026 10:56:42 +0000 Subject: [PATCH 09/29] Explain the observed immutable bind regression --- acceptance/bundle/dms/bind-immutable/test.toml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/acceptance/bundle/dms/bind-immutable/test.toml b/acceptance/bundle/dms/bind-immutable/test.toml index 6ed9b96e388..172266cc6a6 100644 --- a/acceptance/bundle/dms/bind-immutable/test.toml +++ b/acceptance/bundle/dms/bind-immutable/test.toml @@ -1,3 +1,4 @@ -# Ensure bind works with immutable folders and deployment history enabled. -# Reusing bundle deploy code fails here because bind does not upload or stage files. +# We saw bind succeed without recording deployment history for immutable folders. +# It reused deploy code that expected staged files, but bind does not stage files. +# This test prevents that regression. Cloud = false # Assert history metadata without requiring snapshot permissions on a real workspace. From a7fa8ffe5d23d452bd3b4ce603a27b7962bf3b7d Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Nov 2026 10:59:44 +0000 Subject: [PATCH 10/29] Clarify the immutable bind regression assertion --- acceptance/bundle/dms/bind-immutable/test.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/acceptance/bundle/dms/bind-immutable/test.toml b/acceptance/bundle/dms/bind-immutable/test.toml index 172266cc6a6..51e648da9bd 100644 --- a/acceptance/bundle/dms/bind-immutable/test.toml +++ b/acceptance/bundle/dms/bind-immutable/test.toml @@ -1,4 +1,4 @@ # We saw bind succeed without recording deployment history for immutable folders. # It reused deploy code that expected staged files, but bind does not stage files. -# This test prevents that regression. +# This test prevents that regression, ensuring DMS works for bind with immutable folders enabled. Cloud = false # Assert history metadata without requiring snapshot permissions on a real workspace. From f268f40fdcfc889626e8519e4da8b3b0d18e0dde Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Nov 2026 12:31:38 +0000 Subject: [PATCH 11/29] Use requested wording for immutable bind test comment --- acceptance/bundle/dms/bind-immutable/test.toml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/acceptance/bundle/dms/bind-immutable/test.toml b/acceptance/bundle/dms/bind-immutable/test.toml index 51e648da9bd..ee99e628c60 100644 --- a/acceptance/bundle/dms/bind-immutable/test.toml +++ b/acceptance/bundle/dms/bind-immutable/test.toml @@ -1,4 +1,4 @@ -# We saw bind succeed without recording deployment history for immutable folders. -# It reused deploy code that expected staged files, but bind does not stage files. -# This test prevents that regression, ensuring DMS works for bind with immutable folders enabled. -Cloud = false # Assert history metadata without requiring snapshot permissions on a real workspace. +# This test ensures that deployment history works with bind and immutable folders. This case is interesting +# because by default we record the snapshot path in deploy, but that is not available during bind. So it's important +# to ensure that we don't do that during deployment bind, and recording history continues to work. +Cloud = false From 0af3d000e19c87463820d01a6dd80690a0d0ec76 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Nov 2026 12:37:30 +0000 Subject: [PATCH 12/29] Limit immutable history test to successful binding --- .../dms/bind-immutable/databricks.yml.tmpl | 1 - .../bundle/dms/bind-immutable/extra.yml | 4 -- .../bundle/dms/bind-immutable/output.txt | 38 ++++--------------- acceptance/bundle/dms/bind-immutable/script | 15 +------- 4 files changed, 9 insertions(+), 49 deletions(-) delete mode 100644 acceptance/bundle/dms/bind-immutable/extra.yml diff --git a/acceptance/bundle/dms/bind-immutable/databricks.yml.tmpl b/acceptance/bundle/dms/bind-immutable/databricks.yml.tmpl index 300a5d3a8e6..4d6fc71467e 100644 --- a/acceptance/bundle/dms/bind-immutable/databricks.yml.tmpl +++ b/acceptance/bundle/dms/bind-immutable/databricks.yml.tmpl @@ -1,6 +1,5 @@ bundle: name: dms-bind-immutable-$UNIQUE_NAME -include: [] workspace: sync_mode: immutable resources: diff --git a/acceptance/bundle/dms/bind-immutable/extra.yml b/acceptance/bundle/dms/bind-immutable/extra.yml deleted file mode 100644 index cd35a4be7e8..00000000000 --- a/acceptance/bundle/dms/bind-immutable/extra.yml +++ /dev/null @@ -1,4 +0,0 @@ -resources: - jobs: - bar: - name: bar diff --git a/acceptance/bundle/dms/bind-immutable/output.txt b/acceptance/bundle/dms/bind-immutable/output.txt index debd86616cf..99e62a0e129 100644 --- a/acceptance/bundle/dms/bind-immutable/output.txt +++ b/acceptance/bundle/dms/bind-immutable/output.txt @@ -1,35 +1,13 @@ -=== An immutable bundle can record its first bind before any snapshot exists >>> [CLI] bundle deployment bind foo [JOB_ID] --auto-approve Successfully bound job with an id '[JOB_ID]' Run 'bundle deploy' to deploy changes to your workspace ->>> print_requests.py --dms //versions --oneline -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "3"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.foo"}], "version_type": "VERSION_TYPE_DEPLOY"}} -{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/3/operations/jobs.foo", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-immutable-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"foo\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/3/complete", "body": {"completion_reason": "VERSION_COMPLETE_SUCCESS"}} - -=== Deploy creates the immutable snapshot ->>> [CLI] bundle deploy -Created internal_immutable_snapshots.immutable -Updated jobs.foo -Files: 0 uploaded, 0 deleted -Resources: 1 created, 1 changed, 0 deleted, 0 unchanged - -=== Binding another resource preserves the last deployed snapshot metadata ->>> update_file.py databricks.yml include: [] include: [extra.yml] - ->>> [CLI] bundle deployment bind bar [JOB_ID] --auto-approve -Successfully bound job with an id '[JOB_ID]' -Run 'bundle deploy' to deploy changes to your workspace - ->>> print_requests.py --dms //api/2.0/bundle --oneline -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "7"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.bar"}], "previous_version_id": "4", "version_type": "VERSION_TYPE_DEPLOY"}} -{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/7/operations/jobs.bar", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-immutable-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"bar\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/7/complete", "body": {"completion_reason": "VERSION_COMPLETE_SUCCESS"}} - ->>> MSYS_NO_PATHCONV=1 [CLI] api get /api/2.0/bundle/deployments/[DEPLOYMENT_ID] -{ - "file_path": "[DEPLOYED_FILE_PATH]", - "preserved": true -} +>>> MSYS_NO_PATHCONV=1 [CLI] bundle-deployments list-resources deployments/[DEPLOYMENT_ID] -o json +[ + { + "last_action_type": "OPERATION_ACTION_TYPE_BIND", + "resource_id": "[JOB_ID]", + "resource_key": "jobs.foo" + } +] diff --git a/acceptance/bundle/dms/bind-immutable/script b/acceptance/bundle/dms/bind-immutable/script index cfb1102a4fc..929a499ddd6 100644 --- a/acceptance/bundle/dms/bind-immutable/script +++ b/acceptance/bundle/dms/bind-immutable/script @@ -2,20 +2,7 @@ envsubst < databricks.yml.tmpl > databricks.yml job_id=$($CLI jobs create --json '{"name": "bind-target-'$UNIQUE_NAME'"}' | jq -r '.job_id') add_repl "$job_id" JOB_ID -title "An immutable bundle can record its first bind before any snapshot exists" trace $CLI bundle deployment bind foo "$job_id" --auto-approve -trace print_requests.py --dms //versions --oneline | contains.py OPERATION_ACTION_TYPE_BIND VERSION_COMPLETE_SUCCESS - -title "Deploy creates the immutable snapshot" -trace $CLI bundle deploy deployment_id=$(MSYS_NO_PATHCONV=1 $CLI workspace get-status "/Workspace/Users/${CURRENT_USER_NAME}/.bundle/dms-bind-immutable-${UNIQUE_NAME}/default/state/resources.deployment.json" -o json | jq -r '.object_id') -file_path=$(MSYS_NO_PATHCONV=1 $CLI api get "/api/2.0/bundle/deployments/${deployment_id}" | jq -r '.workspace_info.file_path') -add_repl "$file_path" DEPLOYED_FILE_PATH -rm -f "$OUT_REQUESTS" - -title "Binding another resource preserves the last deployed snapshot metadata" -trace update_file.py databricks.yml 'include: []' 'include: [extra.yml]' -trace $CLI bundle deployment bind bar "$job_id" --auto-approve -trace print_requests.py --dms //api/2.0/bundle --oneline | contains.py OPERATION_ACTION_TYPE_BIND VERSION_COMPLETE_SUCCESS -trace MSYS_NO_PATHCONV=1 $CLI api get "/api/2.0/bundle/deployments/${deployment_id}" | jq --arg expected "$file_path" '{file_path: .workspace_info.file_path, preserved: (.workspace_info.file_path == $expected)}' +trace MSYS_NO_PATHCONV=1 $CLI bundle-deployments list-resources "deployments/${deployment_id}" -o json | jq -S 'map({resource_key, resource_id, last_action_type})' rm -f "$OUT_REQUESTS" From 0ba8782eedf6ce427b24bb234eb2d131d35c15b3 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Nov 2026 12:55:57 +0000 Subject: [PATCH 13/29] Record bind history without rewriting resource state --- .../recreation/out.state_after_bind.json | 1 + .../bind/dashboard/recreation/script | 2 +- .../dms/bind-history-failures/output.txt | 18 ++++++------- acceptance/bundle/dms/bind-unbind/output.txt | 6 ++--- bundle/direct/bind.go | 26 +++++++++++++++---- bundle/direct/dstate/state.go | 21 +++++++++++++++ bundle/phases/bind.go | 14 +++------- 7 files changed, 59 insertions(+), 29 deletions(-) diff --git a/acceptance/bundle/deployment/bind/dashboard/recreation/out.state_after_bind.json b/acceptance/bundle/deployment/bind/dashboard/recreation/out.state_after_bind.json index 4ba78767d69..05614d798bf 100644 --- a/acceptance/bundle/deployment/bind/dashboard/recreation/out.state_after_bind.json +++ b/acceptance/bundle/deployment/bind/dashboard/recreation/out.state_after_bind.json @@ -2,6 +2,7 @@ "state_version": 3, "cli_version": "[CLI_VERSION]", "lineage": "[UUID]", + "serial": 2, "state": { "resources.dashboards.dashboard1": { "__id__": "[DASHBOARD_ID]", diff --git a/acceptance/bundle/deployment/bind/dashboard/recreation/script b/acceptance/bundle/deployment/bind/dashboard/recreation/script index 1ef7b6071a5..6d8f12f1821 100644 --- a/acceptance/bundle/deployment/bind/dashboard/recreation/script +++ b/acceptance/bundle/deployment/bind/dashboard/recreation/script @@ -13,7 +13,7 @@ cleanupRemoveDashboard() { trap cleanupRemoveDashboard EXIT trace $CLI bundle deployment bind dashboard1 "${DASHBOARD_ID}" --auto-approve -trace print_state.py --no-dms | jq 'del(.serial, .features)' | nostamp.py --indent 1 > out.state_after_bind.json +trace print_state.py --no-dms | nostamp.py --indent 1 > out.state_after_bind.json ETAG=$(jq '.state["resources.dashboards.dashboard1"].state.etag // .resources[0].instances[0].attributes.etag // "no-etag"' < out.state_after_bind.json) echo $ETAG add_repl $ETAG ETAG diff --git a/acceptance/bundle/dms/bind-history-failures/output.txt b/acceptance/bundle/dms/bind-history-failures/output.txt index 47cc75bae27..ec0c5636ff2 100644 --- a/acceptance/bundle/dms/bind-history-failures/output.txt +++ b/acceptance/bundle/dms/bind-history-failures/output.txt @@ -39,12 +39,12 @@ Run 'bundle deploy' to deploy changes to your workspace >>> print_requests.py --dms //versions --oneline {"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "3"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.create_failed"}], "version_type": "VERSION_TYPE_DEPLOY"}} -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "6"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.record_failed"}], "version_type": "VERSION_TYPE_DEPLOY"}} -{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/6/operations/jobs.record_failed", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-history-failures-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"record_failed\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/6/complete", "body": {"completion_reason": "VERSION_COMPLETE_FAILURE"}} -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "9"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.complete_failed"}], "previous_version_id": "6", "version_type": "VERSION_TYPE_DEPLOY"}} -{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/9/operations/jobs.complete_failed", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-history-failures-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"complete_failed\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/9/complete", "body": {"completion_reason": "VERSION_COMPLETE_SUCCESS"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "5"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.record_failed"}], "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/5/operations/jobs.record_failed", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-history-failures-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"record_failed\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/5/complete", "body": {"completion_reason": "VERSION_COMPLETE_FAILURE"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "7"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.complete_failed"}], "previous_version_id": "5", "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/7/operations/jobs.complete_failed", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-history-failures-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"complete_failed\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/7/complete", "body": {"completion_reason": "VERSION_COMPLETE_SUCCESS"}} === A remote state upload failure fails the bind version >>> fault.py POST /api/2.0/workspace-files/import-file/**resources.json 400 0 1 @@ -59,9 +59,9 @@ API message: Fault injected by test. >>> print_requests.py --dms //versions --oneline -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "12"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.upload_failed"}], "previous_version_id": "9", "version_type": "VERSION_TYPE_DEPLOY"}} -{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/12/operations/jobs.upload_failed", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-history-failures-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"upload_failed\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/12/complete", "body": {"completion_reason": "VERSION_COMPLETE_FAILURE"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "9"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.upload_failed"}], "previous_version_id": "7", "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/9/operations/jobs.upload_failed", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-history-failures-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"upload_failed\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/9/complete", "body": {"completion_reason": "VERSION_COMPLETE_FAILURE"}} === Remote state still contains only the successful bindings >>> [CLI] bundle summary -o json diff --git a/acceptance/bundle/dms/bind-unbind/output.txt b/acceptance/bundle/dms/bind-unbind/output.txt index e07ca28f859..38d79d119fb 100644 --- a/acceptance/bundle/dms/bind-unbind/output.txt +++ b/acceptance/bundle/dms/bind-unbind/output.txt @@ -86,9 +86,9 @@ Run 'bundle deploy' to deploy changes to your workspace } >>> print_requests.py --dms //versions --oneline -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "8"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.foo"}], "previous_version_id": "4", "version_type": "VERSION_TYPE_DEPLOY"}} -{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/8/operations/jobs.foo", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-unbind-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"foo\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/8/complete", "body": {"completion_reason": "VERSION_COMPLETE_SUCCESS"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "7"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.foo"}], "previous_version_id": "4", "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/7/operations/jobs.foo", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-unbind-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"foo\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/7/complete", "body": {"completion_reason": "VERSION_COMPLETE_SUCCESS"}} === A subsequent deploy continues managing the same job >>> [CLI] bundle deploy diff --git a/bundle/direct/bind.go b/bundle/direct/bind.go index 73852cdde00..8e0a4e69191 100644 --- a/bundle/direct/bind.go +++ b/bundle/direct/bind.go @@ -10,6 +10,7 @@ import ( "github.com/databricks/cli/bundle/deployplan" "github.com/databricks/cli/bundle/direct/dresources" "github.com/databricks/cli/bundle/direct/dstate" + bundleenv "github.com/databricks/cli/bundle/env" "github.com/databricks/cli/libs/log" "github.com/databricks/cli/libs/structs/structaccess" "github.com/databricks/cli/libs/structs/structpath" @@ -92,15 +93,11 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac } // Open temp state - err := b.StateDB.Open(ctx, tmpStatePath, dstate.ReadingFile, dstate.WithRecovery(false), dstate.OpenDmsArgs{}) + err := b.openBindingState(ctx, tmpStatePath, configRoot, false) if err != nil { os.Remove(tmpStatePath) return nil, err } - if err := b.StateDB.UpgradeToWrite(); err != nil { - os.Remove(tmpStatePath) - return nil, err - } // Save state with ID and empty state (like migrate does) err = b.StateDB.SaveState(ctx, resourceKey, resourceID, struct{}{}, nil) @@ -241,6 +238,25 @@ func (result *BindResult) Cancel() { } } +// openBindingState selects history mode before the binding write so recording needs no extra state commit. +func (b *DeploymentBundle) openBindingState(ctx context.Context, statePath string, configRoot *config.Root, withRecovery dstate.WithRecovery) error { + var configured *bool + if configRoot.Experimental != nil { + configured = configRoot.Experimental.DeploymentHistory + } + mode := dstate.ReadingFile + if bundleenv.RecordsDeploymentHistory(ctx, configured) { + mode = dstate.WritingDeploymentHistoryShadow + } + if err := b.StateDB.Open(ctx, statePath, mode, withRecovery, dstate.OpenDmsArgs{}); err != nil { + return err + } + if mode == dstate.ReadingFile { + return b.StateDB.UpgradeToWrite() + } + return nil +} + // Unbind removes a resource from direct engine state without deleting // the workspace resource. Also removes associated permissions/grants entries. func (b *DeploymentBundle) Unbind(ctx context.Context, statePath, resourceKey string) error { diff --git a/bundle/direct/dstate/state.go b/bundle/direct/dstate/state.go index de00e76233a..591c335f108 100644 --- a/bundle/direct/dstate/state.go +++ b/bundle/direct/dstate/state.go @@ -276,6 +276,27 @@ func (db *DeploymentState) CompleteVersion(ctx context.Context, success bool) (b return success, nil } +// RecordResourceHistory publishes an operation's state (nil for removal) without writing local state. +func (db *DeploymentState) RecordResourceHistory(ctx context.Context, resourceKey string, entry *ResourceEntry) error { + db.AssertOpenedForWrite() + buf := db.getOperationBuffer() + if buf == nil { + return nil + } + var recorded json.RawMessage + var resourceID string + if entry != nil { + var err error + recorded, err = json.Marshal(RecordedState{State: entry.State, DependsOn: entry.DependsOn}) + if err != nil { + return err + } + resourceID = entry.ID + } + buf.RecordOperation(ctx, resourceKey, false, resourceID, recorded) + return nil +} + // RecordFailure records that a resource did not apply, so the history says why rather than // leaving the resource out. resourceID is the id it had before the failure. func (db *DeploymentState) RecordFailure(resourceKey, resourceID string, cause error) { diff --git a/bundle/phases/bind.go b/bundle/phases/bind.go index 0aee639b437..2cbb9c3ea4c 100644 --- a/bundle/phases/bind.go +++ b/bundle/phases/bind.go @@ -15,7 +15,6 @@ import ( "github.com/databricks/cli/bundle/deploy/snapshot" "github.com/databricks/cli/bundle/deployplan" "github.com/databricks/cli/bundle/direct/dstate" - bundleenv "github.com/databricks/cli/bundle/env" "github.com/databricks/cli/bundle/statemgmt" "github.com/databricks/cli/libs/agent" "github.com/databricks/cli/libs/cmdctx" @@ -218,11 +217,7 @@ func recordBindHistory(ctx context.Context, b *bundle.Bundle, resourceKey string err = errors.Join(err, finalizeErr) }() - var configured *bool - if b.Config.Experimental != nil { - configured = b.Config.Experimental.DeploymentHistory - } - if !db.RecordsHistory() && !bundleenv.RecordsDeploymentHistory(ctx, configured) { + if !db.RecordsHistory() { return nil } @@ -238,10 +233,7 @@ func recordBindHistory(ctx context.Context, b *bundle.Bundle, resourceKey string } db.VersionID = next - 1 } - if db.Data.Features == nil { - db.Data.Features = make(map[string]struct{}) - } - db.Data.Features[dstate.FeatureDeploymentHistoryShadow] = struct{}{} + if err := db.UpgradeToWrite(); err != nil { return err } @@ -261,5 +253,5 @@ func recordBindHistory(ctx context.Context, b *bundle.Bundle, resourceKey string if !ok { return fmt.Errorf("bound resource %s is missing from deployment state", resourceKey) } - return db.SaveState(ctx, resourceKey, entry.ID, entry.State, entry.DependsOn) + return db.RecordResourceHistory(ctx, resourceKey, &entry) } From 68f395e9af4d03e49c00782d6e450339e090a496 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Nov 2026 13:22:55 +0000 Subject: [PATCH 14/29] Parse the last recorded deployment version directly --- bundle/phases/bind.go | 9 +++------ cmd/bundle/utils/process.go | 20 +++----------------- libs/dms/metadata.go | 13 +++++++++++++ libs/dms/metadata_test.go | 21 +++++++++++++++++++++ 4 files changed, 40 insertions(+), 23 deletions(-) diff --git a/bundle/phases/bind.go b/bundle/phases/bind.go index 2cbb9c3ea4c..fdf939e2ace 100644 --- a/bundle/phases/bind.go +++ b/bundle/phases/bind.go @@ -226,12 +226,9 @@ func recordBindHistory(ctx context.Context, b *bundle.Bundle, resourceKey string return err } db.DeploymentID = deploymentID - if current != nil { - next, err := dms.NextVersion(current.LastVersionId) - if err != nil { - return err - } - db.VersionID = next - 1 + db.VersionID, err = dms.ParseLastVersionID(current) + if err != nil { + return err } if err := db.UpgradeToWrite(); err != nil { diff --git a/cmd/bundle/utils/process.go b/cmd/bundle/utils/process.go index 33d53a1fed1..0f866174802 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" @@ -416,7 +415,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 @@ -632,19 +631,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. @@ -661,7 +647,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 } @@ -681,7 +667,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/libs/dms/metadata.go b/libs/dms/metadata.go index 240c85a35c3..98010ac7b6f 100644 --- a/libs/dms/metadata.go +++ b/libs/dms/metadata.go @@ -75,6 +75,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) { diff --git a/libs/dms/metadata_test.go b/libs/dms/metadata_test.go index 5122533dafc..799a910b273 100644 --- a/libs/dms/metadata_test.go +++ b/libs/dms/metadata_test.go @@ -3,6 +3,7 @@ package dms import ( "testing" + "github.com/databricks/databricks-sdk-go/service/bundledeployments" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -41,3 +42,23 @@ func TestDeploymentIDFromName(t *testing.T) { _, err = DeploymentIDFromName("deployments/") assert.Error(t, err) } + +func TestParseLastVersionID(t *testing.T) { + for _, tc := range []struct { + name string + deployment *bundledeployments.Deployment + want int + }{ + {"no deployment", nil, 0}, + {"no version", &bundledeployments.Deployment{}, 0}, + {"existing version", &bundledeployments.Deployment{LastVersionId: "4"}, 4}, + } { + t.Run(tc.name, func(t *testing.T) { + got, err := ParseLastVersionID(tc.deployment) + require.NoError(t, err) + assert.Equal(t, tc.want, got) + }) + } + _, err := ParseLastVersionID(&bundledeployments.Deployment{LastVersionId: "invalid"}) + assert.ErrorContains(t, err, "last_version_id") +} From 7a5b4e8938eb5b50bc02c3139ea36b9781451029 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Nov 2026 13:22:55 +0000 Subject: [PATCH 15/29] testserver: accept Feature Store resources in DMS versions --- libs/testserver/bundledeployments_test.go | 43 +++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 libs/testserver/bundledeployments_test.go diff --git a/libs/testserver/bundledeployments_test.go b/libs/testserver/bundledeployments_test.go new file mode 100644 index 00000000000..41444971767 --- /dev/null +++ b/libs/testserver/bundledeployments_test.go @@ -0,0 +1,43 @@ +package testserver_test + +import ( + "testing" + + "github.com/databricks/cli/libs/testserver" + "github.com/databricks/databricks-sdk-go" + "github.com/databricks/databricks-sdk-go/service/bundledeployments" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestCreateVersionAcceptsFeatureResource(t *testing.T) { + server := testserver.New(t) + testserver.AddDefaultHandlers(server) + client, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "testtoken"}) + require.NoError(t, err) + ctx := t.Context() + const statePath = "/Workspace/dms-feature-test" + require.NoError(t, client.Workspace.MkdirsByPath(ctx, statePath)) + deployment, err := client.BundleDeployments.CreateDeployment(ctx, bundledeployments.CreateDeploymentRequest{ + Deployment: bundledeployments.Deployment{InitialParentPath: statePath}, + }) + require.NoError(t, err) + + version, err := client.BundleDeployments.CreateVersion(ctx, bundledeployments.CreateVersionRequest{ + Parent: deployment.Name, + VersionId: "1", + Version: bundledeployments.Version{ + VersionType: bundledeployments.VersionTypeVersionTypeDeploy, + Operations: []bundledeployments.StagedOperation{{ + ResourceKey: "features.feature1", + ActionType: bundledeployments.OperationActionTypeOperationActionTypeBind, + }}, + }, + }) + require.NoError(t, err) + operations, err := client.BundleDeployments.ListOperationsAll(ctx, bundledeployments.ListOperationsRequest{Parent: version.Name}) + require.NoError(t, err) + require.Len(t, operations, 1) + assert.Equal(t, "features.feature1", operations[0].ResourceKey) + assert.Equal(t, bundledeployments.OperationActionTypeOperationActionTypeBind, operations[0].ActionType) +} From db0cae5ac7451496ecf5adae9fc3fe3daf5a07d9 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Nov 2026 13:26:06 +0000 Subject: [PATCH 16/29] Remove feature warning workarounds after fixing the DMS stub --- .../bundle/deployment/bind/feature/out.bind.plain.txt | 4 ---- .../bundle/deployment/bind/feature/out.bind.recorded.txt | 5 ----- acceptance/bundle/deployment/bind/feature/out.test.toml | 1 - acceptance/bundle/deployment/bind/feature/output.txt | 4 ++++ acceptance/bundle/deployment/bind/feature/script | 2 +- acceptance/bundle/deployment/bind/feature/test.toml | 6 ------ 6 files changed, 5 insertions(+), 17 deletions(-) delete mode 100644 acceptance/bundle/deployment/bind/feature/out.bind.plain.txt delete mode 100644 acceptance/bundle/deployment/bind/feature/out.bind.recorded.txt diff --git a/acceptance/bundle/deployment/bind/feature/out.bind.plain.txt b/acceptance/bundle/deployment/bind/feature/out.bind.plain.txt deleted file mode 100644 index 2155184a00f..00000000000 --- a/acceptance/bundle/deployment/bind/feature/out.bind.plain.txt +++ /dev/null @@ -1,4 +0,0 @@ - ->>> [CLI] bundle deployment bind feature1 main.myschema.boundfeature --auto-approve -Successfully bound feature with an id 'main.myschema.boundfeature' -Run 'bundle deploy' to deploy changes to your workspace diff --git a/acceptance/bundle/deployment/bind/feature/out.bind.recorded.txt b/acceptance/bundle/deployment/bind/feature/out.bind.recorded.txt deleted file mode 100644 index e91d2b70ab7..00000000000 --- a/acceptance/bundle/deployment/bind/feature/out.bind.recorded.txt +++ /dev/null @@ -1,5 +0,0 @@ - ->>> [CLI] bundle deployment bind feature1 main.myschema.boundfeature --auto-approve -Warn: Failed to record bind history: failed to create deployment version: operations.resource_key must have a known resource type prefix (e.g. 'jobs.', 'pipelines.'): features.feature1 -Successfully bound feature with an id 'main.myschema.boundfeature' -Run 'bundle deploy' to deploy changes to your workspace diff --git a/acceptance/bundle/deployment/bind/feature/out.test.toml b/acceptance/bundle/deployment/bind/feature/out.test.toml index 8a45520420b..a927a5fbc06 100644 --- a/acceptance/bundle/deployment/bind/feature/out.test.toml +++ b/acceptance/bundle/deployment/bind/feature/out.test.toml @@ -1,3 +1,2 @@ Cloud = false EnvMatrix.DMS = ["", "true"] -EnvMatrix.OUTPUT_MODE = ["plain", "recorded"] diff --git a/acceptance/bundle/deployment/bind/feature/output.txt b/acceptance/bundle/deployment/bind/feature/output.txt index 8466bb0eaa5..d4d47f6a660 100644 --- a/acceptance/bundle/deployment/bind/feature/output.txt +++ b/acceptance/bundle/deployment/bind/feature/output.txt @@ -1,4 +1,8 @@ +>>> [CLI] bundle deployment bind feature1 main.myschema.boundfeature --auto-approve +Successfully bound feature with an id 'main.myschema.boundfeature' +Run 'bundle deploy' to deploy changes to your workspace + >>> [CLI] bundle summary Name: test-bundle Target: default diff --git a/acceptance/bundle/deployment/bind/feature/script b/acceptance/bundle/deployment/bind/feature/script index ed49b8938a5..8e27f69056c 100644 --- a/acceptance/bundle/deployment/bind/feature/script +++ b/acceptance/bundle/deployment/bind/feature/script @@ -1,4 +1,4 @@ -trace $CLI bundle deployment bind feature1 main.myschema.boundfeature --auto-approve 2> out.bind.$OUTPUT_MODE.txt +trace $CLI bundle deployment bind feature1 main.myschema.boundfeature --auto-approve trace $CLI bundle summary trace $CLI bundle deployment unbind feature1 diff --git a/acceptance/bundle/deployment/bind/feature/test.toml b/acceptance/bundle/deployment/bind/feature/test.toml index 97da8412e45..84e8bf9b8f4 100644 --- a/acceptance/bundle/deployment/bind/feature/test.toml +++ b/acceptance/bundle/deployment/bind/feature/test.toml @@ -1,9 +1,3 @@ -# Keep CI's DMS= variant while using non-empty labels for per-variant output files. -EnvMatrix.OUTPUT_MODE = ["plain", "recorded"] -EnvMatrixExclude.plain_with_history = ["DMS=true", "OUTPUT_MODE=plain"] -EnvMatrixExclude.recorded_without_history = ["DMS=", "OUTPUT_MODE=recorded"] -EnvVaryOutput = "OUTPUT_MODE" -EnvRepl.OUTPUT_MODE = false Cloud = false Ignore = [ From 9c591427723b2c309bab5a93e663597a9f13c7d7 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Nov 2026 14:24:54 +0000 Subject: [PATCH 17/29] Normalize summary deployment stamps with nostamp --- acceptance/bin/nostamp.py | 24 ++++++++++++++++++- .../deployment/bind/pipelines/update/script | 2 +- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/acceptance/bin/nostamp.py b/acceptance/bin/nostamp.py index 8a0c222630c..db503035870 100755 --- a/acceptance/bin/nostamp.py +++ b/acceptance/bin/nostamp.py @@ -7,13 +7,14 @@ is a 2-space indent, keys in input order, <>& left unescaped, and integers at full precision. Tests under bundle/dms assert the stamp itself and must not use it. -Removed in three shapes plus the plan header: +Removed in these shapes: 1. deployment_id/version_id nested in a deployment block, recognized by the neighbouring "kind" and "metadata_file_path", which are kept; version_id == "" is kept (a Terraform state dump carries that for an unstamped job). 2. changes entries keyed "deployment.deployment_id" / "deployment.version_id". 3. a changes object emptied by (2), or already empty. 4. the recorded plan header field features. + 5. deployment_id/latest_version_id in bundle.deployment; other settings are kept. """ import argparse @@ -23,6 +24,7 @@ _STAMP_KEYS = ("deployment_id", "version_id") _CHANGE_KEYS = ("deployment.deployment_id", "deployment.version_id") _PLAN_HEADER_KEYS = ("features",) +_BUNDLE_STAMP_KEYS = ("deployment_id", "latest_version_id") def scrub(node): @@ -48,10 +50,30 @@ def scrub(node): def render(data, indent): + """ + >>> data = {"bundle": {"deployment": {"deployment_id": "42", "latest_version_id": 7}}} + >>> json.loads(render(data, 2)) + {'bundle': {}} + >>> deployment = {"deployment_id": "42", "latest_version_id": 7, "lock": {"enabled": False}} + >>> json.loads(render({"bundle": {"deployment": deployment}}, 2)) + {'bundle': {'deployment': {'lock': {'enabled': False}}}} + >>> json.loads(render({"bundle": {"deployment": {"lock": {"enabled": False}}}}, 2)) + {'bundle': {'deployment': {'lock': {'enabled': False}}}} + >>> json.loads(render({"bundle": {"deployment": {}}}, 2)) + {'bundle': {'deployment': {}}} + """ data = scrub(data) if isinstance(data, dict): for k in _PLAN_HEADER_KEYS: data.pop(k, None) # 4. plan header, present only at the root + bundle = data.get("bundle") + if isinstance(bundle, dict): + deployment = bundle.get("deployment") + if isinstance(deployment, dict) and any(k in deployment for k in _BUNDLE_STAMP_KEYS): + for k in _BUNDLE_STAMP_KEYS: + deployment.pop(k, None) + if not deployment: + bundle.pop("deployment") return json.dumps(data, indent=indent, ensure_ascii=False, separators=(",", ": ")) diff --git a/acceptance/bundle/deployment/bind/pipelines/update/script b/acceptance/bundle/deployment/bind/pipelines/update/script index d1a67c9b960..77f95d4d3ed 100644 --- a/acceptance/bundle/deployment/bind/pipelines/update/script +++ b/acceptance/bundle/deployment/bind/pipelines/update/script @@ -9,7 +9,7 @@ rm -f "$OUT_REQUESTS" trace $CLI bundle deployment bind foo $NEW_PIPELINE_ID --auto-approve &> out.bind-success.txt print_requests.py '^//import-file/' '^//workspace/delete' -trace $CLI bundle summary -o json | jq 'del(.bundle.deployment)' | nostamp.py > out.summary.json +trace $CLI bundle summary -o json | nostamp.py > out.summary.json trace $CLI bundle plan rm -f "$OUT_REQUESTS" From c9b2a70042413ca9a32c516b3f7501f209fd9ecd Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Nov 2026 14:49:10 +0000 Subject: [PATCH 18/29] Remove nostamp inline doctests --- acceptance/bin/nostamp.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/acceptance/bin/nostamp.py b/acceptance/bin/nostamp.py index db503035870..aefb1dd8e02 100755 --- a/acceptance/bin/nostamp.py +++ b/acceptance/bin/nostamp.py @@ -50,18 +50,6 @@ def scrub(node): def render(data, indent): - """ - >>> data = {"bundle": {"deployment": {"deployment_id": "42", "latest_version_id": 7}}} - >>> json.loads(render(data, 2)) - {'bundle': {}} - >>> deployment = {"deployment_id": "42", "latest_version_id": 7, "lock": {"enabled": False}} - >>> json.loads(render({"bundle": {"deployment": deployment}}, 2)) - {'bundle': {'deployment': {'lock': {'enabled': False}}}} - >>> json.loads(render({"bundle": {"deployment": {"lock": {"enabled": False}}}}, 2)) - {'bundle': {'deployment': {'lock': {'enabled': False}}}} - >>> json.loads(render({"bundle": {"deployment": {}}}, 2)) - {'bundle': {'deployment': {}}} - """ data = scrub(data) if isinstance(data, dict): for k in _PLAN_HEADER_KEYS: From 7f9307f92ee281b2304f9252e198d22df93a1dc9 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Nov 2026 14:49:37 +0000 Subject: [PATCH 19/29] Explain summary DMS stamp normalization --- acceptance/bin/nostamp.py | 1 + 1 file changed, 1 insertion(+) diff --git a/acceptance/bin/nostamp.py b/acceptance/bin/nostamp.py index aefb1dd8e02..fedaa280a88 100755 --- a/acceptance/bin/nostamp.py +++ b/acceptance/bin/nostamp.py @@ -54,6 +54,7 @@ def render(data, indent): if isinstance(data, dict): for k in _PLAN_HEADER_KEYS: data.pop(k, None) # 4. plan header, present only at the root + # Strip the summary's DMS IDs, keeping deployment settings such as locks. bundle = data.get("bundle") if isinstance(bundle, dict): deployment = bundle.get("deployment") From 4d376f21497d17e882636c9047000e1dea0ae2b9 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Nov 2026 14:50:17 +0000 Subject: [PATCH 20/29] Remove the DMS test-server regression test --- libs/testserver/bundledeployments_test.go | 43 ----------------------- 1 file changed, 43 deletions(-) delete mode 100644 libs/testserver/bundledeployments_test.go diff --git a/libs/testserver/bundledeployments_test.go b/libs/testserver/bundledeployments_test.go deleted file mode 100644 index 41444971767..00000000000 --- a/libs/testserver/bundledeployments_test.go +++ /dev/null @@ -1,43 +0,0 @@ -package testserver_test - -import ( - "testing" - - "github.com/databricks/cli/libs/testserver" - "github.com/databricks/databricks-sdk-go" - "github.com/databricks/databricks-sdk-go/service/bundledeployments" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func TestCreateVersionAcceptsFeatureResource(t *testing.T) { - server := testserver.New(t) - testserver.AddDefaultHandlers(server) - client, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "testtoken"}) - require.NoError(t, err) - ctx := t.Context() - const statePath = "/Workspace/dms-feature-test" - require.NoError(t, client.Workspace.MkdirsByPath(ctx, statePath)) - deployment, err := client.BundleDeployments.CreateDeployment(ctx, bundledeployments.CreateDeploymentRequest{ - Deployment: bundledeployments.Deployment{InitialParentPath: statePath}, - }) - require.NoError(t, err) - - version, err := client.BundleDeployments.CreateVersion(ctx, bundledeployments.CreateVersionRequest{ - Parent: deployment.Name, - VersionId: "1", - Version: bundledeployments.Version{ - VersionType: bundledeployments.VersionTypeVersionTypeDeploy, - Operations: []bundledeployments.StagedOperation{{ - ResourceKey: "features.feature1", - ActionType: bundledeployments.OperationActionTypeOperationActionTypeBind, - }}, - }, - }) - require.NoError(t, err) - operations, err := client.BundleDeployments.ListOperationsAll(ctx, bundledeployments.ListOperationsRequest{Parent: version.Name}) - require.NoError(t, err) - require.Len(t, operations, 1) - assert.Equal(t, "features.feature1", operations[0].ResourceKey) - assert.Equal(t, bundledeployments.OperationActionTypeOperationActionTypeBind, operations[0].ActionType) -} From 82ec2172e9f5352eae54328f9aa78990b17583ae Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Nov 2026 15:09:50 +0000 Subject: [PATCH 21/29] Number the summary stamp normalization comment --- acceptance/bin/nostamp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/acceptance/bin/nostamp.py b/acceptance/bin/nostamp.py index fedaa280a88..8f370edc657 100755 --- a/acceptance/bin/nostamp.py +++ b/acceptance/bin/nostamp.py @@ -54,7 +54,7 @@ def render(data, indent): if isinstance(data, dict): for k in _PLAN_HEADER_KEYS: data.pop(k, None) # 4. plan header, present only at the root - # Strip the summary's DMS IDs, keeping deployment settings such as locks. + # 5. Strip the summary's DMS IDs, keeping deployment settings such as locks. bundle = data.get("bundle") if isinstance(bundle, dict): deployment = bundle.get("deployment") From 9fb6767803dee5b631f201ab16cb5205bea215f1 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Nov 2026 17:49:00 +0000 Subject: [PATCH 22/29] Publish bind history directly through the DMS SDK --- .../dms/bind-history-failures/output.txt | 2 +- bundle/direct/bind.go | 26 +--- bundle/direct/dstate/state.go | 21 --- bundle/phases/bind.go | 143 +++++++++++++----- 4 files changed, 108 insertions(+), 84 deletions(-) diff --git a/acceptance/bundle/dms/bind-history-failures/output.txt b/acceptance/bundle/dms/bind-history-failures/output.txt index ec0c5636ff2..8a2cf5d20f8 100644 --- a/acceptance/bundle/dms/bind-history-failures/output.txt +++ b/acceptance/bundle/dms/bind-history-failures/output.txt @@ -11,7 +11,7 @@ Run 'bundle deploy' to deploy changes to your workspace >>> fault.py PATCH /api/2.0/bundle/** 400 0 1 >>> [CLI] bundle deployment bind record_failed [JOB_ID] --auto-approve -Warn: Failed to record deployment history: recording operation for resources.jobs.record_failed: Fault injected by test. +Warn: Failed to record bind history: Fault injected by test. Successfully bound job with an id '[JOB_ID]' Run 'bundle deploy' to deploy changes to your workspace diff --git a/bundle/direct/bind.go b/bundle/direct/bind.go index 8e0a4e69191..73852cdde00 100644 --- a/bundle/direct/bind.go +++ b/bundle/direct/bind.go @@ -10,7 +10,6 @@ import ( "github.com/databricks/cli/bundle/deployplan" "github.com/databricks/cli/bundle/direct/dresources" "github.com/databricks/cli/bundle/direct/dstate" - bundleenv "github.com/databricks/cli/bundle/env" "github.com/databricks/cli/libs/log" "github.com/databricks/cli/libs/structs/structaccess" "github.com/databricks/cli/libs/structs/structpath" @@ -93,11 +92,15 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac } // Open temp state - err := b.openBindingState(ctx, tmpStatePath, configRoot, false) + err := b.StateDB.Open(ctx, tmpStatePath, dstate.ReadingFile, dstate.WithRecovery(false), dstate.OpenDmsArgs{}) if err != nil { os.Remove(tmpStatePath) return nil, err } + if err := b.StateDB.UpgradeToWrite(); err != nil { + os.Remove(tmpStatePath) + return nil, err + } // Save state with ID and empty state (like migrate does) err = b.StateDB.SaveState(ctx, resourceKey, resourceID, struct{}{}, nil) @@ -238,25 +241,6 @@ func (result *BindResult) Cancel() { } } -// openBindingState selects history mode before the binding write so recording needs no extra state commit. -func (b *DeploymentBundle) openBindingState(ctx context.Context, statePath string, configRoot *config.Root, withRecovery dstate.WithRecovery) error { - var configured *bool - if configRoot.Experimental != nil { - configured = configRoot.Experimental.DeploymentHistory - } - mode := dstate.ReadingFile - if bundleenv.RecordsDeploymentHistory(ctx, configured) { - mode = dstate.WritingDeploymentHistoryShadow - } - if err := b.StateDB.Open(ctx, statePath, mode, withRecovery, dstate.OpenDmsArgs{}); err != nil { - return err - } - if mode == dstate.ReadingFile { - return b.StateDB.UpgradeToWrite() - } - return nil -} - // Unbind removes a resource from direct engine state without deleting // the workspace resource. Also removes associated permissions/grants entries. func (b *DeploymentBundle) Unbind(ctx context.Context, statePath, resourceKey string) error { diff --git a/bundle/direct/dstate/state.go b/bundle/direct/dstate/state.go index 591c335f108..de00e76233a 100644 --- a/bundle/direct/dstate/state.go +++ b/bundle/direct/dstate/state.go @@ -276,27 +276,6 @@ func (db *DeploymentState) CompleteVersion(ctx context.Context, success bool) (b return success, nil } -// RecordResourceHistory publishes an operation's state (nil for removal) without writing local state. -func (db *DeploymentState) RecordResourceHistory(ctx context.Context, resourceKey string, entry *ResourceEntry) error { - db.AssertOpenedForWrite() - buf := db.getOperationBuffer() - if buf == nil { - return nil - } - var recorded json.RawMessage - var resourceID string - if entry != nil { - var err error - recorded, err = json.Marshal(RecordedState{State: entry.State, DependsOn: entry.DependsOn}) - if err != nil { - return err - } - resourceID = entry.ID - } - buf.RecordOperation(ctx, resourceKey, false, resourceID, recorded) - return nil -} - // RecordFailure records that a resource did not apply, so the history says why rather than // leaving the resource out. resourceID is the id it had before the failure. func (db *DeploymentState) RecordFailure(resourceKey, resourceID string, cause error) { diff --git a/bundle/phases/bind.go b/bundle/phases/bind.go index fdf939e2ace..327baf15254 100644 --- a/bundle/phases/bind.go +++ b/bundle/phases/bind.go @@ -7,7 +7,9 @@ import ( "fmt" "maps" "slices" + "strconv" "strings" + "time" "github.com/databricks/cli/bundle" "github.com/databricks/cli/bundle/config/engine" @@ -15,13 +17,15 @@ import ( "github.com/databricks/cli/bundle/deploy/snapshot" "github.com/databricks/cli/bundle/deployplan" "github.com/databricks/cli/bundle/direct/dstate" + bundleenv "github.com/databricks/cli/bundle/env" "github.com/databricks/cli/bundle/statemgmt" + "github.com/databricks/cli/internal/build" "github.com/databricks/cli/libs/agent" - "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/dms" "github.com/databricks/cli/libs/log" "github.com/databricks/cli/libs/logdiag" + "github.com/databricks/cli/libs/retry" "github.com/databricks/databricks-sdk-go/service/bundledeployments" ) @@ -34,10 +38,7 @@ type BindOptions struct { } // createBindingDeployment registers a first binding without changing metadata from an existing deploy. -func createBindingDeployment(ctx context.Context, b *bundle.Bundle, current *bundledeployments.Deployment) error { - if current != nil { - return nil - } +func createBindingDeployment(ctx context.Context, b *bundle.Bundle) (string, error) { // Bind does not upload files or stage an immutable snapshot, so it cannot report a deployed file path. created, err := b.WorkspaceClient(ctx).BundleDeployments.CreateDeployment(ctx, bundledeployments.CreateDeploymentRequest{ Deployment: bundledeployments.Deployment{ @@ -49,14 +50,13 @@ func createBindingDeployment(ctx context.Context, b *bundle.Bundle, current *bun }, }) if err != nil { - return fmt.Errorf("failed to create deployment: %w", err) + return "", fmt.Errorf("failed to create deployment: %w", err) } deploymentID, err := dms.DeploymentIDFromName(created.Name) if err != nil { - return fmt.Errorf("failed to create deployment: %w", err) + return "", fmt.Errorf("failed to create deployment: %w", err) } - b.DeploymentBundle.StateDB.DeploymentID = deploymentID - return nil + return deploymentID, nil } func Bind(ctx context.Context, b *bundle.Bundle, opts *BindOptions, stateDesc *statemgmt.StateDesc) { @@ -68,9 +68,10 @@ func Bind(ctx context.Context, b *bundle.Bundle, opts *BindOptions, stateDesc *s return } - historySucceeded := true + var historyVersion string + var historyErr error defer func() { - completeBindingHistory(ctx, b, historySucceeded && !logdiag.HasError(ctx)) + completeBindingHistory(ctx, b, historyVersion, historyErr == nil && !logdiag.HasError(ctx)) bundle.ApplyContext(ctx, b, lock.Release(lock.GoalBind)) }() @@ -147,21 +148,28 @@ func Bind(ctx context.Context, b *bundle.Bundle, opts *BindOptions, stateDesc *s return } - if err := recordBindHistory(ctx, b, resourceKey); err != nil { - historySucceeded = false - log.Warnf(ctx, "Failed to record bind history: %s", err) + historyVersion, historyErr = recordBindHistory(ctx, b, resourceKey) + if historyErr != nil { + log.Warnf(ctx, "Failed to record bind history: %s", historyErr) } statemgmt.PushResourcesState(ctx, b) } // completeBindingHistory completes the recorded version after the remote state upload. -func completeBindingHistory(ctx context.Context, b *bundle.Bundle, success bool) { - if !b.DeploymentBundle.StateDB.RecordsHistory() { +func completeBindingHistory(ctx context.Context, b *bundle.Bundle, versionName string, success bool) { + if versionName == "" { return } - completionCtx := cmdctx.SetWorkspaceClient(ctx, b.WorkspaceClient(ctx)) - if _, err := b.DeploymentBundle.StateDB.CompleteVersion(completionCtx, success); err != nil { + reason := bundledeployments.VersionCompleteVersionCompleteFailure + if success { + reason = bundledeployments.VersionCompleteVersionCompleteSuccess + } + _, err := b.WorkspaceClient(ctx).BundleDeployments.CompleteVersion(ctx, bundledeployments.CompleteVersionRequest{ + Name: versionName, + CompletionReason: reason, + }) + if err != nil { log.Warnf(ctx, "Failed to complete deployment history version: %s", err) } } @@ -205,50 +213,103 @@ func Unbind(ctx context.Context, b *bundle.Bundle, group, resourceKey string, en statemgmt.PushResourcesState(ctx, b) } -// recordBindHistory records a committed binding's saved state when history recording is enabled. -func recordBindHistory(ctx context.Context, b *bundle.Bundle, resourceKey string) (err error) { +// recordBindHistory publishes a committed binding to DMS without changing local state. +func recordBindHistory(ctx context.Context, b *bundle.Bundle, resourceKey string) (versionName string, err error) { db := &b.DeploymentBundle.StateDB _, statePath := b.StateFilenameDirect(ctx) if err := db.Open(ctx, statePath, dstate.ReadingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{}); err != nil { - return err + return "", err } defer func() { _, finalizeErr := db.Finalize(ctx) err = errors.Join(err, finalizeErr) }() - if !db.RecordsHistory() { - return nil + var configured *bool + if b.Config.Experimental != nil { + configured = b.Config.Experimental.DeploymentHistory + } + if !db.RecordsHistory() && !bundleenv.RecordsDeploymentHistory(ctx, configured) { + return "", nil } deploymentID, current, err := dms.LookupDeployment(ctx, b.WorkspaceClient(ctx), b.Config.Workspace.StatePath) if err != nil { - return err + return "", err } - db.DeploymentID = deploymentID - db.VersionID, err = dms.ParseLastVersionID(current) + previous, err := dms.ParseLastVersionID(current) if err != nil { - return err + return "", err } - - if err := db.UpgradeToWrite(); err != nil { - return err + if current == nil { + deploymentID, err = createBindingDeployment(ctx, b) + if err != nil { + return "", err + } } - if !cmdctx.HasWorkspaceClient(ctx) { - ctx = cmdctx.SetWorkspaceClient(ctx, b.WorkspaceClient(ctx)) + versionID := max(previous, db.Data.Serial) + 1 + previousVersionID := "" + if previous > 0 { + previousVersionID = strconv.Itoa(previous) } - if err := createBindingDeployment(ctx, b, current); err != nil { - return err + var gitInfo *bundledeployments.GitInfo + if git := b.Config.Bundle.Git; git.Branch != "" || git.Commit != "" || git.OriginURL != "" { + gitInfo = &bundledeployments.GitInfo{Branch: git.Branch, Commit: git.Commit, OriginUrl: git.OriginURL} } - if err := startVersion(ctx, b, dms.VersionTypeDeploy, []bundledeployments.StagedOperation{{ - ResourceKey: strings.TrimPrefix(resourceKey, dms.StatePrefix), - ActionType: bundledeployments.OperationActionTypeOperationActionTypeBind, - }}); err != nil { - return err + version, err := b.WorkspaceClient(ctx).BundleDeployments.CreateVersion(ctx, bundledeployments.CreateVersionRequest{ + Parent: dms.DeploymentName(deploymentID), + VersionId: strconv.Itoa(versionID), + Version: bundledeployments.Version{ + CliVersion: build.GetInfo().Version, + VersionType: dms.VersionTypeDeploy, + PreviousVersionId: previousVersionID, + GitInfo: gitInfo, + Operations: []bundledeployments.StagedOperation{{ + ResourceKey: strings.TrimPrefix(resourceKey, dms.StatePrefix), + ActionType: bundledeployments.OperationActionTypeOperationActionTypeBind, + }}, + }, + }) + if err != nil { + return "", fmt.Errorf("failed to create deployment version: %w", err) } + versionName = version.Name entry, ok := db.GetResourceEntry(resourceKey) if !ok { - return fmt.Errorf("bound resource %s is missing from deployment state", resourceKey) + return versionName, fmt.Errorf("bound resource %s is missing from deployment state", resourceKey) } - return db.RecordResourceHistory(ctx, resourceKey, &entry) + return versionName, recordBindingOperation(ctx, b, versionName, resourceKey, &entry) +} + +// recordBindingOperation sends an operation's saved state, or its removal, directly to DMS. +func recordBindingOperation(ctx context.Context, b *bundle.Bundle, versionName, resourceKey string, entry *dstate.ResourceEntry) error { + ctx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + var state json.RawMessage + var resourceID string + if entry != nil { + var err error + state, err = json.Marshal(dstate.RecordedState{State: entry.State, DependsOn: entry.DependsOn}) + if err != nil { + return err + } + resourceID = entry.ID + } + update, err := dms.NewStateUpdate(resourceID, state, false) + if err != nil { + return err + } + _, err = retry.With(ctx, retry.IsTransient, func() (*bundledeployments.Operation, error) { + return b.WorkspaceClient(ctx).BundleDeployments.UpdateOperation(ctx, bundledeployments.UpdateOperationRequest{ + Name: versionName + "/operations/" + strings.TrimPrefix(resourceKey, dms.StatePrefix), + Operation: bundledeployments.Operation{ + State: string(update.State), + ResourceId: update.ResourceID, + Status: update.Status, + ForceSendFields: []string{"SequenceId", "ErrorMessage"}, + }, + UpdateMask: update.Fields.Mask(), + }) + }) + return err } From 783f794c848a0ca90571ddd9d99b5f2a4bc65c0e Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Tue, 10 Nov 2026 09:06:46 +0000 Subject: [PATCH 23/29] Remove redundant version parsing unit coverage --- libs/dms/metadata_test.go | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/libs/dms/metadata_test.go b/libs/dms/metadata_test.go index 799a910b273..5122533dafc 100644 --- a/libs/dms/metadata_test.go +++ b/libs/dms/metadata_test.go @@ -3,7 +3,6 @@ package dms import ( "testing" - "github.com/databricks/databricks-sdk-go/service/bundledeployments" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -42,23 +41,3 @@ func TestDeploymentIDFromName(t *testing.T) { _, err = DeploymentIDFromName("deployments/") assert.Error(t, err) } - -func TestParseLastVersionID(t *testing.T) { - for _, tc := range []struct { - name string - deployment *bundledeployments.Deployment - want int - }{ - {"no deployment", nil, 0}, - {"no version", &bundledeployments.Deployment{}, 0}, - {"existing version", &bundledeployments.Deployment{LastVersionId: "4"}, 4}, - } { - t.Run(tc.name, func(t *testing.T) { - got, err := ParseLastVersionID(tc.deployment) - require.NoError(t, err) - assert.Equal(t, tc.want, got) - }) - } - _, err := ParseLastVersionID(&bundledeployments.Deployment{LastVersionId: "invalid"}) - assert.ErrorContains(t, err, "last_version_id") -} From 076088c01dba08320c4f3a7ea163fc01720e916c Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Tue, 10 Nov 2026 09:35:22 +0000 Subject: [PATCH 24/29] Pass binding state through the bind result for history --- bundle/direct/bind.go | 15 +++++++++++++++ bundle/phases/bind.go | 40 ++++++++++++++++++---------------------- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/bundle/direct/bind.go b/bundle/direct/bind.go index 73852cdde00..0efb96b6621 100644 --- a/bundle/direct/bind.go +++ b/bundle/direct/bind.go @@ -35,8 +35,16 @@ func (e ErrResourceAlreadyBound) Error() string { e.ResourceKey, e.ExistingID) } +// BindingState carries the resulting bindings and file metadata; nil entries represent removals. +type BindingState struct { + Serial int + RecordsHistory bool + Resources map[string]*dstate.ResourceEntry +} + // BindResult contains the result of a bind operation including any detected changes. type BindResult struct { + State BindingState // HasChanges is true if deploying after bind would make changes to the resource HasChanges bool // Action is the planned action for the bound resource (e.g., "skip", "update", "recreate") @@ -197,6 +205,12 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac os.Remove(tmpStatePath) return nil, err } + resourceState, _ := b.StateDB.GetResourceEntry(resourceKey) + state := BindingState{ + Serial: b.StateDB.Data.Serial, + RecordsHistory: b.StateDB.RecordsHistory(), + Resources: map[string]*dstate.ResourceEntry{resourceKey: &resourceState}, + } plan, err = b.CalculatePlan(ctx, client, configRoot) if _, ferr := b.StateDB.Finalize(ctx); ferr != nil { log.Warnf(ctx, "failed to finalize state: %v", ferr) @@ -208,6 +222,7 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac // Check if the bound resource has changes result := &BindResult{ + State: state, HasChanges: false, Action: deployplan.Skip, Plan: plan, diff --git a/bundle/phases/bind.go b/bundle/phases/bind.go index 327baf15254..c58daf05f51 100644 --- a/bundle/phases/bind.go +++ b/bundle/phases/bind.go @@ -16,6 +16,7 @@ import ( "github.com/databricks/cli/bundle/deploy/lock" "github.com/databricks/cli/bundle/deploy/snapshot" "github.com/databricks/cli/bundle/deployplan" + "github.com/databricks/cli/bundle/direct" "github.com/databricks/cli/bundle/direct/dstate" bundleenv "github.com/databricks/cli/bundle/env" "github.com/databricks/cli/bundle/statemgmt" @@ -148,7 +149,7 @@ func Bind(ctx context.Context, b *bundle.Bundle, opts *BindOptions, stateDesc *s return } - historyVersion, historyErr = recordBindHistory(ctx, b, resourceKey) + historyVersion, historyErr = recordBindHistory(ctx, b, result.State) if historyErr != nil { log.Warnf(ctx, "Failed to record bind history: %s", historyErr) } @@ -214,22 +215,12 @@ func Unbind(ctx context.Context, b *bundle.Bundle, group, resourceKey string, en } // recordBindHistory publishes a committed binding to DMS without changing local state. -func recordBindHistory(ctx context.Context, b *bundle.Bundle, resourceKey string) (versionName string, err error) { - db := &b.DeploymentBundle.StateDB - _, statePath := b.StateFilenameDirect(ctx) - if err := db.Open(ctx, statePath, dstate.ReadingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{}); err != nil { - return "", err - } - defer func() { - _, finalizeErr := db.Finalize(ctx) - err = errors.Join(err, finalizeErr) - }() - +func recordBindHistory(ctx context.Context, b *bundle.Bundle, state direct.BindingState) (versionName string, err error) { var configured *bool if b.Config.Experimental != nil { configured = b.Config.Experimental.DeploymentHistory } - if !db.RecordsHistory() && !bundleenv.RecordsDeploymentHistory(ctx, configured) { + if !state.RecordsHistory && !bundleenv.RecordsDeploymentHistory(ctx, configured) { return "", nil } @@ -247,7 +238,7 @@ func recordBindHistory(ctx context.Context, b *bundle.Bundle, resourceKey string return "", err } } - versionID := max(previous, db.Data.Serial) + 1 + versionID := max(previous, state.Serial) + 1 previousVersionID := "" if previous > 0 { previousVersionID = strconv.Itoa(previous) @@ -256,6 +247,14 @@ func recordBindHistory(ctx context.Context, b *bundle.Bundle, resourceKey string if git := b.Config.Bundle.Git; git.Branch != "" || git.Commit != "" || git.OriginURL != "" { gitInfo = &bundledeployments.GitInfo{Branch: git.Branch, Commit: git.Commit, OriginUrl: git.OriginURL} } + keys := slices.Sorted(maps.Keys(state.Resources)) + staged := make([]bundledeployments.StagedOperation, 0, len(keys)) + for _, key := range keys { + staged = append(staged, bundledeployments.StagedOperation{ + ResourceKey: strings.TrimPrefix(key, dms.StatePrefix), + ActionType: bundledeployments.OperationActionTypeOperationActionTypeBind, + }) + } version, err := b.WorkspaceClient(ctx).BundleDeployments.CreateVersion(ctx, bundledeployments.CreateVersionRequest{ Parent: dms.DeploymentName(deploymentID), VersionId: strconv.Itoa(versionID), @@ -264,21 +263,18 @@ func recordBindHistory(ctx context.Context, b *bundle.Bundle, resourceKey string VersionType: dms.VersionTypeDeploy, PreviousVersionId: previousVersionID, GitInfo: gitInfo, - Operations: []bundledeployments.StagedOperation{{ - ResourceKey: strings.TrimPrefix(resourceKey, dms.StatePrefix), - ActionType: bundledeployments.OperationActionTypeOperationActionTypeBind, - }}, + Operations: staged, }, }) if err != nil { return "", fmt.Errorf("failed to create deployment version: %w", err) } versionName = version.Name - entry, ok := db.GetResourceEntry(resourceKey) - if !ok { - return versionName, fmt.Errorf("bound resource %s is missing from deployment state", resourceKey) + var recordErr error + for _, key := range keys { + recordErr = errors.Join(recordErr, recordBindingOperation(ctx, b, versionName, key, state.Resources[key])) } - return versionName, recordBindingOperation(ctx, b, versionName, resourceKey, &entry) + return versionName, recordErr } // recordBindingOperation sends an operation's saved state, or its removal, directly to DMS. From de7b69def28671181479780a852d23215ed8a60d Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Tue, 10 Nov 2026 10:54:07 +0000 Subject: [PATCH 25/29] Make bind history metadata optional and DMS-specific --- bundle/direct/bind.go | 32 ++++++++++++++++++++------------ bundle/phases/bind.go | 42 ++++++++++++++++++------------------------ 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/bundle/direct/bind.go b/bundle/direct/bind.go index 0efb96b6621..0439adc6fde 100644 --- a/bundle/direct/bind.go +++ b/bundle/direct/bind.go @@ -10,6 +10,7 @@ import ( "github.com/databricks/cli/bundle/deployplan" "github.com/databricks/cli/bundle/direct/dresources" "github.com/databricks/cli/bundle/direct/dstate" + bundleenv "github.com/databricks/cli/bundle/env" "github.com/databricks/cli/libs/log" "github.com/databricks/cli/libs/structs/structaccess" "github.com/databricks/cli/libs/structs/structpath" @@ -35,16 +36,16 @@ func (e ErrResourceAlreadyBound) Error() string { e.ResourceKey, e.ExistingID) } -// BindingState carries the resulting bindings and file metadata; nil entries represent removals. -type BindingState struct { - Serial int - RecordsHistory bool - Resources map[string]*dstate.ResourceEntry +// DMSBindMetadata holds the bound resource state and serial to publish after bind is committed. +type DMSBindMetadata struct { + Serial int + ResourceState dstate.ResourceEntry } // BindResult contains the result of a bind operation including any detected changes. type BindResult struct { - State BindingState + // DMSMetadata is nil when deployment history is disabled. + DMSMetadata *DMSBindMetadata // HasChanges is true if deploying after bind would make changes to the resource HasChanges bool // Action is the planned action for the bound resource (e.g., "skip", "update", "recreate") @@ -205,11 +206,10 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac os.Remove(tmpStatePath) return nil, err } - resourceState, _ := b.StateDB.GetResourceEntry(resourceKey) - state := BindingState{ - Serial: b.StateDB.Data.Serial, - RecordsHistory: b.StateDB.RecordsHistory(), - Resources: map[string]*dstate.ResourceEntry{resourceKey: &resourceState}, + var dmsMetadata *DMSBindMetadata + if b.dmsMetadataEnabled(ctx, configRoot) { + resourceState, _ := b.StateDB.GetResourceEntry(resourceKey) + dmsMetadata = &DMSBindMetadata{Serial: b.StateDB.Data.Serial, ResourceState: resourceState} } plan, err = b.CalculatePlan(ctx, client, configRoot) if _, ferr := b.StateDB.Finalize(ctx); ferr != nil { @@ -222,7 +222,7 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac // Check if the bound resource has changes result := &BindResult{ - State: state, + DMSMetadata: dmsMetadata, HasChanges: false, Action: deployplan.Skip, Plan: plan, @@ -256,6 +256,14 @@ func (result *BindResult) Cancel() { } } +func (b *DeploymentBundle) dmsMetadataEnabled(ctx context.Context, configRoot *config.Root) bool { + var configured *bool + if configRoot.Experimental != nil { + configured = configRoot.Experimental.DeploymentHistory + } + return b.StateDB.RecordsHistory() || bundleenv.RecordsDeploymentHistory(ctx, configured) +} + // Unbind removes a resource from direct engine state without deleting // the workspace resource. Also removes associated permissions/grants entries. func (b *DeploymentBundle) Unbind(ctx context.Context, statePath, resourceKey string) error { diff --git a/bundle/phases/bind.go b/bundle/phases/bind.go index c58daf05f51..5d1dab93bf4 100644 --- a/bundle/phases/bind.go +++ b/bundle/phases/bind.go @@ -18,7 +18,6 @@ import ( "github.com/databricks/cli/bundle/deployplan" "github.com/databricks/cli/bundle/direct" "github.com/databricks/cli/bundle/direct/dstate" - bundleenv "github.com/databricks/cli/bundle/env" "github.com/databricks/cli/bundle/statemgmt" "github.com/databricks/cli/internal/build" "github.com/databricks/cli/libs/agent" @@ -149,7 +148,7 @@ func Bind(ctx context.Context, b *bundle.Bundle, opts *BindOptions, stateDesc *s return } - historyVersion, historyErr = recordBindHistory(ctx, b, result.State) + historyVersion, historyErr = recordBindHistory(ctx, b, resourceKey, result.DMSMetadata) if historyErr != nil { log.Warnf(ctx, "Failed to record bind history: %s", historyErr) } @@ -214,16 +213,23 @@ func Unbind(ctx context.Context, b *bundle.Bundle, group, resourceKey string, en statemgmt.PushResourcesState(ctx, b) } -// recordBindHistory publishes a committed binding to DMS without changing local state. -func recordBindHistory(ctx context.Context, b *bundle.Bundle, state direct.BindingState) (versionName string, err error) { - var configured *bool - if b.Config.Experimental != nil { - configured = b.Config.Experimental.DeploymentHistory - } - if !state.RecordsHistory && !bundleenv.RecordsDeploymentHistory(ctx, configured) { +// recordBindHistory publishes a committed binding when DMS metadata was requested. +func recordBindHistory(ctx context.Context, b *bundle.Bundle, resourceKey string, metadata *direct.DMSBindMetadata) (string, error) { + if metadata == nil { return "", nil } + versionName, err := createBindingVersion(ctx, b, metadata.Serial, []bundledeployments.StagedOperation{{ + ResourceKey: strings.TrimPrefix(resourceKey, dms.StatePrefix), + ActionType: bundledeployments.OperationActionTypeOperationActionTypeBind, + }}) + if err != nil { + return "", err + } + return versionName, recordBindingOperation(ctx, b, versionName, resourceKey, &metadata.ResourceState) +} +// createBindingVersion claims a history version for the staged binding operations. +func createBindingVersion(ctx context.Context, b *bundle.Bundle, serial int, staged []bundledeployments.StagedOperation) (string, error) { deploymentID, current, err := dms.LookupDeployment(ctx, b.WorkspaceClient(ctx), b.Config.Workspace.StatePath) if err != nil { return "", err @@ -238,7 +244,7 @@ func recordBindHistory(ctx context.Context, b *bundle.Bundle, state direct.Bindi return "", err } } - versionID := max(previous, state.Serial) + 1 + versionID := max(previous, serial) + 1 previousVersionID := "" if previous > 0 { previousVersionID = strconv.Itoa(previous) @@ -247,14 +253,6 @@ func recordBindHistory(ctx context.Context, b *bundle.Bundle, state direct.Bindi if git := b.Config.Bundle.Git; git.Branch != "" || git.Commit != "" || git.OriginURL != "" { gitInfo = &bundledeployments.GitInfo{Branch: git.Branch, Commit: git.Commit, OriginUrl: git.OriginURL} } - keys := slices.Sorted(maps.Keys(state.Resources)) - staged := make([]bundledeployments.StagedOperation, 0, len(keys)) - for _, key := range keys { - staged = append(staged, bundledeployments.StagedOperation{ - ResourceKey: strings.TrimPrefix(key, dms.StatePrefix), - ActionType: bundledeployments.OperationActionTypeOperationActionTypeBind, - }) - } version, err := b.WorkspaceClient(ctx).BundleDeployments.CreateVersion(ctx, bundledeployments.CreateVersionRequest{ Parent: dms.DeploymentName(deploymentID), VersionId: strconv.Itoa(versionID), @@ -269,12 +267,7 @@ func recordBindHistory(ctx context.Context, b *bundle.Bundle, state direct.Bindi if err != nil { return "", fmt.Errorf("failed to create deployment version: %w", err) } - versionName = version.Name - var recordErr error - for _, key := range keys { - recordErr = errors.Join(recordErr, recordBindingOperation(ctx, b, versionName, key, state.Resources[key])) - } - return versionName, recordErr + return version.Name, nil } // recordBindingOperation sends an operation's saved state, or its removal, directly to DMS. @@ -295,6 +288,7 @@ func recordBindingOperation(ctx context.Context, b *bundle.Bundle, versionName, if err != nil { return err } + // Newly staged operations start at sequence 0; DMS requires it even though Go omits zero values. _, err = retry.With(ctx, retry.IsTransient, func() (*bundledeployments.Operation, error) { return b.WorkspaceClient(ctx).BundleDeployments.UpdateOperation(ctx, bundledeployments.UpdateOperationRequest{ Name: versionName + "/operations/" + strings.TrimPrefix(resourceKey, dms.StatePrefix), From 93d3dcf7a6f374eba64c935eba39e54d49d780fa Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Tue, 10 Nov 2026 11:09:40 +0000 Subject: [PATCH 26/29] Inline bind history metadata selection --- bundle/direct/bind.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/bundle/direct/bind.go b/bundle/direct/bind.go index 0439adc6fde..289b1076b29 100644 --- a/bundle/direct/bind.go +++ b/bundle/direct/bind.go @@ -206,8 +206,12 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac os.Remove(tmpStatePath) return nil, err } + var configured *bool + if configRoot.Experimental != nil { + configured = configRoot.Experimental.DeploymentHistory + } var dmsMetadata *DMSBindMetadata - if b.dmsMetadataEnabled(ctx, configRoot) { + if b.StateDB.RecordsHistory() || bundleenv.RecordsDeploymentHistory(ctx, configured) { resourceState, _ := b.StateDB.GetResourceEntry(resourceKey) dmsMetadata = &DMSBindMetadata{Serial: b.StateDB.Data.Serial, ResourceState: resourceState} } @@ -256,14 +260,6 @@ func (result *BindResult) Cancel() { } } -func (b *DeploymentBundle) dmsMetadataEnabled(ctx context.Context, configRoot *config.Root) bool { - var configured *bool - if configRoot.Experimental != nil { - configured = configRoot.Experimental.DeploymentHistory - } - return b.StateDB.RecordsHistory() || bundleenv.RecordsDeploymentHistory(ctx, configured) -} - // Unbind removes a resource from direct engine state without deleting // the workspace resource. Also removes associated permissions/grants entries. func (b *DeploymentBundle) Unbind(ctx context.Context, statePath, resourceKey string) error { From 3456f0ac89bc6a5cd0645eecd5599250273b1543 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Tue, 10 Nov 2026 12:09:45 +0000 Subject: [PATCH 27/29] Fix moved parser test and stabilize cloud bind preview --- acceptance/bundle/dms/bind-unbind/output.txt | 3 --- acceptance/bundle/dms/bind-unbind/script | 3 ++- cmd/bundle/utils/process_test.go | 3 ++- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/acceptance/bundle/dms/bind-unbind/output.txt b/acceptance/bundle/dms/bind-unbind/output.txt index 38d79d119fb..922b2985955 100644 --- a/acceptance/bundle/dms/bind-unbind/output.txt +++ b/acceptance/bundle/dms/bind-unbind/output.txt @@ -6,10 +6,7 @@ Plan: update resources.jobs.foo Changes detected: ~ deployment: null -> {"kind":"BUNDLE","metadata_file_path":"/Workspace/Users/[USERNAME]/.bundle/dms-bind-unbind-[UNIQUE_NAME]/default/state/metadata.json"} ~ edit_mode: null -> "UI_LOCKED" - ~ format: null -> "MULTI_TASK" - ~ max_concurrent_runs: null -> 1 ~ name: "bind-target-[UNIQUE_NAME]" -> "foo" - ~ queue: null -> {"enabled":true} Error: this bind operation requires user confirmation, but the current console does not support prompting. To proceed, use --auto-approve after reviewing the plan above. diff --git a/acceptance/bundle/dms/bind-unbind/script b/acceptance/bundle/dms/bind-unbind/script index 36c97bf8c3d..8c7d75c3d43 100644 --- a/acceptance/bundle/dms/bind-unbind/script +++ b/acceptance/bundle/dms/bind-unbind/script @@ -1,7 +1,8 @@ envsubst < databricks.yml.tmpl > databricks.yml title "Bind an existing job with deployment history enabled" -job_id=$($CLI jobs create --json '{"name": "bind-target-'$UNIQUE_NAME'"}' | jq -r '.job_id') +# Match Jobs API defaults so the bind preview is identical locally and on cloud. +job_id=$($CLI jobs create --json '{"name": "bind-target-'$UNIQUE_NAME'", "format": "MULTI_TASK", "max_concurrent_runs": 1, "queue": {"enabled": true}}' | jq -r '.job_id') add_repl "$job_id" JOB_ID cleanup_bind_target() { errcode $CLI bundle destroy --auto-approve >> LOG.cleanup 2>&1 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 From 8177299bb652c385a1e871241b3025b04f71493e Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Fri, 13 Nov 2026 12:55:51 +0000 Subject: [PATCH 28/29] Record binds as updates through the shared DMS buffer --- .../dms/bind-history-failures/output.txt | 16 +++- .../bundle/dms/bind-immutable/output.txt | 2 +- acceptance/bundle/dms/bind-unbind/output.txt | 12 ++- bundle/phases/bind.go | 93 ++++++++----------- 4 files changed, 61 insertions(+), 62 deletions(-) diff --git a/acceptance/bundle/dms/bind-history-failures/output.txt b/acceptance/bundle/dms/bind-history-failures/output.txt index 8a2cf5d20f8..dd3a75221c9 100644 --- a/acceptance/bundle/dms/bind-history-failures/output.txt +++ b/acceptance/bundle/dms/bind-history-failures/output.txt @@ -1,5 +1,6 @@ === A version creation failure does not prevent binding + >>> fault.py POST /api/2.0/bundle/deployments/*/versions 400 0 1 >>> [CLI] bundle deployment bind create_failed [JOB_ID] --auto-approve @@ -8,14 +9,16 @@ Successfully bound job with an id '[JOB_ID]' Run 'bundle deploy' to deploy changes to your workspace === An operation recording failure does not prevent binding + >>> fault.py PATCH /api/2.0/bundle/** 400 0 1 >>> [CLI] bundle deployment bind record_failed [JOB_ID] --auto-approve -Warn: Failed to record bind history: Fault injected by test. +Warn: Failed to record bind history: recording operation for resources.jobs.record_failed: Fault injected by test. Successfully bound job with an id '[JOB_ID]' Run 'bundle deploy' to deploy changes to your workspace === A version completion failure does not prevent binding + >>> fault.py POST /api/2.0/bundle/deployments/*/versions/*/complete 400 0 1 >>> [CLI] bundle deployment bind complete_failed [JOB_ID] --auto-approve @@ -24,6 +27,7 @@ Successfully bound job with an id '[JOB_ID]' Run 'bundle deploy' to deploy changes to your workspace === All bindings survived the remote state round trips + >>> jq .state | map_values(.__id__) .databricks/bundle/default/resources.json { "resources.jobs.complete_failed": "[JOB_ID]", @@ -38,15 +42,16 @@ Run 'bundle deploy' to deploy changes to your workspace } >>> print_requests.py --dms //versions --oneline -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "3"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.create_failed"}], "version_type": "VERSION_TYPE_DEPLOY"}} -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "5"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.record_failed"}], "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "3"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_UPDATE", "resource_key": "jobs.create_failed"}], "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "5"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_UPDATE", "resource_key": "jobs.record_failed"}], "version_type": "VERSION_TYPE_DEPLOY"}} {"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/5/operations/jobs.record_failed", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-history-failures-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"record_failed\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} {"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/5/complete", "body": {"completion_reason": "VERSION_COMPLETE_FAILURE"}} -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "7"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.complete_failed"}], "previous_version_id": "5", "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "7"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_UPDATE", "resource_key": "jobs.complete_failed"}], "previous_version_id": "5", "version_type": "VERSION_TYPE_DEPLOY"}} {"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/7/operations/jobs.complete_failed", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-history-failures-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"complete_failed\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} {"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/7/complete", "body": {"completion_reason": "VERSION_COMPLETE_SUCCESS"}} === A remote state upload failure fails the bind version + >>> fault.py POST /api/2.0/workspace-files/import-file/**resources.json 400 0 1 >>> musterr [CLI] bundle deployment bind upload_failed [JOB_ID] --auto-approve @@ -59,11 +64,12 @@ API message: Fault injected by test. >>> print_requests.py --dms //versions --oneline -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "9"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.upload_failed"}], "previous_version_id": "7", "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "9"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_UPDATE", "resource_key": "jobs.upload_failed"}], "previous_version_id": "7", "version_type": "VERSION_TYPE_DEPLOY"}} {"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/9/operations/jobs.upload_failed", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-history-failures-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"upload_failed\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} {"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/9/complete", "body": {"completion_reason": "VERSION_COMPLETE_FAILURE"}} === Remote state still contains only the successful bindings + >>> [CLI] bundle summary -o json { "complete_failed": "[JOB_ID]", diff --git a/acceptance/bundle/dms/bind-immutable/output.txt b/acceptance/bundle/dms/bind-immutable/output.txt index 99e62a0e129..451affad5d7 100644 --- a/acceptance/bundle/dms/bind-immutable/output.txt +++ b/acceptance/bundle/dms/bind-immutable/output.txt @@ -6,7 +6,7 @@ Run 'bundle deploy' to deploy changes to your workspace >>> MSYS_NO_PATHCONV=1 [CLI] bundle-deployments list-resources deployments/[DEPLOYMENT_ID] -o json [ { - "last_action_type": "OPERATION_ACTION_TYPE_BIND", + "last_action_type": "OPERATION_ACTION_TYPE_UPDATE", "resource_id": "[JOB_ID]", "resource_key": "jobs.foo" } diff --git a/acceptance/bundle/dms/bind-unbind/output.txt b/acceptance/bundle/dms/bind-unbind/output.txt index 922b2985955..3c0d02308bf 100644 --- a/acceptance/bundle/dms/bind-unbind/output.txt +++ b/acceptance/bundle/dms/bind-unbind/output.txt @@ -1,5 +1,6 @@ === Bind an existing job with deployment history enabled + >>> musterr [CLI] bundle deployment bind foo [JOB_ID] Plan: update resources.jobs.foo @@ -19,7 +20,7 @@ Successfully bound job with an id '[JOB_ID]' Run 'bundle deploy' to deploy changes to your workspace >>> print_requests.py --dms //versions --oneline -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "3"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.foo"}], "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "3"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_UPDATE", "resource_key": "jobs.foo"}], "version_type": "VERSION_TYPE_DEPLOY"}} {"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/3/operations/jobs.foo", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-unbind-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"foo\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} {"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/3/complete", "body": {"completion_reason": "VERSION_COMPLETE_SUCCESS"}} @@ -32,13 +33,14 @@ Run 'bundle deploy' to deploy changes to your workspace >>> MSYS_NO_PATHCONV=1 [CLI] bundle-deployments list-resources deployments/[DEPLOYMENT_ID] -o json [ { - "last_action_type": "OPERATION_ACTION_TYPE_BIND", + "last_action_type": "OPERATION_ACTION_TYPE_UPDATE", "resource_id": "[JOB_ID]", "resource_key": "jobs.foo" } ] === Deploy keeps the bound job ID with recorded history + >>> [CLI] bundle deploy Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-bind-unbind-[UNIQUE_NAME]/default/files... Updated jobs.foo @@ -54,6 +56,7 @@ Resources: 0 created, 1 changed, 0 deleted, 0 unchanged } === Unbind preserves the shadow history marker and leaves the job intact + >>> [CLI] bundle deployment unbind foo >>> jq {features, state} .databricks/bundle/default/resources.json @@ -70,6 +73,7 @@ Resources: 0 created, 1 changed, 0 deleted, 0 unchanged } === Rebind preserves the existing shadow history marker + >>> [CLI] bundle deployment bind foo [JOB_ID] --auto-approve Successfully bound job with an id '[JOB_ID]' Run 'bundle deploy' to deploy changes to your workspace @@ -83,11 +87,12 @@ Run 'bundle deploy' to deploy changes to your workspace } >>> print_requests.py --dms //versions --oneline -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "7"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_BIND", "resource_key": "jobs.foo"}], "previous_version_id": "4", "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "7"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_UPDATE", "resource_key": "jobs.foo"}], "previous_version_id": "4", "version_type": "VERSION_TYPE_DEPLOY"}} {"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/7/operations/jobs.foo", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-unbind-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"foo\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} {"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/7/complete", "body": {"completion_reason": "VERSION_COMPLETE_SUCCESS"}} === A subsequent deploy continues managing the same job + >>> [CLI] bundle deploy Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-bind-unbind-[UNIQUE_NAME]/default/files... Files: 0 uploaded, 0 deleted @@ -102,6 +107,7 @@ Resources: 0 created, 0 changed, 0 deleted, 1 unchanged } === A rejected duplicate bind creates no history version + >>> musterr [CLI] bundle deployment bind foo [JOB_ID] --auto-approve Error: Resource already managed diff --git a/bundle/phases/bind.go b/bundle/phases/bind.go index 5d1dab93bf4..fb9a1f316be 100644 --- a/bundle/phases/bind.go +++ b/bundle/phases/bind.go @@ -9,7 +9,6 @@ import ( "slices" "strconv" "strings" - "time" "github.com/databricks/cli/bundle" "github.com/databricks/cli/bundle/config/engine" @@ -21,11 +20,11 @@ import ( "github.com/databricks/cli/bundle/statemgmt" "github.com/databricks/cli/internal/build" "github.com/databricks/cli/libs/agent" + "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/dms" "github.com/databricks/cli/libs/log" "github.com/databricks/cli/libs/logdiag" - "github.com/databricks/cli/libs/retry" "github.com/databricks/databricks-sdk-go/service/bundledeployments" ) @@ -37,6 +36,12 @@ type BindOptions struct { ResourceId string } +// bindingHistory keeps the recorder alive through the remote state upload. +type bindingHistory struct { + name string + buffer *dms.OperationBuffer +} + // createBindingDeployment registers a first binding without changing metadata from an existing deploy. func createBindingDeployment(ctx context.Context, b *bundle.Bundle) (string, error) { // Bind does not upload files or stage an immutable snapshot, so it cannot report a deployed file path. @@ -68,10 +73,10 @@ func Bind(ctx context.Context, b *bundle.Bundle, opts *BindOptions, stateDesc *s return } - var historyVersion string + var history *bindingHistory var historyErr error defer func() { - completeBindingHistory(ctx, b, historyVersion, historyErr == nil && !logdiag.HasError(ctx)) + completeBindingHistory(ctx, b, history, historyErr == nil && !logdiag.HasError(ctx)) bundle.ApplyContext(ctx, b, lock.Release(lock.GoalBind)) }() @@ -148,7 +153,7 @@ func Bind(ctx context.Context, b *bundle.Bundle, opts *BindOptions, stateDesc *s return } - historyVersion, historyErr = recordBindHistory(ctx, b, resourceKey, result.DMSMetadata) + history, historyErr = recordBindHistory(ctx, b, resourceKey, result.DMSMetadata) if historyErr != nil { log.Warnf(ctx, "Failed to record bind history: %s", historyErr) } @@ -157,16 +162,17 @@ func Bind(ctx context.Context, b *bundle.Bundle, opts *BindOptions, stateDesc *s } // completeBindingHistory completes the recorded version after the remote state upload. -func completeBindingHistory(ctx context.Context, b *bundle.Bundle, versionName string, success bool) { - if versionName == "" { +func completeBindingHistory(ctx context.Context, b *bundle.Bundle, history *bindingHistory, success bool) { + if history == nil { return } + history.buffer.Stop() reason := bundledeployments.VersionCompleteVersionCompleteFailure if success { reason = bundledeployments.VersionCompleteVersionCompleteSuccess } _, err := b.WorkspaceClient(ctx).BundleDeployments.CompleteVersion(ctx, bundledeployments.CompleteVersionRequest{ - Name: versionName, + Name: history.name, CompletionReason: reason, }) if err != nil { @@ -214,34 +220,40 @@ func Unbind(ctx context.Context, b *bundle.Bundle, group, resourceKey string, en } // recordBindHistory publishes a committed binding when DMS metadata was requested. -func recordBindHistory(ctx context.Context, b *bundle.Bundle, resourceKey string, metadata *direct.DMSBindMetadata) (string, error) { +func recordBindHistory(ctx context.Context, b *bundle.Bundle, resourceKey string, metadata *direct.DMSBindMetadata) (*bindingHistory, error) { if metadata == nil { - return "", nil + return nil, nil + } + entry := metadata.ResourceState + state, err := json.Marshal(dstate.RecordedState{State: entry.State, DependsOn: entry.DependsOn}) + if err != nil { + return nil, err } - versionName, err := createBindingVersion(ctx, b, metadata.Serial, []bundledeployments.StagedOperation{{ + history, err := createBindingVersion(ctx, b, metadata.Serial, []bundledeployments.StagedOperation{{ ResourceKey: strings.TrimPrefix(resourceKey, dms.StatePrefix), - ActionType: bundledeployments.OperationActionTypeOperationActionTypeBind, + ActionType: bundledeployments.OperationActionTypeOperationActionTypeUpdate, }}) if err != nil { - return "", err + return nil, err } - return versionName, recordBindingOperation(ctx, b, versionName, resourceKey, &metadata.ResourceState) + history.buffer.RecordOperation(ctx, resourceKey, false, entry.ID, state) + return history, history.buffer.Drain(ctx) } // createBindingVersion claims a history version for the staged binding operations. -func createBindingVersion(ctx context.Context, b *bundle.Bundle, serial int, staged []bundledeployments.StagedOperation) (string, error) { +func createBindingVersion(ctx context.Context, b *bundle.Bundle, serial int, staged []bundledeployments.StagedOperation) (*bindingHistory, error) { deploymentID, current, err := dms.LookupDeployment(ctx, b.WorkspaceClient(ctx), b.Config.Workspace.StatePath) if err != nil { - return "", err + return nil, err } previous, err := dms.ParseLastVersionID(current) if err != nil { - return "", err + return nil, err } if current == nil { deploymentID, err = createBindingDeployment(ctx, b) if err != nil { - return "", err + return nil, err } } versionID := max(previous, serial) + 1 @@ -265,41 +277,16 @@ func createBindingVersion(ctx context.Context, b *bundle.Bundle, serial int, sta }, }) if err != nil { - return "", fmt.Errorf("failed to create deployment version: %w", err) + return nil, fmt.Errorf("failed to create deployment version: %w", err) } - return version.Name, nil -} - -// recordBindingOperation sends an operation's saved state, or its removal, directly to DMS. -func recordBindingOperation(ctx context.Context, b *bundle.Bundle, versionName, resourceKey string, entry *dstate.ResourceEntry) error { - ctx, cancel := context.WithTimeout(ctx, 5*time.Second) - defer cancel() - var state json.RawMessage - var resourceID string - if entry != nil { - var err error - state, err = json.Marshal(dstate.RecordedState{State: entry.State, DependsOn: entry.DependsOn}) - if err != nil { - return err - } - resourceID = entry.ID + if !cmdctx.HasWorkspaceClient(ctx) { + ctx = cmdctx.SetWorkspaceClient(ctx, b.WorkspaceClient(ctx)) } - update, err := dms.NewStateUpdate(resourceID, state, false) - if err != nil { - return err - } - // Newly staged operations start at sequence 0; DMS requires it even though Go omits zero values. - _, err = retry.With(ctx, retry.IsTransient, func() (*bundledeployments.Operation, error) { - return b.WorkspaceClient(ctx).BundleDeployments.UpdateOperation(ctx, bundledeployments.UpdateOperationRequest{ - Name: versionName + "/operations/" + strings.TrimPrefix(resourceKey, dms.StatePrefix), - Operation: bundledeployments.Operation{ - State: string(update.State), - ResourceId: update.ResourceID, - Status: update.Status, - ForceSendFields: []string{"SequenceId", "ErrorMessage"}, - }, - UpdateMask: update.Fields.Mask(), - }) - }) - return err + return &bindingHistory{ + name: version.Name, + buffer: dms.StartOperationBuffer(ctx, deploymentID, versionID, dms.BufferOptions{ + BestEffort: true, + StagedOperationCount: len(staged), + }), + }, nil } From 6da40a68a1d66cb1999c7dc62c2fa66c45a5a12e Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 16 Nov 2026 04:07:05 +0000 Subject: [PATCH 29/29] Commit approved binds through the StateDB history lifecycle --- .../recreation/out.state_after_bind.json | 2 +- .../bind/feature_spec/out.bind.plain.txt | 4 + .../bind/feature_spec/out.bind.recorded.txt | 5 + .../bind/feature_spec/out.test.toml | 1 + .../bind/feature_spec/out.unbind.plain.txt | 2 + .../bind/feature_spec/out.unbind.recorded.txt | 2 + .../deployment/bind/feature_spec/output.txt | 6 - .../deployment/bind/feature_spec/script | 4 +- .../deployment/bind/feature_spec/test.toml | 6 + .../bind/job/job-abort-bind/output.txt | 2 + .../deployment/bind/job/job-abort-bind/script | 1 + .../deployment/bind/job/noop-job/output.txt | 8 + .../deployment/bind/job/noop-job/script | 2 + .../dms/bind-history-failures/output.txt | 22 +- acceptance/bundle/dms/bind-unbind/output.txt | 27 ++- acceptance/bundle/dms/bind-unbind/script | 6 +- .../dms_fault/complete_version/output.txt | 6 +- .../dms_fault/create_deployment/output.txt | 2 + .../dms_fault/create_version/output.txt | 2 + .../dms_fault/delete_deployment/output.txt | 4 +- .../dms_fault/get_deployment/output.txt | 10 +- .../bundle/dms_fault/get_operation/output.txt | 4 +- .../bundle/dms_fault/get_resource/output.txt | 4 +- .../bundle/dms_fault/get_version/output.txt | 4 +- .../bundle/dms_fault/heartbeat/output.txt | 4 +- .../dms_fault/list_deployments/output.txt | 4 +- .../dms_fault/list_operations/output.txt | 4 +- .../dms_fault/list_resources/output.txt | 4 +- .../bundle/dms_fault/list_versions/output.txt | 4 +- .../dms_fault/update_operation/output.txt | 6 +- bundle/direct/bind.go | 198 ++++-------------- bundle/direct/bind_test.go | 86 ++++++++ bundle/phases/bind.go | 147 ++++--------- bundle/phases/bind_test.go | 97 +++++++++ 34 files changed, 383 insertions(+), 307 deletions(-) create mode 100644 acceptance/bundle/deployment/bind/feature_spec/out.bind.plain.txt create mode 100644 acceptance/bundle/deployment/bind/feature_spec/out.bind.recorded.txt create mode 100644 acceptance/bundle/deployment/bind/feature_spec/out.unbind.plain.txt create mode 100644 acceptance/bundle/deployment/bind/feature_spec/out.unbind.recorded.txt create mode 100644 bundle/direct/bind_test.go create mode 100644 bundle/phases/bind_test.go diff --git a/acceptance/bundle/deployment/bind/dashboard/recreation/out.state_after_bind.json b/acceptance/bundle/deployment/bind/dashboard/recreation/out.state_after_bind.json index 05614d798bf..74e555c734d 100644 --- a/acceptance/bundle/deployment/bind/dashboard/recreation/out.state_after_bind.json +++ b/acceptance/bundle/deployment/bind/dashboard/recreation/out.state_after_bind.json @@ -2,7 +2,7 @@ "state_version": 3, "cli_version": "[CLI_VERSION]", "lineage": "[UUID]", - "serial": 2, + "serial": 1, "state": { "resources.dashboards.dashboard1": { "__id__": "[DASHBOARD_ID]", diff --git a/acceptance/bundle/deployment/bind/feature_spec/out.bind.plain.txt b/acceptance/bundle/deployment/bind/feature_spec/out.bind.plain.txt new file mode 100644 index 00000000000..bfe657bfd50 --- /dev/null +++ b/acceptance/bundle/deployment/bind/feature_spec/out.bind.plain.txt @@ -0,0 +1,4 @@ + +>>> [CLI] bundle deployment bind spec1 main.fspec_[UNIQUE_NAME].boundspec --auto-approve +Successfully bound feature_spec with an id 'main.fspec_[UNIQUE_NAME].boundspec' +Run 'bundle deploy' to deploy changes to your workspace diff --git a/acceptance/bundle/deployment/bind/feature_spec/out.bind.recorded.txt b/acceptance/bundle/deployment/bind/feature_spec/out.bind.recorded.txt new file mode 100644 index 00000000000..55c6316629b --- /dev/null +++ b/acceptance/bundle/deployment/bind/feature_spec/out.bind.recorded.txt @@ -0,0 +1,5 @@ + +>>> [CLI] bundle deployment bind spec1 main.fspec_[UNIQUE_NAME].boundspec --auto-approve +Warn: Failed to record bind history: failed to create deployment version: operations.resource_key must have a known resource type prefix (e.g. 'jobs.', 'pipelines.'): feature_specs.spec1 +Successfully bound feature_spec with an id 'main.fspec_[UNIQUE_NAME].boundspec' +Run 'bundle deploy' to deploy changes to your workspace diff --git a/acceptance/bundle/deployment/bind/feature_spec/out.test.toml b/acceptance/bundle/deployment/bind/feature_spec/out.test.toml index 0f167effef1..efa0d67e8c2 100644 --- a/acceptance/bundle/deployment/bind/feature_spec/out.test.toml +++ b/acceptance/bundle/deployment/bind/feature_spec/out.test.toml @@ -1,2 +1,3 @@ Cloud = true EnvMatrix.DMS = ["", "true"] +EnvMatrix.OUTPUT_MODE = ["plain", "recorded"] diff --git a/acceptance/bundle/deployment/bind/feature_spec/out.unbind.plain.txt b/acceptance/bundle/deployment/bind/feature_spec/out.unbind.plain.txt new file mode 100644 index 00000000000..aa9e81e6253 --- /dev/null +++ b/acceptance/bundle/deployment/bind/feature_spec/out.unbind.plain.txt @@ -0,0 +1,2 @@ + +>>> [CLI] bundle deployment unbind spec1 diff --git a/acceptance/bundle/deployment/bind/feature_spec/out.unbind.recorded.txt b/acceptance/bundle/deployment/bind/feature_spec/out.unbind.recorded.txt new file mode 100644 index 00000000000..aa9e81e6253 --- /dev/null +++ b/acceptance/bundle/deployment/bind/feature_spec/out.unbind.recorded.txt @@ -0,0 +1,2 @@ + +>>> [CLI] bundle deployment unbind spec1 diff --git a/acceptance/bundle/deployment/bind/feature_spec/output.txt b/acceptance/bundle/deployment/bind/feature_spec/output.txt index a859db162c0..871f04d4ed8 100644 --- a/acceptance/bundle/deployment/bind/feature_spec/output.txt +++ b/acceptance/bundle/deployment/bind/feature_spec/output.txt @@ -16,10 +16,6 @@ "name": "main.fspec_[UNIQUE_NAME].boundspec" } ->>> [CLI] bundle deployment bind spec1 main.fspec_[UNIQUE_NAME].boundspec --auto-approve -Successfully bound feature_spec with an id 'main.fspec_[UNIQUE_NAME].boundspec' -Run 'bundle deploy' to deploy changes to your workspace - >>> [CLI] bundle summary Name: test-bind-feature-spec-[UNIQUE_NAME] Target: default @@ -37,8 +33,6 @@ Resources: >>> [CLI] bundle plan Plan: 0 to add, 0 to change, 0 to delete, 1 unchanged ->>> [CLI] bundle deployment unbind spec1 - >>> [CLI] bundle summary Name: test-bind-feature-spec-[UNIQUE_NAME] Target: default diff --git a/acceptance/bundle/deployment/bind/feature_spec/script b/acceptance/bundle/deployment/bind/feature_spec/script index 608c7fc8edc..896a123e7ed 100644 --- a/acceptance/bundle/deployment/bind/feature_spec/script +++ b/acceptance/bundle/deployment/bind/feature_spec/script @@ -6,13 +6,13 @@ trace $CLI feature-engineering create-feature-spec --json "{ \"features\": [{\"feature_lookup\": {\"table\": \"$SCHEMA_FULL_NAME.user_features\", \"lookup_keys\": [\"user_id\"], \"feature_names\": [\"age\"]}}] }" | jq '{name}' -trace $CLI bundle deployment bind spec1 $SCHEMA_FULL_NAME.boundspec --auto-approve +trace $CLI bundle deployment bind spec1 $SCHEMA_FULL_NAME.boundspec --auto-approve &> out.bind.$OUTPUT_MODE.txt trace $CLI bundle summary title "The bound feature spec matches the config, so there is nothing to change" trace $CLI bundle plan -trace $CLI bundle deployment unbind spec1 +trace $CLI bundle deployment unbind spec1 &> out.unbind.$OUTPUT_MODE.txt trace $CLI bundle summary trace $CLI feature-engineering delete-feature-spec $SCHEMA_FULL_NAME.boundspec diff --git a/acceptance/bundle/deployment/bind/feature_spec/test.toml b/acceptance/bundle/deployment/bind/feature_spec/test.toml index 66cb91f3551..c9f32889dab 100644 --- a/acceptance/bundle/deployment/bind/feature_spec/test.toml +++ b/acceptance/bundle/deployment/bind/feature_spec/test.toml @@ -1,3 +1,9 @@ +# Keep history warnings in separate outputs. +EnvMatrix.OUTPUT_MODE = ["plain", "recorded"] +EnvMatrixExclude.plain_history = ["DMS=true", "OUTPUT_MODE=plain"] +EnvMatrixExclude.recorded_without_history = ["DMS=", "OUTPUT_MODE=recorded"] +EnvVaryOutput = "OUTPUT_MODE" + Ignore = [ ".databricks", ] diff --git a/acceptance/bundle/deployment/bind/job/job-abort-bind/output.txt b/acceptance/bundle/deployment/bind/job/job-abort-bind/output.txt index 8e212f477d7..fbe3c197c9c 100644 --- a/acceptance/bundle/deployment/bind/job/job-abort-bind/output.txt +++ b/acceptance/bundle/deployment/bind/job/job-abort-bind/output.txt @@ -9,6 +9,8 @@ To proceed, use --auto-approve after reviewing the plan above. Exit code: 1 +>>> assert_not_exists.py .databricks/bundle/default/resources.json .databricks/bundle/default/resources.json.wal .databricks/bundle/default/resources.json.temp-bind + === Deploy bundle: >>> [CLI] bundle deploy --force-lock diff --git a/acceptance/bundle/deployment/bind/job/job-abort-bind/script b/acceptance/bundle/deployment/bind/job/job-abort-bind/script index 63aedd7f285..748994da8c3 100644 --- a/acceptance/bundle/deployment/bind/job/job-abort-bind/script +++ b/acceptance/bundle/deployment/bind/job/job-abort-bind/script @@ -38,6 +38,7 @@ title "Expect binding to fail without an auto-approve flag:" trace errcode $CLI bundle deployment bind foo $JOB_ID &> out.bind-result.txt sed -n '/^Error:/,$p' out.bind-result.txt rm out.bind-result.txt +trace assert_not_exists.py .databricks/bundle/default/resources.json .databricks/bundle/default/resources.json.wal .databricks/bundle/default/resources.json.temp-bind title "Deploy bundle:" trace $CLI bundle deploy --force-lock diff --git a/acceptance/bundle/deployment/bind/job/noop-job/output.txt b/acceptance/bundle/deployment/bind/job/noop-job/output.txt index 956f90977b4..c9a42b9adf8 100644 --- a/acceptance/bundle/deployment/bind/job/noop-job/output.txt +++ b/acceptance/bundle/deployment/bind/job/noop-job/output.txt @@ -5,6 +5,14 @@ Successfully bound job with an id '[JOB_ID]' Run 'bundle deploy' to deploy changes to your workspace +>>> jq {serial, resource_id: .state["resources.jobs.job_1"].__id__} .databricks/bundle/default/resources.json +{ + "serial": 1, + "resource_id": "[JOB_ID]" +} + +>>> assert_not_exists.py .databricks/bundle/default/resources.json.wal .databricks/bundle/default/resources.json.temp-bind + >>> [CLI] bundle deploy Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/my_project/default/files... Updated jobs.job_1 diff --git a/acceptance/bundle/deployment/bind/job/noop-job/script b/acceptance/bundle/deployment/bind/job/noop-job/script index adafb1c4da4..7a2f30eba35 100644 --- a/acceptance/bundle/deployment/bind/job/noop-job/script +++ b/acceptance/bundle/deployment/bind/job/noop-job/script @@ -2,6 +2,8 @@ job_id=$(trace $CLI jobs create --json '{"name": "My Job"}' | jq -r '.job_id') add_repl "$job_id" JOB_ID trace $CLI bundle deployment bind job_1 $job_id --auto-approve +trace jq '{serial, resource_id: .state["resources.jobs.job_1"].__id__}' .databricks/bundle/default/resources.json +trace assert_not_exists.py .databricks/bundle/default/resources.json.wal .databricks/bundle/default/resources.json.temp-bind trace $CLI bundle deploy diff --git a/acceptance/bundle/dms/bind-history-failures/output.txt b/acceptance/bundle/dms/bind-history-failures/output.txt index dd3a75221c9..3a7009625f8 100644 --- a/acceptance/bundle/dms/bind-history-failures/output.txt +++ b/acceptance/bundle/dms/bind-history-failures/output.txt @@ -13,7 +13,7 @@ Run 'bundle deploy' to deploy changes to your workspace >>> fault.py PATCH /api/2.0/bundle/** 400 0 1 >>> [CLI] bundle deployment bind record_failed [JOB_ID] --auto-approve -Warn: Failed to record bind history: recording operation for resources.jobs.record_failed: Fault injected by test. +Warn: Failed to record deployment history: recording operation for resources.jobs.record_failed: Fault injected by test. Successfully bound job with an id '[JOB_ID]' Run 'bundle deploy' to deploy changes to your workspace @@ -42,13 +42,13 @@ Run 'bundle deploy' to deploy changes to your workspace } >>> print_requests.py --dms //versions --oneline -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "3"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_UPDATE", "resource_key": "jobs.create_failed"}], "version_type": "VERSION_TYPE_DEPLOY"}} -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "5"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_UPDATE", "resource_key": "jobs.record_failed"}], "version_type": "VERSION_TYPE_DEPLOY"}} -{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/5/operations/jobs.record_failed", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-history-failures-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"record_failed\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/5/complete", "body": {"completion_reason": "VERSION_COMPLETE_FAILURE"}} -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "7"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_UPDATE", "resource_key": "jobs.complete_failed"}], "previous_version_id": "5", "version_type": "VERSION_TYPE_DEPLOY"}} -{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/7/operations/jobs.complete_failed", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-history-failures-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"complete_failed\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/7/complete", "body": {"completion_reason": "VERSION_COMPLETE_SUCCESS"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "1"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_UPDATE", "resource_key": "jobs.create_failed"}], "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "2"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_UPDATE", "resource_key": "jobs.record_failed"}], "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/2/operations/jobs.record_failed", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-history-failures-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"record_failed\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/2/complete", "body": {"completion_reason": "VERSION_COMPLETE_FAILURE"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "3"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_UPDATE", "resource_key": "jobs.complete_failed"}], "previous_version_id": "2", "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/3/operations/jobs.complete_failed", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-history-failures-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"complete_failed\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/3/complete", "body": {"completion_reason": "VERSION_COMPLETE_SUCCESS"}} === A remote state upload failure fails the bind version @@ -64,9 +64,9 @@ API message: Fault injected by test. >>> print_requests.py --dms //versions --oneline -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "9"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_UPDATE", "resource_key": "jobs.upload_failed"}], "previous_version_id": "7", "version_type": "VERSION_TYPE_DEPLOY"}} -{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/9/operations/jobs.upload_failed", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-history-failures-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"upload_failed\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/9/complete", "body": {"completion_reason": "VERSION_COMPLETE_FAILURE"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "4"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_UPDATE", "resource_key": "jobs.upload_failed"}], "previous_version_id": "3", "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/4/operations/jobs.upload_failed", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-history-failures-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"upload_failed\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/4/complete", "body": {"completion_reason": "VERSION_COMPLETE_FAILURE"}} === Remote state still contains only the successful bindings diff --git a/acceptance/bundle/dms/bind-unbind/output.txt b/acceptance/bundle/dms/bind-unbind/output.txt index 3c0d02308bf..4b9965c0b0d 100644 --- a/acceptance/bundle/dms/bind-unbind/output.txt +++ b/acceptance/bundle/dms/bind-unbind/output.txt @@ -13,16 +13,27 @@ Error: this bind operation requires user confirmation, but the current console d To proceed, use --auto-approve after reviewing the plan above. +>>> assert_not_exists.py .databricks/bundle/default/resources.json .databricks/bundle/default/resources.json.wal .databricks/bundle/default/resources.json.temp-bind + >>> print_requests.py --dms //api/2.0/bundle >>> [CLI] bundle deployment bind foo [JOB_ID] --auto-approve Successfully bound job with an id '[JOB_ID]' Run 'bundle deploy' to deploy changes to your workspace +>>> jq {serial, features, resource_id: .state["resources.jobs.foo"].__id__} .databricks/bundle/default/resources.json +{ + "serial": 1, + "features": { + "deployment_history_shadow": {} + }, + "resource_id": "[JOB_ID]" +} + >>> print_requests.py --dms //versions --oneline -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "3"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_UPDATE", "resource_key": "jobs.foo"}], "version_type": "VERSION_TYPE_DEPLOY"}} -{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/3/operations/jobs.foo", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-unbind-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"foo\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/3/complete", "body": {"completion_reason": "VERSION_COMPLETE_SUCCESS"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "1"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_UPDATE", "resource_key": "jobs.foo"}], "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/1/operations/jobs.foo", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-unbind-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"foo\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/1/complete", "body": {"completion_reason": "VERSION_COMPLETE_SUCCESS"}} >>> [CLI] jobs get [JOB_ID] { @@ -72,9 +83,9 @@ Resources: 0 created, 1 changed, 0 deleted, 0 unchanged "job_id": [JOB_ID] } -=== Rebind preserves the existing shadow history marker +=== Rebind preserves the existing shadow history marker without configuration ->>> [CLI] bundle deployment bind foo [JOB_ID] --auto-approve +>>> DATABRICKS_BUNDLE_DEPLOYMENT_HISTORY= [CLI] bundle deployment bind foo [JOB_ID] --auto-approve Successfully bound job with an id '[JOB_ID]' Run 'bundle deploy' to deploy changes to your workspace @@ -87,9 +98,9 @@ Run 'bundle deploy' to deploy changes to your workspace } >>> print_requests.py --dms //versions --oneline -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "7"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_UPDATE", "resource_key": "jobs.foo"}], "previous_version_id": "4", "version_type": "VERSION_TYPE_DEPLOY"}} -{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/7/operations/jobs.foo", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-unbind-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"foo\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} -{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/7/complete", "body": {"completion_reason": "VERSION_COMPLETE_SUCCESS"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions", "q": {"version_id": "4"}, "body": {"cli_version": "[CLI_VERSION]", "operations": [{"action_type": "OPERATION_ACTION_TYPE_UPDATE", "resource_key": "jobs.foo"}], "previous_version_id": "2", "version_type": "VERSION_TYPE_DEPLOY"}} +{"method": "PATCH", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/4/operations/jobs.foo", "q": {"update_mask": "state,error_message,resource_id,status"}, "body": {"error_message": "", "resource_id": "[JOB_ID]", "sequence_id": 0, "state": "{\"state\":{\"deployment\":{\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-bind-unbind-[UNIQUE_NAME]/default/state/metadata.json\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"foo\",\"queue\":{\"enabled\":true}}}", "status": "OPERATION_STATUS_SUCCEEDED"}} +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/4/complete", "body": {"completion_reason": "VERSION_COMPLETE_SUCCESS"}} === A subsequent deploy continues managing the same job diff --git a/acceptance/bundle/dms/bind-unbind/script b/acceptance/bundle/dms/bind-unbind/script index 8c7d75c3d43..bc19499567e 100644 --- a/acceptance/bundle/dms/bind-unbind/script +++ b/acceptance/bundle/dms/bind-unbind/script @@ -11,8 +11,10 @@ cleanup_bind_target() { trap cleanup_bind_target EXIT trace musterr $CLI bundle deployment bind foo "$job_id" +trace assert_not_exists.py .databricks/bundle/default/resources.json .databricks/bundle/default/resources.json.wal .databricks/bundle/default/resources.json.temp-bind trace print_requests.py --dms //api/2.0/bundle trace $CLI bundle deployment bind foo "$job_id" --auto-approve +trace jq '{serial, features, resource_id: .state["resources.jobs.foo"].__id__}' .databricks/bundle/default/resources.json trace print_requests.py --dms //versions --oneline trace $CLI jobs get "$job_id" | jq '{job_id, name: .settings.name}' deployment_id=$(MSYS_NO_PATHCONV=1 $CLI workspace get-status "/Workspace/Users/${CURRENT_USER_NAME}/.bundle/dms-bind-unbind-${UNIQUE_NAME}/default/state/resources.deployment.json" -o json | jq -r '.object_id') @@ -30,8 +32,8 @@ trace $CLI jobs get "$job_id" | jq '{job_id}' rm -f "$OUT_REQUESTS" -title "Rebind preserves the existing shadow history marker" -trace $CLI bundle deployment bind foo "$job_id" --auto-approve +title "Rebind preserves the existing shadow history marker without configuration" +trace DATABRICKS_BUNDLE_DEPLOYMENT_HISTORY= $CLI bundle deployment bind foo "$job_id" --auto-approve trace jq '{features, resource_id: .state["resources.jobs.foo"].__id__}' .databricks/bundle/default/resources.json trace print_requests.py --dms //versions --oneline diff --git a/acceptance/bundle/dms_fault/complete_version/output.txt b/acceptance/bundle/dms_fault/complete_version/output.txt index c7d574f4431..2d6b9c60c94 100644 --- a/acceptance/bundle/dms_fault/complete_version/output.txt +++ b/acceptance/bundle/dms_fault/complete_version/output.txt @@ -51,6 +51,7 @@ >>> bundle generate job --existing-job-id [FOO_ID] --key foo --config-dir resources --source-dir src --force --bind Job configuration successfully saved to resources/foo.job.yml + Warn: Failed to complete deployment history version: Fault injected by test. Successfully bound job with an id '[FOO_ID]' >>> bundle generate pipeline --existing-pipeline-id [BAR_ID] --key bar --config-dir exported --source-dir exported-src --force @@ -70,6 +71,7 @@ >>> bundle generate pipeline --existing-pipeline-id [BAR_ID] --key bar --config-dir resources --source-dir src --force --bind File successfully saved to src/pipeline.sql Pipeline configuration successfully saved to resources/bar.pipeline.yml + Warn: Failed to complete deployment history version: Fault injected by test. Successfully bound pipeline with an id '[BAR_ID]' >>> bundle validate @@ -83,7 +85,7 @@ >>> bundle deploy --auto-approve Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-complete_version-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=7 Updated pipelines.bar Files: 6 uploaded, 0 deleted Resources: 0 created, 1 changed, 0 deleted, 1 unchanged @@ -102,7 +104,7 @@ >>> bundle deploy --auto-approve Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-complete_version-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=11 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 Updated jobs.foo Updated pipelines.bar Files: 2 uploaded, 0 deleted diff --git a/acceptance/bundle/dms_fault/create_deployment/output.txt b/acceptance/bundle/dms_fault/create_deployment/output.txt index 2b1b3f5a082..3f346874945 100644 --- a/acceptance/bundle/dms_fault/create_deployment/output.txt +++ b/acceptance/bundle/dms_fault/create_deployment/output.txt @@ -42,6 +42,7 @@ >>> bundle generate job --existing-job-id [FOO_ID] --key foo --config-dir resources --source-dir src --force --bind Job configuration successfully saved to resources/foo.job.yml + Warn: Failed to record bind history: failed to create deployment: Fault injected by test. Successfully bound job with an id '[FOO_ID]' >>> bundle generate pipeline --existing-pipeline-id [BAR_ID] --key bar --config-dir exported --source-dir exported-src --force @@ -61,6 +62,7 @@ >>> bundle generate pipeline --existing-pipeline-id [BAR_ID] --key bar --config-dir resources --source-dir src --force --bind File successfully saved to src/pipeline.sql Pipeline configuration successfully saved to resources/bar.pipeline.yml + Warn: Failed to record bind history: failed to create deployment: Fault injected by test. Successfully bound pipeline with an id '[BAR_ID]' >>> bundle validate diff --git a/acceptance/bundle/dms_fault/create_version/output.txt b/acceptance/bundle/dms_fault/create_version/output.txt index f58fd56b675..89e7730c044 100644 --- a/acceptance/bundle/dms_fault/create_version/output.txt +++ b/acceptance/bundle/dms_fault/create_version/output.txt @@ -50,6 +50,7 @@ >>> bundle generate job --existing-job-id [FOO_ID] --key foo --config-dir resources --source-dir src --force --bind Job configuration successfully saved to resources/foo.job.yml + Warn: Failed to record bind history: failed to create deployment version: Fault injected by test. Successfully bound job with an id '[FOO_ID]' >>> bundle generate pipeline --existing-pipeline-id [BAR_ID] --key bar --config-dir exported --source-dir exported-src --force @@ -69,6 +70,7 @@ >>> bundle generate pipeline --existing-pipeline-id [BAR_ID] --key bar --config-dir resources --source-dir src --force --bind File successfully saved to src/pipeline.sql Pipeline configuration successfully saved to resources/bar.pipeline.yml + Warn: Failed to record bind history: failed to create deployment version: Fault injected by test. Successfully bound pipeline with an id '[BAR_ID]' >>> bundle validate diff --git a/acceptance/bundle/dms_fault/delete_deployment/output.txt b/acceptance/bundle/dms_fault/delete_deployment/output.txt index 8efdbc07a0d..34db7ea5d7d 100644 --- a/acceptance/bundle/dms_fault/delete_deployment/output.txt +++ b/acceptance/bundle/dms_fault/delete_deployment/output.txt @@ -82,7 +82,7 @@ >>> bundle deploy --auto-approve Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-delete_deployment-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=7 Updated pipelines.bar Files: 6 uploaded, 0 deleted Resources: 0 created, 1 changed, 0 deleted, 1 unchanged @@ -100,7 +100,7 @@ >>> bundle deploy --auto-approve Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-delete_deployment-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=11 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 Updated jobs.foo Updated pipelines.bar Files: 2 uploaded, 0 deleted diff --git a/acceptance/bundle/dms_fault/get_deployment/output.txt b/acceptance/bundle/dms_fault/get_deployment/output.txt index 0dda68edc1e..f13a4533a8e 100644 --- a/acceptance/bundle/dms_fault/get_deployment/output.txt +++ b/acceptance/bundle/dms_fault/get_deployment/output.txt @@ -56,6 +56,9 @@ >>> bundle generate job --existing-job-id [FOO_ID] --key foo --config-dir resources --source-dir src --force --bind Job configuration successfully saved to resources/foo.job.yml + Warn: retrying after 500 Internal Server Error from GET /api/2.0/bundle/deployments/[DEPLOYMENT_ID] + Warn: retrying after 500 Internal Server Error from GET /api/2.0/bundle/deployments/[DEPLOYMENT_ID] + Warn: Failed to record bind history: Fault injected by test. Successfully bound job with an id '[FOO_ID]' >>> bundle generate pipeline --existing-pipeline-id [BAR_ID] --key bar --config-dir exported --source-dir exported-src --force @@ -75,6 +78,9 @@ >>> bundle generate pipeline --existing-pipeline-id [BAR_ID] --key bar --config-dir resources --source-dir src --force --bind File successfully saved to src/pipeline.sql Pipeline configuration successfully saved to resources/bar.pipeline.yml + Warn: retrying after 500 Internal Server Error from GET /api/2.0/bundle/deployments/[DEPLOYMENT_ID] + Warn: retrying after 500 Internal Server Error from GET /api/2.0/bundle/deployments/[DEPLOYMENT_ID] + Warn: Failed to record bind history: Fault injected by test. Successfully bound pipeline with an id '[BAR_ID]' >>> bundle validate @@ -91,7 +97,7 @@ Warn: retrying after 500 Internal Server Error from GET /api/2.0/bundle/deployments/[DEPLOYMENT_ID] Warn: Could not read deployment history: Fault injected by test. Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-get_deployment-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=7 Updated jobs.foo Updated pipelines.bar Files: 6 uploaded, 0 deleted @@ -113,7 +119,7 @@ Warn: retrying after 500 Internal Server Error from GET /api/2.0/bundle/deployments/[DEPLOYMENT_ID] Warn: Could not read deployment history: Fault injected by test. Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-get_deployment-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=11 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 Updated jobs.foo Files: 2 uploaded, 0 deleted Resources: 0 created, 1 changed, 0 deleted, 1 unchanged diff --git a/acceptance/bundle/dms_fault/get_operation/output.txt b/acceptance/bundle/dms_fault/get_operation/output.txt index 65498809a65..77a30f34331 100644 --- a/acceptance/bundle/dms_fault/get_operation/output.txt +++ b/acceptance/bundle/dms_fault/get_operation/output.txt @@ -82,7 +82,7 @@ >>> bundle deploy --auto-approve Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-get_operation-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=7 Updated pipelines.bar Files: 6 uploaded, 0 deleted Resources: 0 created, 1 changed, 0 deleted, 1 unchanged @@ -100,7 +100,7 @@ >>> bundle deploy --auto-approve Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-get_operation-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=11 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 Updated jobs.foo Updated pipelines.bar Files: 2 uploaded, 0 deleted diff --git a/acceptance/bundle/dms_fault/get_resource/output.txt b/acceptance/bundle/dms_fault/get_resource/output.txt index a562c0f5264..1f73acc8286 100644 --- a/acceptance/bundle/dms_fault/get_resource/output.txt +++ b/acceptance/bundle/dms_fault/get_resource/output.txt @@ -82,7 +82,7 @@ >>> bundle deploy --auto-approve Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-get_resource-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=7 Updated pipelines.bar Files: 6 uploaded, 0 deleted Resources: 0 created, 1 changed, 0 deleted, 1 unchanged @@ -100,7 +100,7 @@ >>> bundle deploy --auto-approve Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-get_resource-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=11 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 Updated jobs.foo Updated pipelines.bar Files: 2 uploaded, 0 deleted diff --git a/acceptance/bundle/dms_fault/get_version/output.txt b/acceptance/bundle/dms_fault/get_version/output.txt index b4473435fe9..e2207b8defb 100644 --- a/acceptance/bundle/dms_fault/get_version/output.txt +++ b/acceptance/bundle/dms_fault/get_version/output.txt @@ -82,7 +82,7 @@ >>> bundle deploy --auto-approve Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-get_version-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=7 Updated pipelines.bar Files: 6 uploaded, 0 deleted Resources: 0 created, 1 changed, 0 deleted, 1 unchanged @@ -100,7 +100,7 @@ >>> bundle deploy --auto-approve Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-get_version-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=11 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 Updated jobs.foo Updated pipelines.bar Files: 2 uploaded, 0 deleted diff --git a/acceptance/bundle/dms_fault/heartbeat/output.txt b/acceptance/bundle/dms_fault/heartbeat/output.txt index b512e794a75..239581a481a 100644 --- a/acceptance/bundle/dms_fault/heartbeat/output.txt +++ b/acceptance/bundle/dms_fault/heartbeat/output.txt @@ -82,7 +82,7 @@ >>> bundle deploy --auto-approve Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-heartbeat-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=7 Updated pipelines.bar Files: 6 uploaded, 0 deleted Resources: 0 created, 1 changed, 0 deleted, 1 unchanged @@ -100,7 +100,7 @@ >>> bundle deploy --auto-approve Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-heartbeat-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=11 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 Updated jobs.foo Updated pipelines.bar Files: 2 uploaded, 0 deleted diff --git a/acceptance/bundle/dms_fault/list_deployments/output.txt b/acceptance/bundle/dms_fault/list_deployments/output.txt index 0710bf48939..907f90146fc 100644 --- a/acceptance/bundle/dms_fault/list_deployments/output.txt +++ b/acceptance/bundle/dms_fault/list_deployments/output.txt @@ -82,7 +82,7 @@ >>> bundle deploy --auto-approve Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-list_deployments-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=7 Updated pipelines.bar Files: 6 uploaded, 0 deleted Resources: 0 created, 1 changed, 0 deleted, 1 unchanged @@ -100,7 +100,7 @@ >>> bundle deploy --auto-approve Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-list_deployments-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=11 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 Updated jobs.foo Updated pipelines.bar Files: 2 uploaded, 0 deleted diff --git a/acceptance/bundle/dms_fault/list_operations/output.txt b/acceptance/bundle/dms_fault/list_operations/output.txt index 5764b81d690..37eb784a030 100644 --- a/acceptance/bundle/dms_fault/list_operations/output.txt +++ b/acceptance/bundle/dms_fault/list_operations/output.txt @@ -82,7 +82,7 @@ >>> bundle deploy --auto-approve Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-list_operations-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=7 Updated pipelines.bar Files: 6 uploaded, 0 deleted Resources: 0 created, 1 changed, 0 deleted, 1 unchanged @@ -100,7 +100,7 @@ >>> bundle deploy --auto-approve Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-list_operations-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=11 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 Updated jobs.foo Updated pipelines.bar Files: 2 uploaded, 0 deleted diff --git a/acceptance/bundle/dms_fault/list_resources/output.txt b/acceptance/bundle/dms_fault/list_resources/output.txt index 9f45951b03a..ce7a141c620 100644 --- a/acceptance/bundle/dms_fault/list_resources/output.txt +++ b/acceptance/bundle/dms_fault/list_resources/output.txt @@ -82,7 +82,7 @@ >>> bundle deploy --auto-approve Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-list_resources-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=7 Updated pipelines.bar Files: 6 uploaded, 0 deleted Resources: 0 created, 1 changed, 0 deleted, 1 unchanged @@ -100,7 +100,7 @@ >>> bundle deploy --auto-approve Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-list_resources-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=11 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 Updated jobs.foo Updated pipelines.bar Files: 2 uploaded, 0 deleted diff --git a/acceptance/bundle/dms_fault/list_versions/output.txt b/acceptance/bundle/dms_fault/list_versions/output.txt index dabee52cbcd..bb5d8e97403 100644 --- a/acceptance/bundle/dms_fault/list_versions/output.txt +++ b/acceptance/bundle/dms_fault/list_versions/output.txt @@ -82,7 +82,7 @@ >>> bundle deploy --auto-approve Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-list_versions-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=7 Updated pipelines.bar Files: 6 uploaded, 0 deleted Resources: 0 created, 1 changed, 0 deleted, 1 unchanged @@ -100,7 +100,7 @@ >>> bundle deploy --auto-approve Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-list_versions-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=11 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 Updated jobs.foo Updated pipelines.bar Files: 2 uploaded, 0 deleted diff --git a/acceptance/bundle/dms_fault/update_operation/output.txt b/acceptance/bundle/dms_fault/update_operation/output.txt index 308345c48c4..eb9be000cfb 100644 --- a/acceptance/bundle/dms_fault/update_operation/output.txt +++ b/acceptance/bundle/dms_fault/update_operation/output.txt @@ -51,6 +51,7 @@ >>> bundle generate job --existing-job-id [FOO_ID] --key foo --config-dir resources --source-dir src --force --bind Job configuration successfully saved to resources/foo.job.yml + Warn: Failed to record deployment history: recording operation for [RESOURCE_KEY]: Fault injected by test. Successfully bound job with an id '[FOO_ID]' >>> bundle generate pipeline --existing-pipeline-id [BAR_ID] --key bar --config-dir exported --source-dir exported-src --force @@ -70,6 +71,7 @@ >>> bundle generate pipeline --existing-pipeline-id [BAR_ID] --key bar --config-dir resources --source-dir src --force --bind File successfully saved to src/pipeline.sql Pipeline configuration successfully saved to resources/bar.pipeline.yml + Warn: Failed to record deployment history: recording operation for [RESOURCE_KEY]: Fault injected by test. Successfully bound pipeline with an id '[BAR_ID]' >>> bundle validate @@ -83,7 +85,7 @@ >>> bundle deploy --auto-approve Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-update_operation-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=7 Updated pipelines.bar Warn: Failed to record deployment history: recording operation for [RESOURCE_KEY]: Fault injected by test. Files: 6 uploaded, 0 deleted @@ -102,7 +104,7 @@ >>> bundle deploy --auto-approve Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-fault-update_operation-commands-[UNIQUE_NAME]/files... - Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=11 + Current Deployment Version: [DATABRICKS_URL]/deployments/[DEPLOYMENT_ID]?version=9 Updated jobs.foo Updated pipelines.bar Warn: Failed to record deployment history: recording operation for [RESOURCE_KEY]: Fault injected by test. diff --git a/bundle/direct/bind.go b/bundle/direct/bind.go index 289b1076b29..711635b53ed 100644 --- a/bundle/direct/bind.go +++ b/bundle/direct/bind.go @@ -2,15 +2,16 @@ package direct import ( "context" + "encoding/json" "fmt" - "os" + "maps" + "slices" "strings" "github.com/databricks/cli/bundle/config" "github.com/databricks/cli/bundle/deployplan" "github.com/databricks/cli/bundle/direct/dresources" "github.com/databricks/cli/bundle/direct/dstate" - bundleenv "github.com/databricks/cli/bundle/env" "github.com/databricks/cli/libs/log" "github.com/databricks/cli/libs/structs/structaccess" "github.com/databricks/cli/libs/structs/structpath" @@ -36,54 +37,29 @@ func (e ErrResourceAlreadyBound) Error() string { e.ResourceKey, e.ExistingID) } -// DMSBindMetadata holds the bound resource state and serial to publish after bind is committed. -type DMSBindMetadata struct { - Serial int - ResourceState dstate.ResourceEntry -} - -// BindResult contains the result of a bind operation including any detected changes. -type BindResult struct { - // DMSMetadata is nil when deployment history is disabled. - DMSMetadata *DMSBindMetadata - // HasChanges is true if deploying after bind would make changes to the resource +// PreparedBind holds the state to save after approval and the preview of the next deploy. +type PreparedBind struct { + Resource dstate.ResourceEntry + // HasChanges is true if deploying after bind would make changes to the resource. HasChanges bool - // Action is the planned action for the bound resource (e.g., "skip", "update", "recreate") + // Action is the planned action for the bound resource (e.g., "skip", "update", "recreate"). Action deployplan.ActionType - // Plan contains the full deployment plan for the bound resource + // Plan contains the full deployment plan for the bound resource. Plan *deployplan.Plan - // TempStatePath is the path to the temporary state file - TempStatePath string - // StatePath is the path to the final state file - StatePath string } -// Bind adds an existing workspace resource to a temporary state and calculates -// if there will be any changes when deploying. -// -// Flow: -// 1. Load current state file -// 2. Update id of the target resource -// 3. Update state path to temp file (state file + ".temp-bind") -// 4. Perform plan + update to populate state with resolved config -// 5. Calculate plan again - this is the plan presented to the user -// -// Call Finalize to commit the state or Cancel to discard. -func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.WorkspaceClient, configRoot *config.Root, statePath, resourceKey, resourceID string) (*BindResult, error) { +// Bind prepares a binding in memory and previews the next deploy. The actual state stays +// open read-only; the caller must finalize it after committing the prepared resource or aborting. +func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.WorkspaceClient, configRoot *config.Root, statePath, resourceKey, resourceID string) (*PreparedBind, error) { // Check if the resource is already managed (bound to a different ID) - var checkStateDB dstate.DeploymentState - if err := checkStateDB.Open(ctx, statePath, dstate.ReadingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{}); err != nil { + if err := b.StateDB.Open(ctx, statePath, dstate.ReadingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{}); err != nil { // State that cannot be read is not the same as state without a binding: // the resource may well be managed already, and binding on top of it // would take over a resource whose ownership was never checked. A state // file that does not exist yet opens successfully as an empty one. return nil, fmt.Errorf("cannot check whether %s is already bound: %w", resourceKey, err) } - existingID := checkStateDB.GetResourceID(resourceKey) - if _, err := checkStateDB.Finalize(ctx); err != nil { - log.Warnf(ctx, "failed to finalize state: %v", err) - } - if existingID != "" { + if existingID := b.StateDB.GetResourceID(resourceKey); existingID != "" { return nil, ErrResourceAlreadyBound{ ResourceKey: resourceKey, ExistingID: existingID, @@ -91,64 +67,35 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac } } - tmpStatePath := statePath + ".temp-bind" - - // Copy existing state to temp location if it exists - if data, err := os.ReadFile(statePath); err == nil { - if err := os.WriteFile(tmpStatePath, data, 0o600); err != nil { - return nil, err + // Keep the planner's state, caches, and locks separate from the approved state. + data := b.StateDB.Data + data.Features = maps.Clone(data.Features) + data.State = make(map[string]dstate.ResourceEntry, len(b.StateDB.Data.State)+1) + for key, entry := range b.StateDB.Data.State { + entry.State = slices.Clone(entry.State) + entry.DependsOn = slices.Clone(entry.DependsOn) + data.State[key] = entry + } + data.State[resourceKey] = dstate.ResourceEntry{ID: resourceID, State: json.RawMessage(`{}`)} + preview := &DeploymentBundle{Adapters: b.Adapters} + preview.StateDB.OpenWithData(statePath, data) + defer func() { + if _, err := preview.StateDB.Finalize(ctx); err != nil { + log.Warnf(ctx, "failed to finalize binding preview: %v", err) } - } - - // Open temp state - err := b.StateDB.Open(ctx, tmpStatePath, dstate.ReadingFile, dstate.WithRecovery(false), dstate.OpenDmsArgs{}) - if err != nil { - os.Remove(tmpStatePath) - return nil, err - } - if err := b.StateDB.UpgradeToWrite(); err != nil { - os.Remove(tmpStatePath) - return nil, err - } - - // Save state with ID and empty state (like migrate does) - err = b.StateDB.SaveState(ctx, resourceKey, resourceID, struct{}{}, nil) - if err != nil { - os.Remove(tmpStatePath) - return nil, err - } - - // Finalize to persist temp state to disk - _, err = b.StateDB.Finalize(ctx) - if err != nil { - os.Remove(tmpStatePath) - return nil, err - } - - log.Infof(ctx, "Bound %s to id=%s (in temp state)", resourceKey, resourceID) + }() // First plan + update: populate state with resolved config - err = b.StateDB.Open(ctx, tmpStatePath, dstate.ReadingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{}) + plan, err := preview.CalculatePlan(ctx, client, configRoot) if err != nil { - os.Remove(tmpStatePath) return nil, err } - plan, err := b.CalculatePlan(ctx, client, configRoot) - if err != nil { - os.Remove(tmpStatePath) - return nil, err - } - if _, err := b.StateDB.Finalize(ctx); err != nil { - log.Warnf(ctx, "failed to finalize state: %v", err) - } - - // Populate the state with the resolved config entry := plan.Plan[resourceKey] - sv, ok := b.StateCache.Load(resourceKey) + sv, ok := preview.StateCache.Load(resourceKey) if ok && sv != nil { var dependsOn []deployplan.DependsOnEntry if entry != nil { - dependsOn = entry.DependsOn + dependsOn = slices.Clone(entry.DependsOn) } // Copy etag from remote state for resources that use etag-based drift @@ -166,74 +113,37 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac // Compact hashed_fields fields so the persisted state stays small. Not needed for // correctness — the next plan (CalculatePlan) compacts the saved state on read. - adapter, err := b.getAdapterForKey(resourceKey) + adapter, err := preview.getAdapterForKey(resourceKey) if err != nil { - os.Remove(tmpStatePath) return nil, err } compacted, err := dresources.CompactState(adapter.ResourceConfig(), sv.Value) if err != nil { - os.Remove(tmpStatePath) return nil, fmt.Errorf("compacting state: %w", err) } - err = b.StateDB.Open(ctx, tmpStatePath, dstate.ReadingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{}) - if err != nil { - os.Remove(tmpStatePath) - return nil, err - } - if err := b.StateDB.UpgradeToWrite(); err != nil { - os.Remove(tmpStatePath) - return nil, err - } - - err = b.StateDB.SaveState(ctx, resourceKey, resourceID, compacted, dependsOn) + state, err := json.Marshal(compacted) if err != nil { - os.Remove(tmpStatePath) - return nil, err - } - - _, err = b.StateDB.Finalize(ctx) - if err != nil { - os.Remove(tmpStatePath) return nil, err } + data.State[resourceKey] = dstate.ResourceEntry{ID: resourceID, State: state, DependsOn: dependsOn} } - // Second plan: this is the plan to present to the user (change between remote resource and config) - err = b.StateDB.Open(ctx, tmpStatePath, dstate.ReadingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{}) - if err != nil { - os.Remove(tmpStatePath) + // Capture unredacted state before the second plan replaces cached values. + resource := data.State[resourceKey] + resource.State = slices.Clone(resource.State) + resource.DependsOn = slices.Clone(resource.DependsOn) + if _, err := preview.StateDB.Finalize(ctx); err != nil { return nil, err } - var configured *bool - if configRoot.Experimental != nil { - configured = configRoot.Experimental.DeploymentHistory - } - var dmsMetadata *DMSBindMetadata - if b.StateDB.RecordsHistory() || bundleenv.RecordsDeploymentHistory(ctx, configured) { - resourceState, _ := b.StateDB.GetResourceEntry(resourceKey) - dmsMetadata = &DMSBindMetadata{Serial: b.StateDB.Data.Serial, ResourceState: resourceState} - } - plan, err = b.CalculatePlan(ctx, client, configRoot) - if _, ferr := b.StateDB.Finalize(ctx); ferr != nil { - log.Warnf(ctx, "failed to finalize state: %v", ferr) - } + preview.StateDB.OpenWithData(statePath, data) + + // Second plan: this is the plan to present to the user (change between remote resource and config) + plan, err = preview.CalculatePlan(ctx, client, configRoot) if err != nil { - os.Remove(tmpStatePath) return nil, err } - - // Check if the bound resource has changes - result := &BindResult{ - DMSMetadata: dmsMetadata, - HasChanges: false, - Action: deployplan.Skip, - Plan: plan, - TempStatePath: tmpStatePath, - StatePath: statePath, - } - + result := &PreparedBind{Resource: resource, Action: deployplan.Skip, Plan: plan} entry = plan.Plan[resourceKey] if entry != nil { result.Action = entry.Action @@ -241,25 +151,9 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac // does not write), so it changes nothing remotely. result.HasChanges = result.Action != deployplan.Skip && result.Action != deployplan.Undefined && !entry.IsStateOnly() } - return result, nil } -// Finalize completes the bind operation by renaming the temp state to the final location. -func (result *BindResult) Finalize() error { - if result == nil || result.TempStatePath == "" { - return nil - } - return os.Rename(result.TempStatePath, result.StatePath) -} - -// Cancel cleans up temp state without committing changes. -func (result *BindResult) Cancel() { - if result != nil && result.TempStatePath != "" { - os.Remove(result.TempStatePath) - } -} - // Unbind removes a resource from direct engine state without deleting // the workspace resource. Also removes associated permissions/grants entries. func (b *DeploymentBundle) Unbind(ctx context.Context, statePath, resourceKey string) error { diff --git a/bundle/direct/bind_test.go b/bundle/direct/bind_test.go new file mode 100644 index 00000000000..dca935a25ef --- /dev/null +++ b/bundle/direct/bind_test.go @@ -0,0 +1,86 @@ +package direct_test + +import ( + "encoding/json" + "os" + "path/filepath" + "testing" + + "github.com/databricks/cli/bundle/config" + "github.com/databricks/cli/bundle/deployplan" + "github.com/databricks/cli/bundle/direct" + "github.com/databricks/cli/bundle/direct/dstate" + "github.com/databricks/cli/libs/logdiag" + "github.com/databricks/cli/libs/structs/structvar" + "github.com/databricks/cli/libs/testserver" + "github.com/databricks/databricks-sdk-go" + "github.com/databricks/databricks-sdk-go/service/jobs" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestBindPreparesIsolatedState(t *testing.T) { + ctx := logdiag.InitContext(t.Context()) + statePath := filepath.Join(t.TempDir(), "resources.json") + data := dstate.NewDatabase("lineage", 4) + data.Features = map[string]struct{}{dstate.FeatureDeploymentHistoryShadow: {}} + data.State["resources.jobs.kept"] = dstate.ResourceEntry{ + ID: "10", State: json.RawMessage(`{"name":"kept"}`), + DependsOn: []deployplan.DependsOnEntry{{Node: "resources.jobs.old"}}, + } + original, err := json.Marshal(data) + require.NoError(t, err) + require.NoError(t, os.WriteFile(statePath, original, 0o600)) + + server := testserver.New(t) + server.Handle("GET", "/api/2.2/jobs/get", func(req testserver.Request) any { + assert.NoFileExists(t, statePath+".wal") + assert.NoFileExists(t, statePath+".temp-bind") + actual, err := os.ReadFile(statePath) + require.NoError(t, err) + assert.Equal(t, original, actual) + if req.URL.Query().Get("job_id") == "10" { + return jobs.Job{JobId: 10, Settings: &jobs.JobSettings{Name: "kept"}} + } + return jobs.Job{JobId: 20, Settings: &jobs.JobSettings{Name: "remote"}} + }) + client, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "testtoken"}) + require.NoError(t, err) + root, diags := config.LoadFromBytes("databricks.yml", []byte(`resources: + jobs: + kept: + name: kept + bound: + name: configured + tags: + other: ${resources.jobs.kept.id} +`)) + require.Empty(t, diags) + var b direct.DeploymentBundle + cached := structvar.NewStructVar(&jobs.JobSettings{Name: "original cache"}, nil) + b.StateCache.Store("resources.jobs.bound", cached) + b.RemoteStateCache.Store("resources.jobs.bound", "original remote cache") + + prepared, err := b.Bind(ctx, client, root, statePath, "resources.jobs.bound", "20") + require.NoError(t, err) + t.Cleanup(func() { + _, err := b.StateDB.Finalize(ctx) + require.NoError(t, err) + }) + require.False(t, logdiag.HasError(ctx)) + assert.Equal(t, data, b.StateDB.Data) + assert.Empty(t, b.StateDB.GetResourceID("resources.jobs.bound")) + assert.Equal(t, "10", b.StateDB.GetResourceID("resources.jobs.kept")) + assert.Nil(t, b.Plan) + actualCache, ok := b.StateCache.Load("resources.jobs.bound") + require.True(t, ok) + assert.Same(t, cached, actualCache) + remoteCache, ok := b.RemoteStateCache.Load("resources.jobs.bound") + require.True(t, ok) + assert.Equal(t, "original remote cache", remoteCache) + assert.Equal(t, "20", prepared.Resource.ID) + assert.JSONEq(t, `{"name":"configured","tags":{"other":"10"}}`, string(prepared.Resource.State)) + assert.Contains(t, prepared.Resource.DependsOn, deployplan.DependsOnEntry{Node: "resources.jobs.kept", Label: "${resources.jobs.kept.id}"}) + assert.True(t, prepared.HasChanges) + assert.Equal(t, deployplan.Update, prepared.Action) +} diff --git a/bundle/phases/bind.go b/bundle/phases/bind.go index fb9a1f316be..002ccbbaf53 100644 --- a/bundle/phases/bind.go +++ b/bundle/phases/bind.go @@ -7,7 +7,6 @@ import ( "fmt" "maps" "slices" - "strconv" "strings" "github.com/databricks/cli/bundle" @@ -15,10 +14,9 @@ import ( "github.com/databricks/cli/bundle/deploy/lock" "github.com/databricks/cli/bundle/deploy/snapshot" "github.com/databricks/cli/bundle/deployplan" - "github.com/databricks/cli/bundle/direct" "github.com/databricks/cli/bundle/direct/dstate" + bundleenv "github.com/databricks/cli/bundle/env" "github.com/databricks/cli/bundle/statemgmt" - "github.com/databricks/cli/internal/build" "github.com/databricks/cli/libs/agent" "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" @@ -36,13 +34,7 @@ type BindOptions struct { ResourceId string } -// bindingHistory keeps the recorder alive through the remote state upload. -type bindingHistory struct { - name string - buffer *dms.OperationBuffer -} - -// createBindingDeployment registers a first binding without changing metadata from an existing deploy. +// createBindingDeployment creates a deployment record for the first recorded bind, without a deployed file path. func createBindingDeployment(ctx context.Context, b *bundle.Bundle) (string, error) { // Bind does not upload files or stage an immutable snapshot, so it cannot report a deployed file path. created, err := b.WorkspaceClient(ctx).BundleDeployments.CreateDeployment(ctx, bundledeployments.CreateDeploymentRequest{ @@ -73,10 +65,17 @@ func Bind(ctx context.Context, b *bundle.Bundle, opts *BindOptions, stateDesc *s return } - var history *bindingHistory - var historyErr error + if !cmdctx.HasWorkspaceClient(ctx) { + ctx = cmdctx.SetWorkspaceClient(ctx, b.WorkspaceClient(ctx)) + } + db := &b.DeploymentBundle.StateDB defer func() { - completeBindingHistory(ctx, b, history, historyErr == nil && !logdiag.HasError(ctx)) + if _, err := db.Finalize(ctx); err != nil { + logdiag.LogError(ctx, err) + } + if _, err := db.CompleteVersion(ctx, !logdiag.HasError(ctx)); err != nil { + log.Warnf(ctx, "Failed to complete deployment history version: %s", err) + } bundle.ApplyContext(ctx, b, lock.Release(lock.GoalBind)) }() @@ -98,7 +97,7 @@ func Bind(ctx context.Context, b *bundle.Bundle, opts *BindOptions, stateDesc *s } } - // Direct engine: import into temp state, run plan, check for changes. + // Prepare the binding without writing state until the plan is approved. resourceKey := fmt.Sprintf("resources.%s.%s", opts.Group, opts.ResourceKey) _, statePath := b.StateFilenameDirect(ctx) @@ -128,56 +127,50 @@ func Bind(ctx context.Context, b *bundle.Bundle, opts *BindOptions, stateDesc *s } if !cmdio.IsPromptSupported(ctx) { - result.Cancel() logdiag.LogError(ctx, fmt.Errorf("this bind operation requires user confirmation, but the current console does not support prompting.\nTo proceed, use --auto-approve after reviewing the plan above.%s", agent.AgentNotice())) return } ans, err := cmdio.AskYesOrNo(ctx, "Confirm import changes? Changes will be remotely applied only after running 'bundle deploy'.") if err != nil { - result.Cancel() logdiag.LogError(ctx, err) return } if !ans { - result.Cancel() logdiag.LogError(ctx, errors.New("import aborted")) return } } - // Finalize: rename temp state to final location - err = result.Finalize() - if err != nil { + var configured *bool + if b.Config.Experimental != nil { + configured = b.Config.Experimental.DeploymentHistory + } + if db.RecordsHistory() || bundleenv.RecordsDeploymentHistory(ctx, configured) { + if db.Data.Features == nil { + db.Data.Features = make(map[string]struct{}) + } + db.Data.Features[dstate.FeatureDeploymentHistoryShadow] = struct{}{} + } + if err := db.UpgradeToWrite(); err != nil { logdiag.LogError(ctx, err) return } - - history, historyErr = recordBindHistory(ctx, b, resourceKey, result.DMSMetadata) - if historyErr != nil { - log.Warnf(ctx, "Failed to record bind history: %s", historyErr) + if db.RecordsHistory() { + if err := prepareBindHistory(ctx, b, resourceKey); err != nil { + log.Warnf(ctx, "Failed to record bind history: %s", err) + } } - - statemgmt.PushResourcesState(ctx, b) -} - -// completeBindingHistory completes the recorded version after the remote state upload. -func completeBindingHistory(ctx context.Context, b *bundle.Bundle, history *bindingHistory, success bool) { - if history == nil { + resource := result.Resource + if err := db.SaveState(ctx, resourceKey, resource.ID, resource.State, resource.DependsOn); err != nil { + logdiag.LogError(ctx, err) return } - history.buffer.Stop() - reason := bundledeployments.VersionCompleteVersionCompleteFailure - if success { - reason = bundledeployments.VersionCompleteVersionCompleteSuccess - } - _, err := b.WorkspaceClient(ctx).BundleDeployments.CompleteVersion(ctx, bundledeployments.CompleteVersionRequest{ - Name: history.name, - CompletionReason: reason, - }) - if err != nil { - log.Warnf(ctx, "Failed to complete deployment history version: %s", err) + if _, err := db.Finalize(ctx); err != nil { + logdiag.LogError(ctx, err) + return } + statemgmt.PushResourcesState(ctx, b) } func jsonDump(ctx context.Context, v any, field string) string { @@ -219,74 +212,26 @@ func Unbind(ctx context.Context, b *bundle.Bundle, group, resourceKey string, en statemgmt.PushResourcesState(ctx, b) } -// recordBindHistory publishes a committed binding when DMS metadata was requested. -func recordBindHistory(ctx context.Context, b *bundle.Bundle, resourceKey string, metadata *direct.DMSBindMetadata) (*bindingHistory, error) { - if metadata == nil { - return nil, nil - } - entry := metadata.ResourceState - state, err := json.Marshal(dstate.RecordedState{State: entry.State, DependsOn: entry.DependsOn}) - if err != nil { - return nil, err - } - history, err := createBindingVersion(ctx, b, metadata.Serial, []bundledeployments.StagedOperation{{ - ResourceKey: strings.TrimPrefix(resourceKey, dms.StatePrefix), - ActionType: bundledeployments.OperationActionTypeOperationActionTypeUpdate, - }}) - if err != nil { - return nil, err - } - history.buffer.RecordOperation(ctx, resourceKey, false, entry.ID, state) - return history, history.buffer.Drain(ctx) -} - -// createBindingVersion claims a history version for the staged binding operations. -func createBindingVersion(ctx context.Context, b *bundle.Bundle, serial int, staged []bundledeployments.StagedOperation) (*bindingHistory, error) { +// prepareBindHistory stages only the approved binding, preserving deployed file metadata. +func prepareBindHistory(ctx context.Context, b *bundle.Bundle, resourceKey string) error { deploymentID, current, err := dms.LookupDeployment(ctx, b.WorkspaceClient(ctx), b.Config.Workspace.StatePath) if err != nil { - return nil, err + return err } previous, err := dms.ParseLastVersionID(current) if err != nil { - return nil, err + return err } if current == nil { deploymentID, err = createBindingDeployment(ctx, b) if err != nil { - return nil, err + return err } } - versionID := max(previous, serial) + 1 - previousVersionID := "" - if previous > 0 { - previousVersionID = strconv.Itoa(previous) - } - var gitInfo *bundledeployments.GitInfo - if git := b.Config.Bundle.Git; git.Branch != "" || git.Commit != "" || git.OriginURL != "" { - gitInfo = &bundledeployments.GitInfo{Branch: git.Branch, Commit: git.Commit, OriginUrl: git.OriginURL} - } - version, err := b.WorkspaceClient(ctx).BundleDeployments.CreateVersion(ctx, bundledeployments.CreateVersionRequest{ - Parent: dms.DeploymentName(deploymentID), - VersionId: strconv.Itoa(versionID), - Version: bundledeployments.Version{ - CliVersion: build.GetInfo().Version, - VersionType: dms.VersionTypeDeploy, - PreviousVersionId: previousVersionID, - GitInfo: gitInfo, - Operations: staged, - }, - }) - if err != nil { - return nil, fmt.Errorf("failed to create deployment version: %w", err) - } - if !cmdctx.HasWorkspaceClient(ctx) { - ctx = cmdctx.SetWorkspaceClient(ctx, b.WorkspaceClient(ctx)) - } - return &bindingHistory{ - name: version.Name, - buffer: dms.StartOperationBuffer(ctx, deploymentID, versionID, dms.BufferOptions{ - BestEffort: true, - StagedOperationCount: len(staged), - }), - }, nil + db := &b.DeploymentBundle.StateDB + db.DeploymentID, db.VersionID = deploymentID, previous + return startVersion(ctx, b, dms.VersionTypeDeploy, []bundledeployments.StagedOperation{{ + ResourceKey: strings.TrimPrefix(resourceKey, dms.StatePrefix), + ActionType: bundledeployments.OperationActionTypeOperationActionTypeUpdate, + }}) } diff --git a/bundle/phases/bind_test.go b/bundle/phases/bind_test.go new file mode 100644 index 00000000000..aadfd009a1e --- /dev/null +++ b/bundle/phases/bind_test.go @@ -0,0 +1,97 @@ +package phases_test + +import ( + "encoding/json" + "os" + "testing" + + "github.com/databricks/cli/bundle" + "github.com/databricks/cli/bundle/config" + "github.com/databricks/cli/bundle/config/engine" + "github.com/databricks/cli/bundle/direct/dstate" + "github.com/databricks/cli/bundle/phases" + "github.com/databricks/cli/bundle/statemgmt" + "github.com/databricks/cli/libs/cmdctx" + "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/logdiag" + "github.com/databricks/cli/libs/testserver" + "github.com/databricks/databricks-sdk-go" + "github.com/databricks/databricks-sdk-go/service/bundledeployments" + "github.com/databricks/databricks-sdk-go/service/jobs" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestBindLocalCommitFailureCompletesVersion(t *testing.T) { + ctx, _ := cmdio.NewTestContextWithStderr(t.Context()) + ctx = logdiag.InitContext(ctx) + b := &bundle.Bundle{BundleRootPath: t.TempDir()} + root, diags := config.LoadFromBytes("databricks.yml", []byte(`bundle: + name: test + target: default + deployment: + lock: + enabled: false +experimental: + deployment_history: true +workspace: + root_path: /Workspace/test + state_path: /Workspace/test/state +resources: + jobs: + bound: + name: configured +`)) + require.Empty(t, diags) + b.Config = *root + _, statePath := b.StateFilenameDirect(ctx) + + server := testserver.New(t) + server.Handle("GET", "/api/2.2/jobs/get", func(req testserver.Request) any { + return jobs.Job{JobId: 20, Settings: &jobs.JobSettings{Name: "remote"}} + }) + server.Handle("GET", "/api/2.0/workspace/get-status", func(req testserver.Request) any { + return map[string]any{"object_id": 123, "object_type": "FILE"} + }) + server.Handle("GET", "/api/2.0/bundle/deployments/123", func(req testserver.Request) any { + return bundledeployments.Deployment{Name: "deployments/123"} + }) + server.Handle("POST", "/api/2.0/bundle/deployments/123/versions", func(req testserver.Request) any { + // The WAL is already open. Make the atomic state replacement fail after SaveState. + require.FileExists(t, statePath+".wal") + require.NoError(t, os.Mkdir(statePath, 0o700)) + return bundledeployments.Version{Name: "deployments/123/versions/1", VersionId: "1"} + }) + server.Handle("PATCH", "/api/2.0/bundle/deployments/123/versions/1/operations/jobs.bound", func(req testserver.Request) any { + return bundledeployments.Operation{} + }) + var completion bundledeployments.CompleteVersionRequest + server.Handle("POST", "/api/2.0/bundle/deployments/123/versions/1/complete", func(req testserver.Request) any { + require.NoError(t, json.Unmarshal(req.Body, &completion)) + return bundledeployments.Version{} + }) + testserver.AddDefaultHandlers(server) + client, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "testtoken"}) + require.NoError(t, err) + b.SetWorkpaceClient(client) + ctx = cmdctx.SetWorkspaceClient(ctx, client) + + phases.Bind(ctx, b, &phases.BindOptions{Group: "jobs", ResourceKey: "bound", ResourceId: "20", AutoApprove: true}, &statemgmt.StateDesc{Engine: engine.EngineDirect}) + + assert.True(t, logdiag.HasError(ctx)) + assert.False(t, b.DeploymentBundle.StateDB.IsOpen()) + assert.Equal(t, bundledeployments.VersionCompleteVersionCompleteFailure, completion.CompletionReason) + // Completion releases the operation buffer while retaining its final measurements. + require.NotNil(t, b.DeploymentBundle.StateDB.OperationBufferStats()) + completed, err := b.DeploymentBundle.StateDB.CompleteVersion(ctx, true) + require.NoError(t, err) + assert.False(t, completed) + + // Finalize closed the WAL even though persistence failed, so ordinary recovery can retry it. + require.NoError(t, os.Remove(statePath)) + var recovered dstate.DeploymentState + require.NoError(t, recovered.Open(ctx, statePath, dstate.ReadingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{})) + assert.Equal(t, "20", recovered.GetResourceID("resources.jobs.bound")) + _, err = recovered.Finalize(ctx) + require.NoError(t, err) +}