diff --git a/.nextchanges/bundles/record-bind-history.md b/.nextchanges/bundles/record-bind-history.md new file mode 100644 index 00000000000..f937327974d --- /dev/null +++ b/.nextchanges/bundles/record-bind-history.md @@ -0,0 +1 @@ +* Record deployment history for `bundle deployment bind` when deployment history is enabled. ([#7817](https://github.com/databricks/cli/pull/7817)) diff --git a/acceptance/bin/nostamp.py b/acceptance/bin/nostamp.py index 8a0c222630c..8f370edc657 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): @@ -52,6 +54,15 @@ 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 + # 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") + 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/dashboard/recreation/script b/acceptance/bundle/deployment/bind/dashboard/recreation/script index c9656879b7e..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 > 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/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..6cc42259b0f --- /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 deployment history for bind: 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/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..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 > out.summary.json +trace $CLI bundle summary -o json | nostamp.py > out.summary.json trace $CLI bundle plan rm -f "$OUT_REQUESTS" 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..4d6fc71467e --- /dev/null +++ b/acceptance/bundle/dms/bind-immutable/databricks.yml.tmpl @@ -0,0 +1,8 @@ +bundle: + name: dms-bind-immutable-$UNIQUE_NAME +workspace: + sync_mode: immutable +resources: + jobs: + foo: + name: foo 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..451affad5d7 --- /dev/null +++ b/acceptance/bundle/dms/bind-immutable/output.txt @@ -0,0 +1,13 @@ + +>>> [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 + +>>> MSYS_NO_PATHCONV=1 [CLI] bundle-deployments list-resources deployments/[DEPLOYMENT_ID] -o json +[ + { + "last_action_type": "OPERATION_ACTION_TYPE_UPDATE", + "resource_id": "[JOB_ID]", + "resource_key": "jobs.foo" + } +] diff --git a/acceptance/bundle/dms/bind-immutable/script b/acceptance/bundle/dms/bind-immutable/script new file mode 100644 index 00000000000..929a499ddd6 --- /dev/null +++ b/acceptance/bundle/dms/bind-immutable/script @@ -0,0 +1,8 @@ +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 + +trace $CLI bundle deployment bind foo "$job_id" --auto-approve +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') +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" diff --git a/acceptance/bundle/dms/bind-immutable/test.toml b/acceptance/bundle/dms/bind-immutable/test.toml new file mode 100644 index 00000000000..ee99e628c60 --- /dev/null +++ b/acceptance/bundle/dms/bind-immutable/test.toml @@ -0,0 +1,4 @@ +# 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 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 daaa7345a94..d6a1f7c1de0 100644 --- a/acceptance/bundle/dms/bind-unbind/output.txt +++ b/acceptance/bundle/dms/bind-unbind/output.txt @@ -1,11 +1,56 @@ === 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" + ~ name: "bind-target-[UNIQUE_NAME]" -> "foo" + +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. + + +>>> assert_not_exists.py .databricks/bundle/default/resources.json .databricks/bundle/default/resources.json.wal + +>>> 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 +>>> jq {serial, features, resource_id: .state["resources.jobs.foo"].__id__} .databricks/bundle/default/resources.json +{ + "serial": 2, + "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"}} + +>>> [CLI] jobs get [JOB_ID] +{ + "job_id": [JOB_ID], + "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_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... @@ -38,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 @@ -52,6 +97,11 @@ Run 'bundle deploy' to deploy changes to your workspace "resource_id": "[JOB_ID]" } +>>> 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"}} + === A subsequent deploy continues managing the same job >>> [CLI] bundle deploy @@ -66,3 +116,13 @@ 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..5d101b99f27 100644 --- a/acceptance/bundle/dms/bind-unbind/script +++ b/acceptance/bundle/dms/bind-unbind/script @@ -1,11 +1,29 @@ 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 + errcode $CLI jobs delete "$job_id" >> LOG.cleanup 2>&1 +} +trap cleanup_bind_target EXIT + +trace musterr $CLI bundle deployment bind foo "$job_id" +# This bundle starts without state. A rejected bind must leave no committed state +# or pending WAL that a later command could recover. +trace assert_not_exists.py .databricks/bundle/default/resources.json .databricks/bundle/default/resources.json.wal +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') +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 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 +32,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}' -title "Rebind preserves the existing shadow history marker" -trace $CLI bundle deployment bind foo "$job_id" --auto-approve +rm -f "$OUT_REQUESTS" + +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 + 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..b7cf7fdd9a0 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 +Cloud = true diff --git a/acceptance/bundle/dms/bind-upload-failure/databricks.yml.tmpl b/acceptance/bundle/dms/bind-upload-failure/databricks.yml.tmpl new file mode 100644 index 00000000000..a81a42d4b34 --- /dev/null +++ b/acceptance/bundle/dms/bind-upload-failure/databricks.yml.tmpl @@ -0,0 +1,9 @@ +bundle: + name: dms-bind-upload-failure-$UNIQUE_NAME + +resources: + jobs: + kept: + name: kept + upload_failed: + name: upload_failed diff --git a/acceptance/bundle/dms/bind-upload-failure/out.test.toml b/acceptance/bundle/dms/bind-upload-failure/out.test.toml new file mode 100644 index 00000000000..88e6fbd847c --- /dev/null +++ b/acceptance/bundle/dms/bind-upload-failure/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +EnvMatrix.DMS = ["true"] diff --git a/acceptance/bundle/dms/bind-upload-failure/output.txt b/acceptance/bundle/dms/bind-upload-failure/output.txt new file mode 100644 index 00000000000..3306d260e86 --- /dev/null +++ b/acceptance/bundle/dms/bind-upload-failure/output.txt @@ -0,0 +1,24 @@ + +=== 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-upload-failure-[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 //complete --oneline +{"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/5/complete", "body": {"completion_reason": "VERSION_COMPLETE_FAILURE"}} + +=== Remote state still contains only the successful bindings + +>>> [CLI] bundle summary -o json +{ + "kept": "[JOB_ID]", + "upload_failed": null +} diff --git a/acceptance/bundle/dms/bind-upload-failure/script b/acceptance/bundle/dms/bind-upload-failure/script new file mode 100644 index 00000000000..e101c67f578 --- /dev/null +++ b/acceptance/bundle/dms/bind-upload-failure/script @@ -0,0 +1,15 @@ +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 +$CLI bundle deployment bind kept "$job_id" --auto-approve &> LOG.setup +rm -f "$OUT_REQUESTS" + +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 //complete --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-upload-failure/test.toml b/acceptance/bundle/dms/bind-upload-failure/test.toml new file mode 100644 index 00000000000..8ab24f1b817 --- /dev/null +++ b/acceptance/bundle/dms/bind-upload-failure/test.toml @@ -0,0 +1 @@ +Cloud = false # Inject a remote state-upload failure. diff --git a/acceptance/bundle/dms/suppress-transport-warnings/output.txt b/acceptance/bundle/dms/suppress-transport-warnings/output.txt index df85288efc3..7e4d5803615 100644 --- a/acceptance/bundle/dms/suppress-transport-warnings/output.txt +++ b/acceptance/bundle/dms/suppress-transport-warnings/output.txt @@ -2,3 +2,4 @@ Warn: Could not enable deployment history for this deploy: failed to create depl Warn: Could not enable deployment history for this deploy: failed to create deployment version: operations.resource_key must have a known resource type Warn: Failed to complete deployment history version: Internal error Error: cannot create resources.secrets.foo: response body: stream error: stream ID 73; INTERNAL_ERROR; received from peer +Warn: Failed to record deployment history for bind: operations.resource_key must have a known resource type diff --git a/acceptance/bundle/dms/suppress-transport-warnings/script b/acceptance/bundle/dms/suppress-transport-warnings/script index 9aeed2c3017..4eabace7c59 100644 --- a/acceptance/bundle/dms/suppress-transport-warnings/script +++ b/acceptance/bundle/dms/suppress-transport-warnings/script @@ -7,8 +7,11 @@ echo "Warn: Could not read deployment history: response body: stream error: stre echo "Warn: Could not delete deployment history: response body: stream error: stream ID 7; INTERNAL_ERROR; received from peer" echo "Warn: Failed to record deployment history: context deadline exceeded" +echo "Warn: Failed to record deployment history for bind: unexpected EOF" + # Kept: errors the service returns, and transport errors outside deployment history. echo "Warn: Could not enable deployment history for this deploy: failed to create deployment: " echo "Warn: Could not enable deployment history for this deploy: failed to create deployment version: operations.resource_key must have a known resource type" echo "Warn: Failed to complete deployment history version: Internal error" echo "Error: cannot create resources.secrets.foo: response body: stream error: stream ID 73; INTERNAL_ERROR; received from peer" +echo "Warn: Failed to record deployment history for bind: operations.resource_key must have a known resource type" diff --git a/acceptance/bundle/dms_fault/complete_version/output.txt b/acceptance/bundle/dms_fault/complete_version/output.txt index c7d574f4431..785d9afaafd 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=10 Updated pipelines.bar Files: 6 uploaded, 0 deleted Resources: 0 created, 1 changed, 0 deleted, 1 unchanged diff --git a/acceptance/bundle/dms_fault/create_deployment/output.txt b/acceptance/bundle/dms_fault/create_deployment/output.txt index 2b1b3f5a082..7f7b781fd79 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 deployment history for bind: 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 deployment history for bind: 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..20eb577bf16 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 deployment history for bind: 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 deployment history for bind: 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..89fea612a0c 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=10 Updated pipelines.bar Files: 6 uploaded, 0 deleted Resources: 0 created, 1 changed, 0 deleted, 1 unchanged diff --git a/acceptance/bundle/dms_fault/get_deployment/output.txt b/acceptance/bundle/dms_fault/get_deployment/output.txt index a2c05be253f..2d35346e952 100644 --- a/acceptance/bundle/dms_fault/get_deployment/output.txt +++ b/acceptance/bundle/dms_fault/get_deployment/output.txt @@ -58,6 +58,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 deployment history for bind: 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 @@ -77,6 +80,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 deployment history for bind: Fault injected by test. Successfully bound pipeline with an id '[BAR_ID]' >>> bundle validate diff --git a/acceptance/bundle/dms_fault/get_operation/output.txt b/acceptance/bundle/dms_fault/get_operation/output.txt index 65498809a65..df32126cffe 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=10 Updated pipelines.bar Files: 6 uploaded, 0 deleted Resources: 0 created, 1 changed, 0 deleted, 1 unchanged diff --git a/acceptance/bundle/dms_fault/get_resource/output.txt b/acceptance/bundle/dms_fault/get_resource/output.txt index a562c0f5264..be0f833f1af 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=10 Updated pipelines.bar Files: 6 uploaded, 0 deleted Resources: 0 created, 1 changed, 0 deleted, 1 unchanged diff --git a/acceptance/bundle/dms_fault/get_version/output.txt b/acceptance/bundle/dms_fault/get_version/output.txt index b4473435fe9..f97884ad109 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=10 Updated pipelines.bar Files: 6 uploaded, 0 deleted Resources: 0 created, 1 changed, 0 deleted, 1 unchanged diff --git a/acceptance/bundle/dms_fault/heartbeat/output.txt b/acceptance/bundle/dms_fault/heartbeat/output.txt index 8b50d26db05..686abbd6c18 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=10 Updated pipelines.bar Files: 6 uploaded, 0 deleted Resources: 0 created, 1 changed, 0 deleted, 1 unchanged diff --git a/acceptance/bundle/dms_fault/list_deployments/output.txt b/acceptance/bundle/dms_fault/list_deployments/output.txt index 0710bf48939..e1c3cad5d59 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=10 Updated pipelines.bar Files: 6 uploaded, 0 deleted Resources: 0 created, 1 changed, 0 deleted, 1 unchanged diff --git a/acceptance/bundle/dms_fault/list_operations/output.txt b/acceptance/bundle/dms_fault/list_operations/output.txt index 5764b81d690..065d25aa95b 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=10 Updated pipelines.bar Files: 6 uploaded, 0 deleted Resources: 0 created, 1 changed, 0 deleted, 1 unchanged diff --git a/acceptance/bundle/dms_fault/list_resources/output.txt b/acceptance/bundle/dms_fault/list_resources/output.txt index 9f45951b03a..04ce14bc877 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=10 Updated pipelines.bar Files: 6 uploaded, 0 deleted Resources: 0 created, 1 changed, 0 deleted, 1 unchanged diff --git a/acceptance/bundle/dms_fault/list_versions/output.txt b/acceptance/bundle/dms_fault/list_versions/output.txt index dabee52cbcd..071e655051e 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=10 Updated pipelines.bar Files: 6 uploaded, 0 deleted Resources: 0 created, 1 changed, 0 deleted, 1 unchanged diff --git a/acceptance/bundle/dms_fault/run.py b/acceptance/bundle/dms_fault/run.py index b996d10d2cc..52c2a0feef4 100644 --- a/acceptance/bundle/dms_fault/run.py +++ b/acceptance/bundle/dms_fault/run.py @@ -85,6 +85,10 @@ def hits(pattern): def state(cwd): return json.loads((cwd / ".databricks/bundle/default/resources.json").read_text()) + # A newer local checkpoint can hide a skipped upload, so read the remote file directly. + def remote_state(cwd, root): + return json.loads(run(cwd, "workspace", "export", root + "/state/resources.json", "--format", "RAW").stdout) + # Check the history marker and file-backed state, then collect and label resource IDs. def ids(cwd, history=True): data = state(cwd) @@ -210,6 +214,7 @@ def assert_destroyed(cwd, root, current): assert resource not in state(cwd)["state"] generate(cwd, kind, key, current[resource], bind=True) assert ids(cwd)[resource] == current[resource] + assert remote_state(cwd, root)["state"][resource]["__id__"] == current[resource] if kind == "pipeline": assert (cwd / "src/pipeline.sql").read_text() == (cwd / "pipeline.sql").read_text() run(cwd, "bundle", "validate") diff --git a/acceptance/bundle/dms_fault/update_operation/output.txt b/acceptance/bundle/dms_fault/update_operation/output.txt index 308345c48c4..3db42b8ba77 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 for bind: 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 for bind: 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=10 Updated pipelines.bar Warn: Failed to record deployment history: recording operation for [RESOURCE_KEY]: Fault injected by test. Files: 6 uploaded, 0 deleted diff --git a/bundle/direct/bind.go b/bundle/direct/bind.go index 73852cdde00..20f5095d07b 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,8 +36,16 @@ func (e ErrResourceAlreadyBound) Error() string { e.ResourceKey, e.ExistingID) } +// DMSBindMetadata holds binding data to publish after the bind commits. +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 HasChanges bool // Action is the planned action for the bound resource (e.g., "skip", "update", "recreate") @@ -97,6 +106,17 @@ 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 + } + if b.StateDB.RecordsHistory() || bundleenv.RecordsDeploymentHistory(ctx, configured) { + if b.StateDB.Data.Features == nil { + b.StateDB.Data.Features = make(map[string]struct{}) + } + // Keep the history marker, but leave the recorder detached until approval. + b.StateDB.Data.Features[dstate.FeatureDeploymentHistoryShadow] = struct{}{} + } if err := b.StateDB.UpgradeToWrite(); err != nil { os.Remove(tmpStatePath) return nil, err @@ -197,6 +217,11 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac os.Remove(tmpStatePath) return nil, err } + var dmsMetadata *DMSBindMetadata + if b.StateDB.RecordsHistory() { + 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) @@ -208,6 +233,7 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac // Check if the bound resource has changes result := &BindResult{ + DMSMetadata: dmsMetadata, HasChanges: false, Action: deployplan.Skip, Plan: plan, diff --git a/bundle/phases/bind.go b/bundle/phases/bind.go index 8f19f67a7fa..8ea2d82b0ad 100644 --- a/bundle/phases/bind.go +++ b/bundle/phases/bind.go @@ -7,17 +7,25 @@ import ( "fmt" "maps" "slices" + "strconv" + "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" + "github.com/databricks/cli/bundle/direct/dstate" "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/databricks-sdk-go/service/bundledeployments" ) type BindOptions struct { @@ -28,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 +} + func Bind(ctx context.Context, b *bundle.Bundle, opts *BindOptions, stateDesc *statemgmt.StateDesc) { log.Info(ctx, "Phase: bind") engine := stateDesc.Engine @@ -37,7 +51,10 @@ func Bind(ctx context.Context, b *bundle.Bundle, opts *BindOptions, stateDesc *s return } + var history *bindingHistory + var historyErr error defer func() { + completeBindingHistory(ctx, b, history, historyErr == nil && !logdiag.HasError(ctx)) bundle.ApplyContext(ctx, b, lock.Release(lock.GoalBind)) }() @@ -114,9 +131,33 @@ func Bind(ctx context.Context, b *bundle.Bundle, opts *BindOptions, stateDesc *s return } + history, historyErr = recordBindHistory(ctx, b, resourceKey, result.DMSMetadata) + if historyErr != nil { + log.Warnf(ctx, "Failed to record deployment history for bind: %s", historyErr) + } + 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 { + 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) + } +} + func jsonDump(ctx context.Context, v any, field string) string { b, err := json.Marshal(v) if err != nil { @@ -155,3 +196,87 @@ 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) { + w := b.WorkspaceClient(ctx) + deploymentID, current, err := dms.FetchDeployment(ctx, w, b.Config.Workspace.StatePath) + if err != nil { + return nil, err + } + previous := "" + if current != nil { + previous = current.LastVersionId + } + next, err := dms.NextVersion(previous) + if err != nil { + return nil, err + } + // Deploy owns existing metadata; binding does not publish pending config changes. + if current == nil { + deployment := bundleMetadata(b).Deployment() + deployment.InitialParentPath = b.Config.Workspace.StatePath + created, err := w.BundleDeployments.CreateDeployment(ctx, bundledeployments.CreateDeploymentRequest{Deployment: deployment}) + if err != nil { + deploymentID = recoverCreatedDeployment(ctx, b, err) + if deploymentID == "" { + return nil, fmt.Errorf("failed to create deployment: %w", err) + } + } else { + deploymentID, err = dms.DeploymentIDFromName(created.Name) + if err != nil { + return nil, err + } + } + } + // Binding has committed file state already; both counters still bound the next version. + versionID := max(next, serial+1) + versionInfo := bundledeployments.Version{ + CliVersion: build.GetInfo().Version, + VersionType: dms.VersionTypeDeploy, + PreviousVersionId: previous, + Operations: staged, + } + if git := b.Config.Bundle.Git; git.Branch != "" || git.Commit != "" || git.OriginURL != "" { + versionInfo.GitInfo = &bundledeployments.GitInfo{Branch: git.Branch, Commit: git.Commit, OriginUrl: git.OriginURL} + } + version, err := w.BundleDeployments.CreateVersion(ctx, bundledeployments.CreateVersionRequest{ + Parent: dms.DeploymentName(deploymentID), + VersionId: strconv.Itoa(versionID), + Version: versionInfo, + }) + 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 +}