@@ -8,12 +8,14 @@ import (
88 "io/fs"
99 "os"
1010 "path/filepath"
11+ "slices"
1112
1213 "github.com/databricks/cli/bundle"
1314 "github.com/databricks/cli/bundle/config"
1415 "github.com/databricks/cli/bundle/config/engine"
1516 "github.com/databricks/cli/bundle/config/mutator/resourcemutator"
1617 "github.com/databricks/cli/bundle/deploy"
18+ "github.com/databricks/cli/bundle/deployplan"
1719 "github.com/databricks/cli/bundle/direct"
1820 "github.com/databricks/cli/bundle/direct/dresources"
1921 "github.com/databricks/cli/bundle/direct/dstate"
@@ -100,12 +102,22 @@ func MigrateTerraformState(ctx context.Context, b *bundle.Bundle, requiredEngine
100102 if commit {
101103 // Plan-check the converted state; if it fails, do not commit and fall back to the
102104 // terraform engine (the temp file is cleaned up by the deferred Remove).
103- if err := checkPlanOnTempState (ctx , b , tempStatePath , cfg ); err != nil {
105+ plan , err := checkPlanOnTempState (ctx , b , tempStatePath , cfg )
106+ if err != nil {
104107 b .Metrics .SetBoolValue (metrics .DirectMigratePlanError , true )
105108 log .Warnf (ctx , "migration to the direct engine failed its plan check; deploying on terraform this time: %v" , err )
106109 return false , nil
107110 }
108111
112+ // Do not migrate into a plan that would recreate an existing resource: a recreate
113+ // is a destroy + create and risks data loss. Fall back to terraform this run (the
114+ // migration retries next run, once any pending recreate has been applied).
115+ if recreated := recreatedResources (plan ); len (recreated ) > 0 {
116+ b .Metrics .SetBoolValue (metrics .DirectMigrateRecreatePlanned , true )
117+ log .Warnf (ctx , "migration to the direct engine would recreate %v; deploying on terraform this time" , recreated )
118+ return false , nil
119+ }
120+
109121 // Commit to the workspace by pushing resources.json (serial tf+1). This upload is
110122 // the hard commit: once it lands it outranks any leftover terraform state by serial,
111123 // so engine selection can no longer pick terraform and the cleanup in
@@ -201,11 +213,13 @@ func DryRunMigrationTelemetry(ctx context.Context, b *bundle.Bundle) {
201213}
202214
203215// checkPlanOnTempState opens the migrated state at tempStatePath in read mode,
204- // runs a full plan against it, and returns a non-nil error if the plan fails.
205- // Individual planning errors are emitted as warnings with warnPrefix so they
206- // are visible without failing the deploy. The plan is run in an isolated
207- // context so its diagnostics do not affect the deploy's own error state.
208- func checkPlanOnTempState (ctx context.Context , b * bundle.Bundle , tempStatePath string , cfg * config.Root ) error {
216+ // runs a full plan against it, and returns the plan (and a non-nil error if the
217+ // plan fails). Individual planning errors are emitted as warnings with warnPrefix
218+ // so they are visible without failing the deploy. The plan is run in an isolated
219+ // context so its diagnostics do not affect the deploy's own error state. The
220+ // returned plan lets the caller inspect the planned actions (e.g. reject a
221+ // migration that would recreate a resource).
222+ func checkPlanOnTempState (ctx context.Context , b * bundle.Bundle , tempStatePath string , cfg * config.Root ) (* deployplan.Plan , error ) {
209223 planCtx := logdiag .IsolatedContext (ctx )
210224 logdiag .SetCollect (planCtx , true )
211225 defer func () {
@@ -223,11 +237,27 @@ func checkPlanOnTempState(ctx context.Context, b *bundle.Bundle, tempStatePath s
223237 // This plan is not created with the deployment history feature enabled,
224238 // so we can safely pass false for withDeploymentHistory.
225239 if err := planBundle .StateDB .Open (planCtx , tempStatePath , false , false , dstate .WithDeploymentHistory (false ), dstate.OpenDmsArgs {}); err != nil {
226- return fmt .Errorf ("opening migrated state for plan check: %w" , err )
240+ return nil , fmt .Errorf ("opening migrated state for plan check: %w" , err )
227241 }
228242
229- _ , err := planBundle .CalculatePlan (planCtx , b .WorkspaceClient (ctx ), cfg )
230- return err
243+ return planBundle .CalculatePlan (planCtx , b .WorkspaceClient (ctx ), cfg )
244+ }
245+
246+ // recreatedResources returns the sorted keys of resources the plan would recreate
247+ // (destroy + create). A migration should not commit into such a plan: a recreate
248+ // on a resource that already exists risks destroying it, whether it comes from a
249+ // conversion that did not faithfully reproduce an immutable field or from a real
250+ // pending config change (which terraform would recreate too). In both cases the
251+ // safe choice is to stay on terraform this run and retry the migration later.
252+ func recreatedResources (plan * deployplan.Plan ) []string {
253+ var keys []string
254+ for key , entry := range plan .Plan {
255+ if entry .Action == deployplan .Recreate {
256+ keys = append (keys , key )
257+ }
258+ }
259+ slices .Sort (keys )
260+ return keys
231261}
232262
233263// BackupTerraformState moves the terraform state to .backup both remotely
0 commit comments