Skip to content

Commit 0f52d4c

Browse files
denikIsaac
andauthored
Migrate terraform state to direct before deploy (#6749)
## Changes Run the terraform→direct state migration *before* plan/deploy (after the build, so `${artifacts.*}` and library paths are resolved) instead of after a terraform deploy, so the deploy itself runs on the direct engine. The migration: - seeds id-composing fields from the deployed terraform state and compares them normalization-aware (identifier case, trailing slash), so a backend-normalized id converges while a genuine change surfaces as a recreate/rename (`reconcileIDFields`); - is prepared in memory and reversible; a deploy commits it — pushing the direct state and retiring the terraform state — only once the deploy is approved, before applying its own changes. A declined deploy drops it and stays on terraform, so a refused recreate changes nothing (a genuine recreate is gated by `--auto-approve` like any other); - falls back to terraform for that run (with a warning) if a pre-commit step fails — parse, convert, or plan check — so a failed migration never blocks a deploy that would otherwise succeed; - tags the user agent with the engine the run actually uses. `engine: terraform` still opts out. Destroy commits the migration as part of its teardown and then removes all local state, so no terraform state or backup is left behind. The terraform-engine removal is stacked on top of this PR. ## Tests Acceptance tests under `acceptance/bundle/migrate/` cover rename, cross-catalog move, normalization-converge, recreate-refused-without-`--auto-approve` (then applied with it), push/plan/backup failure handling, and clean teardown on destroy (no leftover local or remote state), across schemas, volumes, registered models and pipelines. The rename/converge/catalog-move tests also run against a real workspace (`Cloud = true`, verified on aws-cli); the storage/external-location cases run on the testserver. This pull request and its description were written by Isaac. --------- Co-authored-by: Isaac <no-reply@databricks.com>
1 parent f5baa1d commit 0f52d4c

242 files changed

Lines changed: 2486 additions & 693 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Migrate existing Terraform deployment state to the direct engine before deploying (previously done after a Terraform deploy), so the deploy runs on the direct engine. ([#6749](https://github.com/databricks/cli/pull/6749))

‎acceptance/bundle/deploy/snapshot-comparison/output.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Destroy: 2 deleted
5454

5555
=== Cleanup bundle 2
5656
>>> [CLI] bundle destroy --auto-approve
57+
Notice: migrating your bundle to direct deployment engine (https://github.com/databricks/cli/issues/6765).
5758
The following resources will be deleted:
5859
delete resources.jobs.test_job
5960
delete resources.pipelines.test_pipeline
@@ -64,4 +65,5 @@ Streaming Tables (STs) and Materialized Views (MVs) managed by them. Set 'cascad
6465

6566
All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/snapshot-test-2-[UNIQUE_NAME]/default
6667

68+
Migrated 2 resources to direct deployment engine.
6769
Destroy: 2 deleted

‎acceptance/bundle/migrate/auto-migrate-direct-only/output.txt‎

Lines changed: 0 additions & 64 deletions
This file was deleted.

‎acceptance/bundle/migrate/auto-migrate-empty-tfstate/script‎

Lines changed: 0 additions & 31 deletions
This file was deleted.

‎acceptance/bundle/migrate/auto-migrate-plan-failure/output.txt‎

Lines changed: 0 additions & 55 deletions
This file was deleted.

‎acceptance/bundle/migrate/auto-migrate-push-failure/output.txt‎

Lines changed: 0 additions & 50 deletions
This file was deleted.

‎acceptance/bundle/migrate/auto-migrate-push-failure/script‎

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
bundle:
2+
name: test-bundle
3+
4+
resources:
5+
jobs:
6+
test_job:
7+
name: "Test Auto-Migrate Apply Failure"

acceptance/bundle/migrate/added-direct-only/out.test.toml renamed to acceptance/bundle/migrate/auto/apply-failure/out.test.toml

File renamed without changes.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
=== Initial deploy on terraform
3+
>>> DATABRICKS_BUNDLE_ENGINE=terraform [CLI] bundle deploy
4+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files...
5+
Created jobs.test_job
6+
Files: 1 uploaded, 0 deleted
7+
Resources: 1 created, 0 changed, 0 deleted, 0 unchanged
8+
9+
>>> update_file.py databricks.yml Test Auto-Migrate Apply Failure Test Auto-Migrate Apply Failure (updated)
10+
11+
=== Migrating deploy: migration commits (point of no return), then the job apply fails
12+
>>> DATABRICKS_BUNDLE_ENGINE=direct errcode [CLI] bundle deploy
13+
Notice: migrating your bundle to direct deployment engine (https://github.com/databricks/cli/issues/6765).
14+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files...
15+
Migrated 1 resource to direct deployment engine.
16+
Error: cannot update resources.jobs.test_job: updating id=[NUMID]: Fault injected by test. (403 INJECTED)
17+
18+
Endpoint: POST [DATABRICKS_URL]/api/2.2/jobs/reset
19+
HTTP Status: 403 Forbidden
20+
API error_code: INJECTED
21+
API message: Fault injected by test.
22+
23+
Files: 1 uploaded, 0 deleted
24+
25+
Exit code: 1
26+
27+
>>> print_migration_telemetry
28+
direct_migrated_via_env true
29+
30+
=== On direct despite the failed apply: resources.json present, terraform state retired
31+
32+
>>> find.py \.databricks/.*(\.tfstate|resources.*\.json|\.wal)
33+
.databricks/bundle/default/resources.json
34+
.databricks/bundle/default/terraform/terraform.tfstate.backup
35+
36+
>>> MSYS_NO_PATHCONV=1 [CLI] workspace list /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/state
37+
ID Type Language Path
38+
[NUMID] FILE /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/state/deployment.json
39+
[NUMID] FILE /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/state/metadata.json
40+
[NUMID] FILE /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/state/resources.json
41+
[NUMID] FILE /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/state/terraform.tfstate.backup
42+
43+
=== Retry finishes the apply on the already-migrated direct state (no re-migration)
44+
>>> DATABRICKS_BUNDLE_ENGINE=direct [CLI] bundle deploy
45+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files...
46+
Updated jobs.test_job
47+
Files: 0 uploaded, 0 deleted
48+
Resources: 0 created, 1 changed, 0 deleted, 0 unchanged
49+
50+
>>> print_migration_telemetry
51+
52+
=== Local state remains direct
53+
54+
>>> find.py \.databricks/.*(\.tfstate|resources.*\.json|\.wal)
55+
.databricks/bundle/default/resources.json
56+
.databricks/bundle/default/terraform/terraform.tfstate.backup
57+
58+
>>> MSYS_NO_PATHCONV=1 [CLI] workspace list /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/state
59+
ID Type Language Path
60+
[NUMID] FILE /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/state/deployment.json
61+
[NUMID] FILE /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/state/metadata.json
62+
[NUMID] FILE /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/state/resources.json
63+
[NUMID] FILE /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/state/terraform.tfstate.backup

0 commit comments

Comments
 (0)