Skip to content

Commit 685e28d

Browse files
authored
Fix "migrate" to propagate --profile to "bundle plan" (#4117)
Follow up to #4088, we need to pass "-p profile" as well.
1 parent ffe8f69 commit 685e28d

File tree

8 files changed

+107
-19
lines changed

8 files changed

+107
-19
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
bundle:
2+
name: migrate-basic-test
3+
4+
resources:
5+
jobs:
6+
test_job:
7+
name: "Test Migration Job"
8+
tasks:
9+
- task_key: "main"
10+
notebook_task:
11+
notebook_path: "./notebook.py"
12+
13+
targets:
14+
dev:
15+
default: true
16+
prod:
17+
resources:
18+
schemas:
19+
test_schema:
20+
catalog_name: mycat
21+
name: myschema
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Databricks notebook source
2+
print("Hello from test migration job")

acceptance/bundle/migrate/profile_arg/out.test.toml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
>>> DATABRICKS_BUNDLE_ENGINE=terraform [CLI] bundle deploy
3+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/migrate-basic-test/dev/files...
4+
Deploying resources...
5+
Updating deployment state...
6+
Deployment complete!
7+
8+
>>> [CLI] bundle deployment migrate -p non_existent321
9+
Note: Migration should be done after a full deploy. Running plan now to verify that deployment was done:
10+
Plan: 0 to add, 0 to change, 0 to delete, 1 unchanged
11+
Migrated 1 resources to direct engine state file: [TEST_TMP_DIR]/.databricks/bundle/dev/resources.json
12+
13+
Validate the migration by running "databricks bundle plan -p non_existent321", there should be no actions planned.
14+
15+
The state file is not synchronized to the workspace yet. To do that and finalize the migration, run "bundle deploy -p non_existent321".
16+
17+
To undo the migration, remove [TEST_TMP_DIR]/.databricks/bundle/dev/resources.json and rename [TEST_TMP_DIR]/.databricks/bundle/dev/terraform/terraform.tfstate.backup to [TEST_TMP_DIR]/.databricks/bundle/dev/terraform/terraform.tfstate
18+
19+
20+
>>> DATABRICKS_BUNDLE_ENGINE=terraform [CLI] bundle deploy -t prod
21+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/migrate-basic-test/prod/files...
22+
Deploying resources...
23+
Updating deployment state...
24+
Deployment complete!
25+
26+
>>> [CLI] bundle deployment migrate -p non_existent321 -t prod
27+
Note: Migration should be done after a full deploy. Running plan now to verify that deployment was done:
28+
Plan: 0 to add, 0 to change, 0 to delete, 2 unchanged
29+
Migrated 2 resources to direct engine state file: [TEST_TMP_DIR]/.databricks/bundle/prod/resources.json
30+
31+
Validate the migration by running "databricks bundle plan -t prod -p non_existent321", there should be no actions planned.
32+
33+
The state file is not synchronized to the workspace yet. To do that and finalize the migration, run "bundle deploy -t prod -p non_existent321".
34+
35+
To undo the migration, remove [TEST_TMP_DIR]/.databricks/bundle/prod/resources.json and rename [TEST_TMP_DIR]/.databricks/bundle/prod/terraform/terraform.tfstate.backup to [TEST_TMP_DIR]/.databricks/bundle/prod/terraform/terraform.tfstate
36+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Databricks notebook source
2+
# This is a test pipeline notebook
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
trace DATABRICKS_BUNDLE_ENGINE=terraform $CLI bundle deploy
2+
3+
# only interested in output here; for some reason in test env -p is not validated
4+
trace $CLI bundle deployment migrate -p non_existent321
5+
6+
trace DATABRICKS_BUNDLE_ENGINE=terraform $CLI bundle deploy -t prod
7+
trace $CLI bundle deployment migrate -p non_existent321 -t prod
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
RecordRequests = false

cmd/bundle/deployment/migrate.go

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const backupSuffix = ".backup"
2424

2525
// runPlanCheck runs bundle plan and checks if there are any actions planned.
2626
// Returns error if plan fails or if there are actions planned.
27-
func runPlanCheck(cmd *cobra.Command, target string) error {
27+
func runPlanCheck(cmd *cobra.Command, extraArgs []string, extraArgsStr string) error {
2828
ctx := cmd.Context()
2929

3030
executable, err := os.Executable()
@@ -33,11 +33,7 @@ func runPlanCheck(cmd *cobra.Command, target string) error {
3333
}
3434

3535
args := []string{"bundle", "plan"}
36-
targetArgs := ""
37-
if target != "" {
38-
targetArgs = " -t " + target
39-
args = append(args, "-t", target)
40-
}
36+
args = append(args, extraArgs...)
4137

4238
planCmd := exec.CommandContext(ctx, executable, args...)
4339
var stdout bytes.Buffer
@@ -65,16 +61,42 @@ func runPlanCheck(cmd *cobra.Command, target string) error {
6561
}
6662

6763
if !strings.Contains(output, "Plan:") {
68-
return fmt.Errorf("cannot parse 'databricks bundle plan%s' output, aborting migration. Skip plan check with --noplancheck option", targetArgs)
64+
return fmt.Errorf("cannot parse 'databricks bundle plan%s' output, aborting migration. Skip plan check with --noplancheck option", extraArgsStr)
6965
}
7066

7167
if !strings.Contains(output, "Plan: 0 to add, 0 to change, 0 to delete") {
72-
return fmt.Errorf("'databricks bundle plan%s' shows actions planned, aborting migration. Please run 'databricks bundle deploy%s' first to ensure your bundle is up to date, If actions persist after deploy, skip plan check with --noplancheck option", targetArgs, targetArgs)
68+
return fmt.Errorf("'databricks bundle plan%s' shows actions planned, aborting migration. Please run 'databricks bundle deploy%s' first to ensure your bundle is up to date, If actions persist after deploy, skip plan check with --noplancheck option", extraArgsStr, extraArgsStr)
7369
}
7470

7571
return nil
7672
}
7773

74+
func getCommonArgs(cmd *cobra.Command) ([]string, string) {
75+
var args []string
76+
if flag := cmd.Flag("target"); flag != nil && flag.Changed {
77+
target := flag.Value.String()
78+
if target != "" {
79+
args = append(args, "-t")
80+
args = append(args, target)
81+
}
82+
}
83+
if flag := cmd.Flag("profile"); flag != nil && flag.Changed {
84+
profile := flag.Value.String()
85+
if profile != "" {
86+
args = append(args, "-p")
87+
args = append(args, profile)
88+
}
89+
}
90+
91+
argsStr := ""
92+
93+
if len(args) > 0 {
94+
argsStr = " " + strings.Join(args, " ")
95+
}
96+
97+
return args, argsStr
98+
}
99+
78100
func newMigrateCommand() *cobra.Command {
79101
cmd := &cobra.Command{
80102
Use: "migrate",
@@ -96,15 +118,7 @@ WARNING: Both direct deployment engine and this command are experimental and not
96118
cmd.Flags().BoolVar(&noPlanCheck, "noplancheck", false, "Skip running bundle plan before migration.")
97119

98120
cmd.RunE = func(cmd *cobra.Command, args []string) error {
99-
// Get the target if it was explicitly passed
100-
target := ""
101-
if flag := cmd.Flag("target"); flag != nil && flag.Changed {
102-
target = flag.Value.String()
103-
}
104-
targetArgs := ""
105-
if target != "" {
106-
targetArgs += " -t " + target
107-
}
121+
extraArgs, extraArgsStr := getCommonArgs(cmd)
108122

109123
opts := utils.ProcessOptions{
110124
SkipEngineEnvVar: true,
@@ -154,7 +168,7 @@ To start using direct engine, deploy with DATABRICKS_BUNDLE_ENGINE=direct env va
154168
// Run plan check unless --noplancheck is set
155169
if !noPlanCheck {
156170
fmt.Fprintf(cmd.OutOrStdout(), "Note: Migration should be done after a full deploy. Running plan now to verify that deployment was done:\n")
157-
if err = runPlanCheck(cmd, target); err != nil {
171+
if err = runPlanCheck(cmd, extraArgs, extraArgsStr); err != nil {
158172
return err
159173
}
160174
}
@@ -237,7 +251,7 @@ Validate the migration by running "databricks bundle plan%s", there should be no
237251
The state file is not synchronized to the workspace yet. To do that and finalize the migration, run "bundle deploy%s".
238252
239253
To undo the migration, remove %s and rename %s to %s
240-
`, len(deploymentBundle.StateDB.Data.State), localPath, targetArgs, targetArgs, localPath, localTerraformBackupPath, localTerraformPath))
254+
`, len(deploymentBundle.StateDB.Data.State), localPath, extraArgsStr, extraArgsStr, localPath, localTerraformBackupPath, localTerraformPath))
241255
return nil
242256
}
243257

0 commit comments

Comments
 (0)