Skip to content

Commit a2e6d6f

Browse files
committed
wip migrate command & test
1 parent 8b8708e commit a2e6d6f

File tree

4 files changed

+62
-67
lines changed

4 files changed

+62
-67
lines changed

acceptance/bundle/deployment/migrate/basic/databricks.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,29 @@ resources:
1212
#permissions:
1313
# - level: CAN_VIEW
1414
# user_name: [email protected]
15+
volumes:
16+
test_volume:
17+
catalog_name: "mycat"
18+
schema_name: "myschema"
19+
name: "myvol"
1520

1621
pipelines:
1722
test_pipeline:
1823
name: "Test Migration Pipeline"
24+
tags:
25+
# ids
26+
myjob_id: ${resources.jobs.test_job.id}
27+
myvolume_id: ${resources.volumes.test_volume.id}
28+
29+
# local field, string:
30+
myjob_name: ${resources.jobs.test_job.name}
31+
volume_catalog_name: ${resources.volumes.test_volume.catalog_name}
32+
33+
# remote field, int, null
34+
myjob_timeout: ${resources.jobs.test_job.timeout_seconds}
35+
36+
# remote field, string:
37+
volume_storage_location: ${resources.volumes.test_volume.storage_location}
1938
libraries:
2039
- notebook:
2140
path: "./pipeline.py"
Lines changed: 23 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
>>> musterr [CLI] bundle deployment migrate
3-
This command migrate existing terraform state file to direct deployment engine state. However, no existing local or remote state found.
3+
This command migrates existing terraform state file (terraform.tfstate) to direct deployment engine state file (resources.json). However, no existing local or remote state found.
44

55
To start using direct engine, deploy with DATABRICKS_BUNDLE_ENGINE=direct env var or bundle.engine="direct" in databricks.yml.
66

@@ -10,78 +10,40 @@ Deploying resources...
1010
Updating deployment state...
1111
Deployment complete!
1212

13-
>>> head -n 5 .databricks/bundle/default/terraform/terraform.tfstate
14-
{
15-
"version": 4,
16-
"terraform_version": "1.5.5",
17-
"serial": 3,
18-
"lineage": "[UUID]",
19-
2013
>>> [CLI] bundle deployment migrate
21-
Migrated 2 resources to direct engine state file: [TEST_TMP_DIR]/.databricks/bundle/default/resources.json
14+
Migrated 3 resources to direct engine state file: [TEST_TMP_DIR]/.databricks/bundle/default/resources.json
15+
To finalize deployment, run "bundle deploy".
16+
17+
>>> ls -1 .databricks/bundle/default/
2218
deployment.json
2319
resources.json
2420
sync-snapshots
2521
terraform
2622

27-
>>> cat .databricks/bundle/default/resources.json
23+
>>> [CLI] bundle plan
24+
update pipelines.test_pipeline
25+
26+
Plan: 0 to add, 1 to change, 0 to delete, 2 unchanged
27+
28+
>>> [CLI] bundle debug plan
29+
30+
>>> print_requests.py --get //jobs/get
2831
{
29-
"lineage": "[UUID]",
30-
"serial": 4,
31-
"state": {
32-
"resources.jobs.test_job": {
33-
"__id__": "[NUMID]",
34-
"state": {
35-
"deployment": {
36-
"kind": "BUNDLE",
37-
"metadata_file_path": "/Workspace/Users/[USERNAME]/.bundle/migrate-basic-test/default/state/metadata.json"
38-
},
39-
"edit_mode": "UI_LOCKED",
40-
"format": "MULTI_TASK",
41-
"max_concurrent_runs": 1,
42-
"name": "Test Migration Job",
43-
"queue": {
44-
"enabled": true
45-
},
46-
"tasks": [
47-
{
48-
"notebook_task": {
49-
"notebook_path": "/Workspace/Users/[USERNAME]/.bundle/migrate-basic-test/default/files/notebook"
50-
},
51-
"task_key": "main"
52-
}
32+
"headers": {
33+
"User-Agent": [
34+
"cli/[DEV_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] engine/direct auth/pat"
5335
]
54-
}
5536
},
56-
"resources.pipelines.test_pipeline": {
57-
"__id__": "[UUID]",
58-
"state": {
59-
"channel": "CURRENT",
60-
"deployment": {
61-
"kind": "BUNDLE",
62-
"metadata_file_path": "/Workspace/Users/[USERNAME]/.bundle/migrate-basic-test/default/state/metadata.json"
63-
},
64-
"edition": "ADVANCED",
65-
"libraries": [
66-
{
67-
"notebook": {
68-
"path": "/Workspace/Users/[USERNAME]/.bundle/migrate-basic-test/default/files/pipeline"
69-
}
70-
}
71-
],
72-
"name": "Test Migration Pipeline"
73-
}
37+
"method": "GET",
38+
"path": "/api/2.2/jobs/get",
39+
"q": {
40+
"job_id": "[NUMID]"
7441
}
75-
}
7642
}
77-
>>> [CLI] bundle plan
78-
Plan: 0 to add, 0 to change, 0 to delete, 2 unchanged
79-
80-
>>> print_requests.py --get //jobs/get
8143
{
8244
"headers": {
8345
"User-Agent": [
84-
"cli/[DEV_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] engine/direct auth/pat"
46+
"cli/[DEV_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_debug_plan cmd-exec-id/[UUID] engine/direct auth/pat"
8547
]
8648
},
8749
"method": "GET",
@@ -99,8 +61,9 @@ Deployment complete!
9961

10062
>>> print_requests.py --get //jobs/get
10163
"cli/[DEV_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_deploy cmd-exec-id/[UUID] engine/direct auth/pat"
64+
"cli/[DEV_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_deploy cmd-exec-id/[UUID] engine/direct auth/pat"
10265

10366
=== Should show that it's already migrated
10467
>>> musterr [CLI] bundle deployment migrate
10568
Error: already using direct engine
106-
Details: <local direct state serial=4 lineage="[UUID]">
69+
Details: <local direct state serial=5 lineage="[UUID]">

acceptance/bundle/deployment/migrate/basic/script

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
trace musterr $CLI bundle deployment migrate
33

44
trace DATABRICKS_BUNDLE_ENGINE=terraform $CLI bundle deploy
5-
trace head -n 5 .databricks/bundle/default/terraform/terraform.tfstate
5+
print_state.py > out.original_state.json
66

77
trace $CLI bundle deployment migrate
8-
9-
ls -1 .databricks/bundle/default/ | contains.py "resources.json"
10-
trace cat .databricks/bundle/default/resources.json
8+
print_state.py > out.new_state.json
9+
trace ls -1 .databricks/bundle/default/ | contains.py "resources.json"
1110

1211
rm out.requests.txt
13-
trace $CLI bundle plan
12+
trace $CLI bundle plan | contains.py "2 unchanged"
13+
trace $CLI bundle debug plan > out.plan_after_migrate.json
1414
trace print_requests.py --get //jobs/get | contains.py 'engine/direct'
1515

1616
trace $CLI bundle deploy

cmd/bundle/deployment/migrate.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/databricks/cli/cmd/bundle/utils"
1111
"github.com/databricks/cli/cmd/root"
1212
"github.com/databricks/cli/libs/cmdio"
13+
"github.com/databricks/cli/libs/logdiag"
1314
"github.com/spf13/cobra"
1415
)
1516

@@ -55,7 +56,7 @@ proper state management as it may cause resource conflicts.`,
5556
ctx := cmd.Context()
5657

5758
if stateDesc.Lineage == "" {
58-
cmdio.LogString(ctx, `This command migrate existing terraform state file to direct deployment engine state. However, no existing local or remote state found.
59+
cmdio.LogString(ctx, `This command migrates existing terraform state file (terraform.tfstate) to direct deployment engine state file (resources.json). However, no existing local or remote state found.
5960
6061
To start using direct engine, deploy with DATABRICKS_BUNDLE_ENGINE=direct env var or bundle.engine="direct" in databricks.yml.`)
6162
return root.ErrAlreadyPrinted
@@ -65,6 +66,11 @@ To start using direct engine, deploy with DATABRICKS_BUNDLE_ENGINE=direct env va
6566
return fmt.Errorf("already using direct engine\nDetails: %s", stateDesc.String())
6667
}
6768

69+
_, localTerraformPath := b.StateFilenameTerraform(ctx)
70+
if _, err = os.Stat(localTerraformPath); err != nil {
71+
return fmt.Errorf("reading %s: %w", localTerraformPath, err)
72+
}
73+
6874
terraformResources, err := terraform.ParseResourcesState(ctx, b)
6975
if err != nil {
7076
return fmt.Errorf("failed to parse terraform state: %w", err)
@@ -109,7 +115,14 @@ To start using direct engine, deploy with DATABRICKS_BUNDLE_ENGINE=direct env va
109115
if err := os.Rename(tempStatePath, localPath); err != nil {
110116
return fmt.Errorf("renaming %s to %s: %w", tempStatePath, localPath, err)
111117
}
112-
cmdio.LogString(ctx, fmt.Sprintf("Migrated %d resources to direct engine state file: %s", len(deploymentBundle.StateDB.Data.State), localPath))
118+
err = os.Remove(localTerraformPath)
119+
if err != nil {
120+
// not fatal, since we've increased serial
121+
logdiag.LogError(ctx, err)
122+
}
123+
124+
cmdio.LogString(ctx, fmt.Sprintf(`Migrated %d resources to direct engine state file: %s
125+
To finalize deployment, run "bundle deploy".`, len(deploymentBundle.StateDB.Data.State), localPath))
113126
return nil
114127
}
115128

0 commit comments

Comments
 (0)