From 0d11ce2768ce22cc6ec2e578c119ab6ab9449dcf Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Wed, 25 Nov 2026 15:43:59 +0000 Subject: [PATCH 01/22] direct: check the internal id before updating or deleting what is at the recorded name A schema or volume someone else created under the recorded name, after ours was renamed out of band, was planned as an update and deleted by destroy. The plan now treats it as not ours. Destroy, which has no config to give a renamed resource's new name, finds it by its internal id (listing the parent) and deletes it there. Co-authored-by: Isaac --- .../bundles/schema-volume-identity.md | 1 + .../output.txt | 26 ++-- .../destroy_after_rename_out_of_band/script | 6 +- .../test.toml | 1 - .../foreign_at_recorded_name/output.txt | 39 ++---- .../schemas/foreign_at_recorded_name/script | 10 +- .../foreign_at_recorded_name/test.toml | 1 - .../databricks.yml.tmpl | 9 ++ .../foreign_at_recorded_name/out.test.toml | 2 + .../foreign_at_recorded_name/output.txt | 56 +++++++++ .../volumes/foreign_at_recorded_name/script | 34 ++++++ .../foreign_at_recorded_name/test.toml | 7 ++ bundle/direct/apply.go | 7 +- bundle/direct/bundle_apply.go | 2 +- bundle/direct/bundle_plan.go | 114 +++++++++++++++++- bundle/direct/dresources/README.md | 2 + bundle/direct/dresources/adapter.go | 36 ++++++ bundle/direct/dresources/schema.go | 15 +++ bundle/direct/dresources/volume.go | 16 +++ libs/testserver/handlers.go | 8 ++ libs/testserver/schemas.go | 16 +++ libs/testserver/volumes.go | 24 ++++ 22 files changed, 379 insertions(+), 53 deletions(-) create mode 100644 .nextchanges/bundles/schema-volume-identity.md create mode 100644 acceptance/bundle/resources/volumes/foreign_at_recorded_name/databricks.yml.tmpl create mode 100644 acceptance/bundle/resources/volumes/foreign_at_recorded_name/out.test.toml create mode 100644 acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt create mode 100644 acceptance/bundle/resources/volumes/foreign_at_recorded_name/script create mode 100644 acceptance/bundle/resources/volumes/foreign_at_recorded_name/test.toml diff --git a/.nextchanges/bundles/schema-volume-identity.md b/.nextchanges/bundles/schema-volume-identity.md new file mode 100644 index 00000000000..f1c43cb178f --- /dev/null +++ b/.nextchanges/bundles/schema-volume-identity.md @@ -0,0 +1 @@ +* Fixed `bundle plan`, `bundle deploy` and `bundle destroy` taking a schema or volume that someone else created under the name of the bundle's own (renamed out of band) for the bundle's: destroy deleted it. The plan now checks the recorded `schema_id`/`volume_id`, and destroy finds and deletes a schema or volume the bundle deployed that was renamed out of band, in the direct deployment engine. diff --git a/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/output.txt b/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/output.txt index 0997557db9b..960536d12d9 100644 --- a/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/output.txt +++ b/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/output.txt @@ -18,19 +18,29 @@ Resources: 2 created, 0 changed, 0 deleted, 0 unchanged >>> update_file.py databricks.yml test-schema- test-schema-renamed- -=== Destroy +=== Destroy finds the schema at its new name by its schema_id and deletes it, with its volume >>> [CLI] bundle destroy --auto-approve -All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default +The following resources will be deleted: + delete resources.schemas.schema1 -Destroy: 0 deleted +This action will result in the deletion of the following UC schemas. Any underlying data may be lost: + delete resources.schemas.schema1 ->>> print_requests.py //unity-catalog +All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default -=== The renamed schema is still there +Destroy: 1 deleted ->>> [CLI] schemas get main.test-schema-renamed-[UNIQUE_NAME] +>>> print_requests.py //unity-catalog { - "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", - "schema_id": "[SCHEMA_ID]" + "method": "DELETE", + "path": "/api/2.1/unity-catalog/schemas/main.test-schema-renamed-[UNIQUE_NAME]", + "q": { + "force": "true" + } } + +=== The renamed schema is gone, and the volume with it + +>>> musterr [CLI] schemas get main.test-schema-renamed-[UNIQUE_NAME] +Error: Schema 'main.test-schema-renamed-[UNIQUE_NAME]' does not exist. diff --git a/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/script b/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/script index 5917b662e47..53ea2924014 100644 --- a/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/script +++ b/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/script @@ -22,9 +22,9 @@ trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE print_requests.py //unity-catalog &> LOG.rename_requests trace update_file.py databricks.yml test-schema- test-schema-renamed- -title "Destroy" +title "Destroy finds the schema at its new name by its schema_id and deletes it, with its volume" trace $CLI bundle destroy --auto-approve trace print_requests.py //unity-catalog -title "The renamed schema is still there" -trace $CLI schemas get "$NEW_SCHEMA" | jq '{full_name, schema_id}' +title "The renamed schema is gone, and the volume with it" +trace musterr $CLI schemas get "$NEW_SCHEMA" diff --git a/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/test.toml b/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/test.toml index c982f8a9fc2..4272a80cf61 100644 --- a/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/test.toml +++ b/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/test.toml @@ -1,4 +1,3 @@ -Badness = "Destroy does not look for a schema renamed outside the bundle: it reads the recorded name, finds nothing and drops the state entry ('Destroy: 0 deleted'), so the renamed schema and its volume are left behind. Destroy should find the schema by its recorded schema_id, as deploy's plan does, and delete it." Cloud = true Ignore = [ diff --git a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt index fcfa9eb6c55..049088afc47 100644 --- a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt +++ b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt @@ -15,12 +15,12 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged "schema_id": "[SCHEMA_ID]" } -=== The plan takes the other schema for the bundle's, though its schema_id differs, and plans to update it +=== The other schema has another schema_id, so the plan does not take it for the bundle's: it plans a create >>> [CLI] bundle plan --output json { "resources.schemas.schema1": { - "action": "update", + "action": "create", "new_state": { "value": { "catalog_name": "main", @@ -28,34 +28,17 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged "name": "test-schema-[UNIQUE_NAME]" } }, - "remote_state": { - "browse_only": false, - "catalog_name": "main", - "catalog_type": "MANAGED_CATALOG", - "created_by": "[USERNAME]", - "enable_predictive_optimization": "INHERIT", - "full_name": "main.test-schema-[UNIQUE_NAME]", - "name": "test-schema-[UNIQUE_NAME]", - "owner": "[USERNAME]", - "schema_id": "[FOREIGN_SCHEMA_ID]" - }, "changes": { - "comment": { - "action": "update", - "old": "c1", - "new": "c1" - }, "schema_id": { "action": "skip", "reason": "internal_id", - "old": "[SCHEMA_ID]", - "remote": "[FOREIGN_SCHEMA_ID]" + "old": "[SCHEMA_ID]" } } } } -=== Destroy deletes the other schema +=== Destroy finds the bundle's schema by its schema_id and deletes it, not the other one >>> [CLI] bundle destroy --auto-approve The following resources will be deleted: @@ -71,19 +54,19 @@ Destroy: 1 deleted >>> print_requests.py //unity-catalog { "method": "DELETE", - "path": "/api/2.1/unity-catalog/schemas/main.test-schema-[UNIQUE_NAME]", + "path": "/api/2.1/unity-catalog/schemas/main.test-schema-moved-[UNIQUE_NAME]", "q": { "force": "true" } } ->>> musterr [CLI] schemas get main.test-schema-[UNIQUE_NAME] -Error: Schema 'main.test-schema-[UNIQUE_NAME]' does not exist. +>>> musterr [CLI] schemas get main.test-schema-moved-[UNIQUE_NAME] +Error: Schema 'main.test-schema-moved-[UNIQUE_NAME]' does not exist. -=== The bundle's own schema is left behind +=== The other schema is left alone ->>> [CLI] schemas get main.test-schema-moved-[UNIQUE_NAME] +>>> [CLI] schemas get main.test-schema-[UNIQUE_NAME] { - "full_name": "main.test-schema-moved-[UNIQUE_NAME]", - "schema_id": "[SCHEMA_ID]" + "full_name": "main.test-schema-[UNIQUE_NAME]", + "schema_id": "[FOREIGN_SCHEMA_ID]" } diff --git a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script index d47f32d6805..640227098b7 100644 --- a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script +++ b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script @@ -28,13 +28,13 @@ trace $CLI schemas update "$SCHEMA" --new-name "test-schema-moved-${UNIQUE_NAME} add_repl "$($CLI schemas create "test-schema-${UNIQUE_NAME}" main | jq -r .schema_id)" FOREIGN_SCHEMA_ID print_requests.py //unity-catalog &> LOG.oob_requests -title "The plan takes the other schema for the bundle's, though its schema_id differs, and plans to update it" +title "The other schema has another schema_id, so the plan does not take it for the bundle's: it plans a create" plan -title "Destroy deletes the other schema" +title "Destroy finds the bundle's schema by its schema_id and deletes it, not the other one" trace $CLI bundle destroy --auto-approve trace print_requests.py //unity-catalog -trace musterr $CLI schemas get "$SCHEMA" +trace musterr $CLI schemas get "$MOVED_SCHEMA" -title "The bundle's own schema is left behind" -trace $CLI schemas get "$MOVED_SCHEMA" | jq '{full_name, schema_id}' +title "The other schema is left alone" +trace $CLI schemas get "$SCHEMA" | jq '{full_name, schema_id}' diff --git a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/test.toml b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/test.toml index 434acb5e029..4272a80cf61 100644 --- a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/test.toml +++ b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/test.toml @@ -1,4 +1,3 @@ -Badness = "The plan reads the recorded name and takes whatever schema is there without checking schema_id: a schema someone else created at that name after ours was renamed is planned as an update and then deleted by destroy, while ours is left behind. A schema_id mismatch should mean the bundle's schema is not there, and the plan should look for it by its schema_id." Cloud = true Ignore = [ diff --git a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/databricks.yml.tmpl b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/databricks.yml.tmpl new file mode 100644 index 00000000000..1c96e8b4c23 --- /dev/null +++ b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/databricks.yml.tmpl @@ -0,0 +1,9 @@ +bundle: + name: test-bundle-$UNIQUE_NAME + +resources: + volumes: + volume1: + catalog_name: main + schema_name: test-schema-$UNIQUE_NAME + name: myvolume diff --git a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/out.test.toml b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/out.test.toml new file mode 100644 index 00000000000..0f167effef1 --- /dev/null +++ b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/out.test.toml @@ -0,0 +1,2 @@ +Cloud = true +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt new file mode 100644 index 00000000000..e232a75a953 --- /dev/null +++ b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt @@ -0,0 +1,56 @@ + +=== Deploy a volume + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Created volumes.volume1 +Files: 2 uploaded, 0 deleted +Resources: 1 created, 0 changed, 0 deleted, 0 unchanged + +=== Out of band, rename the volume away, and create another one under its name + +>>> [CLI] volumes update main.test-schema-[UNIQUE_NAME].myvolume --new-name myvolume-moved +{ + "full_name": "main.test-schema-[UNIQUE_NAME].myvolume-moved", + "volume_id": "[VOLUME_ID]" +} + +=== The other volume has another volume_id, so the plan does not take it for the bundle's: it plans a create + +>>> [CLI] bundle plan +create volumes.volume1 + +Plan: 1 to add, 0 to change, 0 to delete, 0 unchanged + +=== Destroy finds the bundle's volume by its volume_id and deletes it, not the other one + +>>> [CLI] bundle destroy --auto-approve +The following resources will be deleted: + delete resources.volumes.volume1 + +This action will result in the deletion of the following volumes. +For managed volumes, the files stored in the volume are also deleted from your +cloud tenant within 30 days. For external volumes, the metadata about the volume +is removed from the catalog, but the underlying files are not deleted: + delete resources.volumes.volume1 + +All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default + +Destroy: 1 deleted + +>>> print_requests.py //unity-catalog +{ + "method": "DELETE", + "path": "/api/2.1/unity-catalog/volumes/main.test-schema-[UNIQUE_NAME].myvolume-moved" +} + +>>> musterr [CLI] volumes read main.test-schema-[UNIQUE_NAME].myvolume-moved +Error: Volume 'main.test-schema-[UNIQUE_NAME].myvolume-moved' does not exist. + +=== The other volume is left alone + +>>> [CLI] volumes read main.test-schema-[UNIQUE_NAME].myvolume +{ + "full_name": "main.test-schema-[UNIQUE_NAME].myvolume", + "volume_id": "[FOREIGN_VOLUME_ID]" +} diff --git a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script new file mode 100644 index 00000000000..c20ab60fcef --- /dev/null +++ b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script @@ -0,0 +1,34 @@ +envsubst < databricks.yml.tmpl > databricks.yml + +# The schema is not in the bundle, so destroy deletes the volume itself rather than its schema. +SCHEMA="main.test-schema-${UNIQUE_NAME}" +$CLI schemas create "test-schema-${UNIQUE_NAME}" main > /dev/null + +cleanup() { + errcode $CLI schemas delete "$SCHEMA" --force > LOG.cleanup 2>&1 + + # Consume the destroy's requests so none are left recorded. + print_requests.py //unity-catalog &> LOG.destroy_requests +} +trap cleanup EXIT + +title "Deploy a volume" +trace $CLI bundle deploy +print_requests.py //unity-catalog &> LOG.deploy_requests +add_repl "$($CLI volumes read "$SCHEMA.myvolume" | jq -r .volume_id)" VOLUME_ID + +title "Out of band, rename the volume away, and create another one under its name" +trace $CLI volumes update "$SCHEMA.myvolume" --new-name myvolume-moved | jq '{full_name, volume_id}' +add_repl "$($CLI volumes create main "test-schema-${UNIQUE_NAME}" myvolume MANAGED | jq -r .volume_id)" FOREIGN_VOLUME_ID +print_requests.py //unity-catalog &> LOG.oob_requests + +title "The other volume has another volume_id, so the plan does not take it for the bundle's: it plans a create" +trace $CLI bundle plan + +title "Destroy finds the bundle's volume by its volume_id and deletes it, not the other one" +trace $CLI bundle destroy --auto-approve +trace print_requests.py //unity-catalog +trace musterr $CLI volumes read "$SCHEMA.myvolume-moved" + +title "The other volume is left alone" +trace $CLI volumes read "$SCHEMA.myvolume" | jq '{full_name, volume_id}' diff --git a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/test.toml b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/test.toml new file mode 100644 index 00000000000..4272a80cf61 --- /dev/null +++ b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/test.toml @@ -0,0 +1,7 @@ +Cloud = true + +Ignore = [ + ".databricks", + ".gitignore", + "databricks.yml", +] diff --git a/bundle/direct/apply.go b/bundle/direct/apply.go index d886b478886..ed7a418ccab 100644 --- a/bundle/direct/apply.go +++ b/bundle/direct/apply.go @@ -1,6 +1,7 @@ package direct import ( + "cmp" "context" "encoding/json" "errors" @@ -23,7 +24,9 @@ func (d *DeploymentUnit) withResourceKey(ctx context.Context) context.Context { return dresources.WithResourceKey(ctx, strings.TrimPrefix(d.ResourceKey, "resources.")) } -func (d *DeploymentUnit) Destroy(ctx context.Context, db *dstate.DeploymentState) error { +// Destroy deletes the resource at its recorded ID, or at movedID when the plan found it there (see +// findByInternalID). +func (d *DeploymentUnit) Destroy(ctx context.Context, db *dstate.DeploymentState, movedID string) error { ctx = log.WithPrefix(ctx, "destroying "+d.ResourceKey) ctx = d.withResourceKey(ctx) id := db.GetResourceID(d.ResourceKey) @@ -32,7 +35,7 @@ func (d *DeploymentUnit) Destroy(ctx context.Context, db *dstate.DeploymentState return nil } - return d.Delete(ctx, db, id) + return d.Delete(ctx, db, cmp.Or(movedID, id)) } func (d *DeploymentUnit) Deploy(ctx context.Context, db *dstate.DeploymentState, newState any, actionType deployplan.ActionType, planEntry *deployplan.PlanEntry) error { diff --git a/bundle/direct/bundle_apply.go b/bundle/direct/bundle_apply.go index 60e1b66bb47..01cf08e3ebf 100644 --- a/bundle/direct/bundle_apply.go +++ b/bundle/direct/bundle_apply.go @@ -121,7 +121,7 @@ func (b *DeploymentBundle) Apply(ctx context.Context, client *databricks.Workspa // without calling the delete API. err = b.StateDB.DeleteState(ctx, resourceKey, false) } else { - err = d.Destroy(ctx, &b.StateDB) + err = d.Destroy(ctx, &b.StateDB, entry.ID) } if err != nil { logdiag.LogError(ctx, fmt.Errorf("%s: %w", errorPrefix, err)) diff --git a/bundle/direct/bundle_plan.go b/bundle/direct/bundle_plan.go index 9a3b20cebb3..7c536e26768 100644 --- a/bundle/direct/bundle_plan.go +++ b/bundle/direct/bundle_plan.go @@ -245,12 +245,10 @@ func (b *DeploymentBundle) CalculatePlan(ctx context.Context, client *databricks remoteState, err := retry.With(ctx, retry.IsTransientRead, func() (any, error) { return adapter.DoRead(ctx, id) }) + missing := false if err != nil { if apierr.IsMissing(err) { - // The resource is already deleted remotely. Keep the Delete entry so - // that applying it removes the stale state entry, but mark it Gone so - // apply skips the delete call and prompts don't list it as a deletion. - entry.Gone = true + missing = true } else { log.Warnf(ctx, "reading %s id=%q: %s", resourceKey, id, err) // This is not an error during deletion, so don't return false here @@ -262,6 +260,41 @@ func (b *DeploymentBundle) CalculatePlan(ctx context.Context, client *databricks entry.Gone = true } + var savedState any + if adapter.HasInternalID() && (missing || (err == nil && !entry.Gone)) { + dbentry, _ := b.StateDB.GetResourceEntry(resourceKey) + savedState, err = parseState(adapter.StateType(), dbentry.State) + if err != nil { + logdiag.LogError(ctx, fmt.Errorf("%s: interpreting state: %w", errorPrefix, err)) + return false + } + } + + if !missing && !entry.Gone && savedState != nil { + other, err := isOtherResource(adapter, savedState, remoteState) + if err != nil { + logdiag.LogError(ctx, fmt.Errorf("%s: %w", errorPrefix, err)) + return false + } + // Never delete what someone else created under the name ours had. + missing = other + } + + if missing { + remoteState = nil + if savedState != nil { + entry.ID, remoteState, err = findByInternalID(ctx, adapter, savedState) + if err != nil { + logdiag.LogError(ctx, fmt.Errorf("%s: %w", errorPrefix, err)) + return false + } + } + // Not found anywhere: the resource is already deleted remotely. Keep the Delete + // entry so that applying it removes the stale state entry, but mark it Gone so + // apply skips the delete call and prompts don't list it as a deletion. + entry.Gone = remoteState == nil + } + entry.RemoteState = remoteState return true @@ -349,6 +382,16 @@ func (b *DeploymentBundle) CalculatePlan(ctx context.Context, client *databricks return false } } + // Someone else's resource under the name ours had is not ours: plan as if ours were not + // there, so the plan neither updates nor later deletes it. + other, err := isOtherResource(adapter, savedState, remoteState) + if err != nil { + logdiag.LogError(ctx, fmt.Errorf("%s: %w", errorPrefix, err)) + return false + } + if other { + remoteState = nil + } if remoteState == nil { entry.ID, remoteState, err = findMoved(ctx, adapter, sv, dbentry.ID, savedState) if err != nil { @@ -956,6 +999,69 @@ func findMoved(ctx context.Context, adapter *dresources.Adapter, sv *structvar.S return id, remote, nil } +// isOtherResource reports whether remote, read at the recorded ID, is another object than the one +// savedState records: both have an internal ID and they differ. The resource was renamed away and +// another one created under its old name. +func isOtherResource(adapter *dresources.Adapter, savedState, remote any) (bool, error) { + if !adapter.HasInternalID() || remote == nil { + return false, nil + } + saved, err := adapter.InternalID(savedState) + if err != nil || saved == "" { + return false, err + } + remapped, err := adapter.RemapState(remote) + if err != nil { + return false, err + } + found, err := adapter.InternalID(remapped) + return found != "" && found != saved, err +} + +// findByInternalID looks for a resource to be deleted that is not at its recorded ID: renamed out of +// band. Destroy has no config to give its new name, as findMoved uses, so the resource searches for +// it (DoFindByInternalID). As in findMoved, what is found is taken only if its internal ID matches. +// A failed search finds nothing: the delete then only drops the state entry, as it did before. +func findByInternalID(ctx context.Context, adapter *dresources.Adapter, savedState any) (string, any, error) { + if !adapter.HasFindByInternalID() { + return "", nil, nil + } + savedInternalID, err := adapter.InternalID(savedState) + if err != nil || savedInternalID == "" { + return "", nil, err + } + id, err := retry.With(ctx, retry.IsTransientRead, func() (string, error) { + return adapter.DoFindByInternalID(ctx, savedState) + }) + if err != nil { + if !apierr.IsMissing(err) { + log.Warnf(ctx, "looking for a renamed resource by its internal id %q: %s", savedInternalID, err) + } + return "", nil, nil + } + if id == "" { + return "", nil, nil + } + remote, err := retry.With(ctx, retry.IsTransientRead, func() (any, error) { + return adapter.DoRead(ctx, id) + }) + if err != nil { + if !apierr.IsMissing(err) { + log.Warnf(ctx, "reading a renamed resource at id=%q: %s", id, err) + } + return "", nil, nil + } + found, err := adapter.RemapState(remote) + if err != nil { + return "", nil, err + } + foundInternalID, err := adapter.InternalID(found) + if err != nil || foundInternalID != savedInternalID { + return "", nil, err + } + return id, remote, nil +} + // missingInternalID reports whether savedState lacks the internal ID that remote, the remote state // remapped to the state type, has. func missingInternalID(adapter *dresources.Adapter, savedState, remote any) (bool, error) { diff --git a/bundle/direct/dresources/README.md b/bundle/direct/dresources/README.md index 6a55f1fc109..a77df917a92 100644 --- a/bundle/direct/dresources/README.md +++ b/bundle/direct/dresources/README.md @@ -62,6 +62,8 @@ If the ID is built from the resource's own fields (a UC full name), implement `I If the backend also has its own ID that a rename keeps (a volume's `volume_id`), add it to the state type, copy it in `RemapState`, and name it in the config as `internal_id_field`. The framework records it on every save and skips its changes in the plan. When nothing is at the recorded ID, the plan reads at the ID the config gives the resource and takes what it finds only if the internal ID matches. This covers a rename done out of band, or by a deploy that stopped before re-keying the resource. +An object at the recorded ID with a different internal ID is someone else's (ours was renamed away and another created under its name): the plan treats ours as not there, so it never updates or deletes the other object. Destroy has no config to give the new name, so to delete a resource renamed out of band it needs `DoFindByInternalID`: it searches the parent (lists the schemas in the catalog, the volumes in the schema) for the recorded internal ID. Without it, destroy only drops the state entry of a resource not found at its recorded ID. + ## Empty states: IsEmptyState If a desired state describes no resource at all (e.g. an empty grants list), implement `IsEmptyState`. The planner omits such a node instead of planning a create, and apply drops its state entry instead of persisting one. diff --git a/bundle/direct/dresources/adapter.go b/bundle/direct/dresources/adapter.go index 538cfcdd8e3..75a2729d251 100644 --- a/bundle/direct/dresources/adapter.go +++ b/bundle/direct/dresources/adapter.go @@ -125,6 +125,13 @@ type IResource interface { // parent's rename is applied without calling DoUpdateWithID (see PlanEntry.StateOnly). // Example: func (*ResourceVolume) IDFromState(state *VolumeState) string IDFromState(state any) string + + // [Optional] DoFindByInternalID returns the ID of the resource whose internal ID (see + // internal_id_field) is the one in state, or "" if none is found. It looks where the resource + // can be without its name: destroy has no config to give the new name of a resource renamed + // out of band. The engine reads what it returns and checks the internal ID again. + // Example: func (r *ResourceSchema) DoFindByInternalID(ctx context.Context, state *SchemaState) (string, error) + DoFindByInternalID(ctx context.Context, state any) (string, error) } // Adapter wraps resource implementation, validates signatures and type consistency across methods @@ -149,6 +156,7 @@ type Adapter struct { doResize *calladapt.BoundCaller isGone *calladapt.BoundCaller idFromState *calladapt.BoundCaller + findByInternalID *calladapt.BoundCaller resourceConfig *ResourceLifecycleConfig generatedResourceConfig *ResourceLifecycleConfig @@ -194,6 +202,7 @@ func NewAdapter(typedNil any, resourceType string, client *databricks.WorkspaceC overrideChangeDesc: nil, isGone: nil, idFromState: nil, + findByInternalID: nil, internalIDPath: nil, resourceConfig: GetResourceConfig(resourceType), generatedResourceConfig: GetGeneratedResourceConfig(resourceType), @@ -326,6 +335,11 @@ func (a *Adapter) initMethods(resource any) error { return err } + a.findByInternalID, err = calladapt.PrepareCall(resource, reflect.TypeFor[IResource](), "DoFindByInternalID") + if err != nil { + return err + } + keyedSlicesCall, err := calladapt.PrepareCall(resource, reflect.TypeFor[IResource](), "KeyedSlices") if err != nil { return err @@ -438,6 +452,13 @@ func (a *Adapter) validate() error { a.internalIDPath = path } + if a.findByInternalID != nil { + if a.internalIDPath == nil { + return errors.New("DoFindByInternalID requires internal_id_field") + } + validations = append(validations, "DoFindByInternalID state", a.findByInternalID.InTypes[1], stateType) + } + if a.resourceConfig != nil && len(a.resourceConfig.ParentIDFields) > 0 && a.idFromState == nil { return errors.New("parent_id_fields requires IDFromState") } @@ -802,6 +823,21 @@ func (a *Adapter) InternalID(state any) (string, error) { return id, nil } +// HasFindByInternalID reports whether the resource implements DoFindByInternalID. +func (a *Adapter) HasFindByInternalID() bool { + return a.findByInternalID != nil +} + +// DoFindByInternalID returns the ID of the resource with the internal ID recorded in state, "" if +// none is found; see IResource.DoFindByInternalID. +func (a *Adapter) DoFindByInternalID(ctx context.Context, state any) (string, error) { + outs, err := a.findByInternalID.Call(ctx, state) + if err != nil { + return "", err + } + return outs[0].(string), nil +} + // RecordInternalID copies the internal ID from remoteState into state, which is about to be saved. // Config never sets it, so without this the saved state would lose it. func (a *Adapter) RecordInternalID(state, remoteState any) error { diff --git a/bundle/direct/dresources/schema.go b/bundle/direct/dresources/schema.go index dc8ea85d305..f053a3642db 100644 --- a/bundle/direct/dresources/schema.go +++ b/bundle/direct/dresources/schema.go @@ -164,6 +164,21 @@ func (*ResourceSchema) IDFromState(state *SchemaState) string { return state.CatalogName + "." + state.Name } +// DoFindByInternalID lists the schemas in the catalog for the one with the recorded schema_id. +func (r *ResourceSchema) DoFindByInternalID(ctx context.Context, state *SchemaState) (string, error) { + iter := r.client.Schemas.List(ctx, catalog.ListSchemasRequest{CatalogName: state.CatalogName}) + for iter.HasNext(ctx) { + schema, err := iter.Next(ctx) + if err != nil { + return "", err + } + if schema.SchemaId == state.SchemaId { + return schema.FullName, nil + } + } + return "", nil +} + func (r *ResourceSchema) DoDelete(ctx context.Context, id string, _ *SchemaState) error { return r.client.Schemas.Delete(ctx, catalog.DeleteSchemaRequest{ FullName: id, diff --git a/bundle/direct/dresources/volume.go b/bundle/direct/dresources/volume.go index 96d49e268af..15252e13976 100644 --- a/bundle/direct/dresources/volume.go +++ b/bundle/direct/dresources/volume.go @@ -165,6 +165,22 @@ func (*ResourceVolume) IDFromState(state *VolumeState) string { return state.CatalogName + "." + state.SchemaName + "." + state.Name } +// DoFindByInternalID lists the volumes in the schema for the one with the recorded volume_id. A +// volume whose schema was renamed is not found: deleting the schema deletes it. +func (r *ResourceVolume) DoFindByInternalID(ctx context.Context, state *VolumeState) (string, error) { + iter := r.client.Volumes.List(ctx, catalog.ListVolumesRequest{CatalogName: state.CatalogName, SchemaName: state.SchemaName}) + for iter.HasNext(ctx) { + volume, err := iter.Next(ctx) + if err != nil { + return "", err + } + if volume.VolumeId == state.VolumeId { + return volume.FullName, nil + } + } + return "", nil +} + func (r *ResourceVolume) DoDelete(ctx context.Context, id string, _ *VolumeState) error { return r.client.Volumes.DeleteByName(ctx, id) } diff --git a/libs/testserver/handlers.go b/libs/testserver/handlers.go index 46e762c0522..73270a1078e 100644 --- a/libs/testserver/handlers.go +++ b/libs/testserver/handlers.go @@ -854,6 +854,10 @@ func AddDefaultHandlers(server *Server) { // Schemas: + server.Handle("GET", "/api/2.1/unity-catalog/schemas", func(req Request) any { + return req.Workspace.SchemasList(req) + }) + server.Handle("GET", "/api/2.1/unity-catalog/schemas/{full_name}", func(req Request) any { return MapGetUC(req.Workspace, req.Workspace.Schemas, req.Vars["full_name"], "Schema") }) @@ -1078,6 +1082,10 @@ func AddDefaultHandlers(server *Server) { // Volumes: looked up case-insensitively, see volumeKey. + server.Handle("GET", "/api/2.1/unity-catalog/volumes", func(req Request) any { + return req.Workspace.VolumesList(req) + }) + server.Handle("GET", "/api/2.1/unity-catalog/volumes/{full_name}", func(req Request) any { return MapGetUC(req.Workspace, req.Workspace.Volumes, volumeKey(req.Vars["full_name"]), "Volume") }) diff --git a/libs/testserver/schemas.go b/libs/testserver/schemas.go index c934adcdc15..d143e9620e1 100644 --- a/libs/testserver/schemas.go +++ b/libs/testserver/schemas.go @@ -3,7 +3,9 @@ package testserver import ( "encoding/json" "fmt" + "maps" "net/http" + "slices" "strings" "github.com/databricks/databricks-sdk-go/service/catalog" @@ -184,3 +186,17 @@ func (s *FakeWorkspace) SchemasDelete(req Request, fullName string) Response { } return Response{} } + +// SchemasList returns the schemas in a catalog, in one page. +func (s *FakeWorkspace) SchemasList(req Request) Response { + defer s.LockUnlock()() + + catalogName := req.URL.Query().Get("catalog_name") + result := catalog.ListSchemasResponse{Schemas: []catalog.SchemaInfo{}} + for _, key := range slices.Sorted(maps.Keys(s.Schemas)) { + if schema := s.Schemas[key]; schema.CatalogName == catalogName { + result.Schemas = append(result.Schemas, schema) + } + } + return Response{Body: result} +} diff --git a/libs/testserver/volumes.go b/libs/testserver/volumes.go index 2a23d4471e4..c8ce77cb753 100644 --- a/libs/testserver/volumes.go +++ b/libs/testserver/volumes.go @@ -3,7 +3,9 @@ package testserver import ( "encoding/json" "fmt" + "maps" "net/http" + "slices" "strings" "github.com/databricks/databricks-sdk-go/service/catalog" @@ -113,3 +115,25 @@ func (s *FakeWorkspace) VolumesUpdate(req Request, fullname string) Response { Body: existing, } } + +// VolumesList returns the volumes in a schema, in one page. Like UC, it fails for a schema that +// does not exist. +func (s *FakeWorkspace) VolumesList(req Request) Response { + defer s.LockUnlock()() + + catalogName := req.URL.Query().Get("catalog_name") + schemaName := req.URL.Query().Get("schema_name") + if _, ok := s.Schemas[catalogName+"."+schemaName]; !ok { + return Response{ + StatusCode: http.StatusNotFound, + Body: map[string]string{"error_code": "SCHEMA_DOES_NOT_EXIST", "message": fmt.Sprintf("Schema '%s.%s' does not exist.", catalogName, schemaName)}, + } + } + result := catalog.ListVolumesResponseContent{Volumes: []catalog.VolumeInfo{}} + for _, key := range slices.Sorted(maps.Keys(s.Volumes)) { + if volume := s.Volumes[key]; volume.CatalogName == catalogName && volume.SchemaName == schemaName { + result.Volumes = append(result.Volumes, volume) + } + } + return Response{Body: result} +} From 46b40dc0a00577cf013eb36b97bdf17af5a26dfe Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Wed, 25 Nov 2026 16:06:46 +0000 Subject: [PATCH 02/22] direct: fail a delete whose identity cannot be checked, warn about the other object Address review: a failed search or identity read fails the plan instead of dropping the state entry or deleting at the recorded name; ID-only state entries skip the check; one helper compares internal ids for findMoved and findByInternalID; a warning names the object left alone and how to take it over; the replay helper maps lowercased UC names. Co-authored-by: Isaac --- .../bundles/schema-volume-identity.md | 2 +- acceptance/bin/replay_tfstate.py | 5 + .../destroy_search_fails/databricks.yml.tmpl | 13 ++ .../destroy_search_fails/out.test.toml | 2 + .../schemas/destroy_search_fails/output.txt | 44 +++++ .../schemas/destroy_search_fails/script | 15 ++ .../schemas/destroy_search_fails/test.toml | 12 ++ .../foreign_at_recorded_name/output.txt | 16 ++ .../schemas/foreign_at_recorded_name/script | 4 + .../foreign_at_recorded_name/output.txt | 4 +- .../volumes/foreign_at_recorded_name/script | 3 +- bundle/deployplan/plan.go | 7 +- bundle/direct/bundle_plan.go | 167 +++++++++++------- bundle/direct/bundle_plan_test.go | 76 ++++++++ bundle/direct/dresources/README.md | 2 +- bundle/direct/dresources/adapter.go | 5 +- bundle/direct/dresources/volume.go | 3 +- libs/testserver/volumes.go | 3 +- 18 files changed, 310 insertions(+), 73 deletions(-) create mode 100644 acceptance/bundle/resources/schemas/destroy_search_fails/databricks.yml.tmpl create mode 100644 acceptance/bundle/resources/schemas/destroy_search_fails/out.test.toml create mode 100644 acceptance/bundle/resources/schemas/destroy_search_fails/output.txt create mode 100644 acceptance/bundle/resources/schemas/destroy_search_fails/script create mode 100644 acceptance/bundle/resources/schemas/destroy_search_fails/test.toml diff --git a/.nextchanges/bundles/schema-volume-identity.md b/.nextchanges/bundles/schema-volume-identity.md index f1c43cb178f..1732ce00a75 100644 --- a/.nextchanges/bundles/schema-volume-identity.md +++ b/.nextchanges/bundles/schema-volume-identity.md @@ -1 +1 @@ -* Fixed `bundle plan`, `bundle deploy` and `bundle destroy` taking a schema or volume that someone else created under the name of the bundle's own (renamed out of band) for the bundle's: destroy deleted it. The plan now checks the recorded `schema_id`/`volume_id`, and destroy finds and deletes a schema or volume the bundle deployed that was renamed out of band, in the direct deployment engine. +* Fixed `bundle destroy` deleting a schema or volume that someone else created under the name of the bundle's own after it was renamed out of band; plan and deploy no longer take it for the bundle's, and destroy deletes the bundle's renamed schema or volume instead of leaving it behind, in the direct deployment engine. diff --git a/acceptance/bin/replay_tfstate.py b/acceptance/bin/replay_tfstate.py index 598f09215fb..6d6fa691498 100755 --- a/acceptance/bin/replay_tfstate.py +++ b/acceptance/bin/replay_tfstate.py @@ -126,6 +126,9 @@ def main(): for nf in NAME_FIELDS: if attrs.get(nf): name_to_attrs[str(attrs[nf])] = attrs + # UC lowercases names (a schema created as MySchema is stored as myschema), so the + # tfstate can hold the lowercased name of the create request's. + name_to_attrs.setdefault(str(attrs[nf]).lower(), attrs) id_map = {} for req in requests: @@ -150,6 +153,8 @@ def main(): create_name = None if isinstance(body, dict): create_name = next((str(body[nf]) for nf in NAME_FIELDS if body.get(nf)), None) + if create_name is not None and create_name not in name_to_attrs: + create_name = create_name.lower() if req["method"] == "POST" and create_name in name_to_attrs: attrs = name_to_attrs[create_name] if str(attrs["id"]) != create_name: diff --git a/acceptance/bundle/resources/schemas/destroy_search_fails/databricks.yml.tmpl b/acceptance/bundle/resources/schemas/destroy_search_fails/databricks.yml.tmpl new file mode 100644 index 00000000000..2234fdf2e39 --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_search_fails/databricks.yml.tmpl @@ -0,0 +1,13 @@ +bundle: + name: test-bundle-$UNIQUE_NAME + +resources: + schemas: + schema1: + catalog_name: main + name: test-schema-$UNIQUE_NAME + volumes: + volume1: + catalog_name: main + schema_name: ${resources.schemas.schema1.name} + name: myvolume diff --git a/acceptance/bundle/resources/schemas/destroy_search_fails/out.test.toml b/acceptance/bundle/resources/schemas/destroy_search_fails/out.test.toml new file mode 100644 index 00000000000..a927a5fbc06 --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_search_fails/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/schemas/destroy_search_fails/output.txt b/acceptance/bundle/resources/schemas/destroy_search_fails/output.txt new file mode 100644 index 00000000000..e280669aacd --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_search_fails/output.txt @@ -0,0 +1,44 @@ + +=== Deploy a schema with a volume in it, and rename the schema out of band + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Created schemas.schema1 +Created volumes.volume1 +Files: 2 uploaded, 0 deleted +Resources: 2 created, 0 changed, 0 deleted, 0 unchanged + +>>> [CLI] schemas update main.test-schema-[UNIQUE_NAME] --new-name test-schema-renamed-[UNIQUE_NAME] +{ + "full_name": "main.test-schema-renamed-[UNIQUE_NAME]" +} + +=== The search for the renamed schema fails, so destroy fails without deleting anything or dropping the state + +>>> musterr [CLI] bundle destroy --auto-approve +Error: cannot plan resources.schemas.schema1: looking for a renamed resource by its internal id "[UUID]": injected (500 INTERNAL_ERROR) + +Endpoint: GET [DATABRICKS_URL]/api/2.1/unity-catalog/schemas?catalog_name=main&max_results=0 +HTTP Status: 500 Internal Server Error +API error_code: INTERNAL_ERROR +API message: injected + +Error: planning failed + + +>>> print_requests.py //unity-catalog + +>>> [CLI] bundle summary --output json +{ + "schemas": { + "schema1": "main.test-schema-[UNIQUE_NAME]" + }, + "volumes": { + "volume1": "main.test-schema-[UNIQUE_NAME].myvolume" + } +} + +>>> [CLI] schemas get main.test-schema-renamed-[UNIQUE_NAME] +{ + "full_name": "main.test-schema-renamed-[UNIQUE_NAME]" +} diff --git a/acceptance/bundle/resources/schemas/destroy_search_fails/script b/acceptance/bundle/resources/schemas/destroy_search_fails/script new file mode 100644 index 00000000000..96bd00736d7 --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_search_fails/script @@ -0,0 +1,15 @@ +envsubst < databricks.yml.tmpl > databricks.yml + +OLD_SCHEMA="main.test-schema-${UNIQUE_NAME}" +NEW_SCHEMA="main.test-schema-renamed-${UNIQUE_NAME}" + +title "Deploy a schema with a volume in it, and rename the schema out of band" +trace $CLI bundle deploy +trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | jq '{full_name}' +print_requests.py //unity-catalog &> LOG.deploy_requests + +title "The search for the renamed schema fails, so destroy fails without deleting anything or dropping the state" +trace musterr $CLI bundle destroy --auto-approve +trace print_requests.py //unity-catalog +trace $CLI bundle summary --output json | jq '.resources | map_values(map_values(.id))' +trace $CLI schemas get "$NEW_SCHEMA" | jq '{full_name}' diff --git a/acceptance/bundle/resources/schemas/destroy_search_fails/test.toml b/acceptance/bundle/resources/schemas/destroy_search_fails/test.toml new file mode 100644 index 00000000000..673a3cef990 --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_search_fails/test.toml @@ -0,0 +1,12 @@ +# Local only: the failure of the search for the renamed schema is injected. + +Ignore = [ + ".databricks", + ".gitignore", + "databricks.yml", +] + +[[Server]] +Pattern = "GET /api/2.1/unity-catalog/schemas" +Response.StatusCode = 500 +Response.Body = '{"error_code": "INTERNAL_ERROR", "message": "injected"}' diff --git a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt index 049088afc47..f79fb1e0148 100644 --- a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt +++ b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt @@ -18,6 +18,7 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged === The other schema has another schema_id, so the plan does not take it for the bundle's: it plans a create >>> [CLI] bundle plan --output json +Warn: planning resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so it is not treated as this bundle's. To manage it with this bundle, run 'bundle deployment unbind' and 'bundle deployment bind'. { "resources.schemas.schema1": { "action": "create", @@ -38,9 +39,24 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged } } +=== Deploy leaves the other schema alone: the create fails on its name + +>>> musterr [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Warn: planning resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so it is not treated as this bundle's. To manage it with this bundle, run 'bundle deployment unbind' and 'bundle deployment bind'. +Error: cannot create resources.schemas.schema1: Schema 'test-schema-[UNIQUE_NAME]' already exists (400 SCHEMA_ALREADY_EXISTS) + +Endpoint: POST [DATABRICKS_URL]/api/2.1/unity-catalog/schemas +HTTP Status: 400 Bad Request +API error_code: SCHEMA_ALREADY_EXISTS +API message: Schema 'test-schema-[UNIQUE_NAME]' already exists + +Files: 2 uploaded, 0 deleted + === Destroy finds the bundle's schema by its schema_id and deletes it, not the other one >>> [CLI] bundle destroy --auto-approve +Warn: planning resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so it is not treated as this bundle's. To manage it with this bundle, run 'bundle deployment unbind' and 'bundle deployment bind'. The following resources will be deleted: delete resources.schemas.schema1 diff --git a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script index 640227098b7..91ee1bfbb67 100644 --- a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script +++ b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script @@ -31,6 +31,10 @@ print_requests.py //unity-catalog &> LOG.oob_requests title "The other schema has another schema_id, so the plan does not take it for the bundle's: it plans a create" plan +title "Deploy leaves the other schema alone: the create fails on its name" +trace musterr $CLI bundle deploy +print_requests.py //unity-catalog &> LOG.failed_deploy_requests + title "Destroy finds the bundle's schema by its schema_id and deletes it, not the other one" trace $CLI bundle destroy --auto-approve trace print_requests.py //unity-catalog diff --git a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt index e232a75a953..34f410819d4 100644 --- a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt +++ b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt @@ -4,7 +4,7 @@ >>> [CLI] bundle deploy Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... Created volumes.volume1 -Files: 2 uploaded, 0 deleted +Files: 3 uploaded, 0 deleted Resources: 1 created, 0 changed, 0 deleted, 0 unchanged === Out of band, rename the volume away, and create another one under its name @@ -18,6 +18,7 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged === The other volume has another volume_id, so the plan does not take it for the bundle's: it plans a create >>> [CLI] bundle plan +Warn: planning resources.volumes.volume1: id="main.test-schema-[UNIQUE_NAME].myvolume" holds another object: its volume_id is "[FOREIGN_VOLUME_ID]", not "[VOLUME_ID]" as recorded, so it is not treated as this bundle's. To manage it with this bundle, run 'bundle deployment unbind' and 'bundle deployment bind'. create volumes.volume1 Plan: 1 to add, 0 to change, 0 to delete, 0 unchanged @@ -25,6 +26,7 @@ Plan: 1 to add, 0 to change, 0 to delete, 0 unchanged === Destroy finds the bundle's volume by its volume_id and deletes it, not the other one >>> [CLI] bundle destroy --auto-approve +Warn: planning resources.volumes.volume1: id="main.test-schema-[UNIQUE_NAME].myvolume" holds another object: its volume_id is "[FOREIGN_VOLUME_ID]", not "[VOLUME_ID]" as recorded, so it is not treated as this bundle's. To manage it with this bundle, run 'bundle deployment unbind' and 'bundle deployment bind'. The following resources will be deleted: delete resources.volumes.volume1 diff --git a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script index c20ab60fcef..27ff93af5bb 100644 --- a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script +++ b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script @@ -2,7 +2,6 @@ envsubst < databricks.yml.tmpl > databricks.yml # The schema is not in the bundle, so destroy deletes the volume itself rather than its schema. SCHEMA="main.test-schema-${UNIQUE_NAME}" -$CLI schemas create "test-schema-${UNIQUE_NAME}" main > /dev/null cleanup() { errcode $CLI schemas delete "$SCHEMA" --force > LOG.cleanup 2>&1 @@ -12,6 +11,8 @@ cleanup() { } trap cleanup EXIT +$CLI schemas create "test-schema-${UNIQUE_NAME}" main > LOG.create + title "Deploy a volume" trace $CLI bundle deploy print_requests.py //unity-catalog &> LOG.deploy_requests diff --git a/bundle/deployplan/plan.go b/bundle/deployplan/plan.go index 6fcaa333e08..f63522b0a2e 100644 --- a/bundle/deployplan/plan.go +++ b/bundle/deployplan/plan.go @@ -125,13 +125,14 @@ func LoadPlanFromFile(path string) (*Plan, error) { type PlanEntry struct { // ID is set when the resource is at another ID than the one in state: a rename moved it there - // (see findMoved), or a parent renamed in this plan moves it before it is recreated (see - // addParentMoves). Applying the entry addresses the resource at ID. + // (see findMoved, and findByInternalID for a Delete), or a parent renamed in this plan moves it + // before it is recreated (see addParentMoves). Applying the entry addresses the resource at ID. ID string `json:"id,omitempty"` DependsOn []DependsOnEntry `json:"depends_on,omitempty"` Action ActionType `json:"action,omitempty"` // Gone is set on Delete entries when the resource no longer exists remotely: planning - // confirmed it with a read, or the state entry has an empty ID (left by a recreate in + // confirmed it with a read (for a resource with an internal ID: found neither at its ID, where + // another object may be, nor by its internal ID), or the state entry has an empty ID (left by a recreate in // an older CLI whose create step failed after the delete). Applying such an entry only // removes it from the state, without calling the delete API, and approval prompts do // not list it as a deletion. diff --git a/bundle/direct/bundle_plan.go b/bundle/direct/bundle_plan.go index 7c536e26768..a459b84a275 100644 --- a/bundle/direct/bundle_plan.go +++ b/bundle/direct/bundle_plan.go @@ -249,6 +249,7 @@ func (b *DeploymentBundle) CalculatePlan(ctx context.Context, client *databricks if err != nil { if apierr.IsMissing(err) { missing = true + remoteState = nil } else { log.Warnf(ctx, "reading %s id=%q: %s", resourceKey, id, err) // This is not an error during deletion, so don't return false here @@ -260,39 +261,24 @@ func (b *DeploymentBundle) CalculatePlan(ctx context.Context, client *databricks entry.Gone = true } - var savedState any - if adapter.HasInternalID() && (missing || (err == nil && !entry.Gone)) { - dbentry, _ := b.StateDB.GetResourceEntry(resourceKey) - savedState, err = parseState(adapter.StateType(), dbentry.State) - if err != nil { - logdiag.LogError(ctx, fmt.Errorf("%s: interpreting state: %w", errorPrefix, err)) - return false + if adapter.HasInternalID() && !entry.Gone { + var readErr error + if !missing { + readErr = err } - } - - if !missing && !entry.Gone && savedState != nil { - other, err := isOtherResource(adapter, savedState, remoteState) + entry.ID, remoteState, err = b.findDeleteTarget(ctx, resourceKey, adapter, id, remoteState, readErr) if err != nil { logdiag.LogError(ctx, fmt.Errorf("%s: %w", errorPrefix, err)) return false } - // Never delete what someone else created under the name ours had. - missing = other + // After a failed read (a permission error), the delete goes to the recorded ID, as before. + missing = remoteState == nil && readErr == nil } - if missing { - remoteState = nil - if savedState != nil { - entry.ID, remoteState, err = findByInternalID(ctx, adapter, savedState) - if err != nil { - logdiag.LogError(ctx, fmt.Errorf("%s: %w", errorPrefix, err)) - return false - } - } - // Not found anywhere: the resource is already deleted remotely. Keep the Delete - // entry so that applying it removes the stale state entry, but mark it Gone so + // The resource is already deleted remotely. Keep the Delete entry so + // that applying it removes the stale state entry, but mark it Gone so // apply skips the delete call and prompts don't list it as a deletion. - entry.Gone = remoteState == nil + entry.Gone = true } entry.RemoteState = remoteState @@ -384,12 +370,13 @@ func (b *DeploymentBundle) CalculatePlan(ctx context.Context, client *databricks } // Someone else's resource under the name ours had is not ours: plan as if ours were not // there, so the plan neither updates nor later deletes it. - other, err := isOtherResource(adapter, savedState, remoteState) + otherID, err := otherInternalID(adapter, savedState, remoteState) if err != nil { logdiag.LogError(ctx, fmt.Errorf("%s: %w", errorPrefix, err)) return false } - if other { + if otherID != "" { + warnOtherResource(ctx, adapter, dbentry.ID, savedState, otherID) remoteState = nil } if remoteState == nil { @@ -988,40 +975,97 @@ func findMoved(ctx context.Context, adapter *dresources.Adapter, sv *structvar.S log.Warnf(ctx, "looking for a moved resource at id=%q: %s", id, err) return "", nil, nil } - found, err := adapter.RemapState(remote) - if err != nil { - return "", nil, err - } - foundInternalID, err := adapter.InternalID(found) - if err != nil || foundInternalID != savedInternalID { + if ok, err := hasInternalIDOf(adapter, remote, savedInternalID); err != nil || !ok { return "", nil, err } return id, remote, nil } -// isOtherResource reports whether remote, read at the recorded ID, is another object than the one -// savedState records: both have an internal ID and they differ. The resource was renamed away and -// another one created under its old name. -func isOtherResource(adapter *dresources.Adapter, savedState, remote any) (bool, error) { +// hasInternalIDOf reports whether remote has the internal ID internalID: it is the resource the +// state records, wherever it is now. +func hasInternalIDOf(adapter *dresources.Adapter, remote any, internalID string) (bool, error) { + remapped, err := adapter.RemapState(remote) + if err != nil { + return false, err + } + found, err := adapter.InternalID(remapped) + return found == internalID, err +} + +// otherInternalID returns the internal ID of remote, read at the recorded ID, if it is another +// object than the one savedState records: ours was renamed away and another one created under its +// old name. It is "" when the resource is ours, or when either side has no internal ID to compare. +func otherInternalID(adapter *dresources.Adapter, savedState, remote any) (string, error) { if !adapter.HasInternalID() || remote == nil { - return false, nil + return "", nil } saved, err := adapter.InternalID(savedState) if err != nil || saved == "" { - return false, err + return "", err } remapped, err := adapter.RemapState(remote) if err != nil { - return false, err + return "", err } found, err := adapter.InternalID(remapped) - return found != "" && found != saved, err + if err != nil || found == saved { + return "", err + } + return found, nil +} + +// warnOtherResource tells the user that what is at the recorded ID is not the bundle's and is left +// alone. A resource deleted and created again out of band looks the same, hence the way to take it over. +func warnOtherResource(ctx context.Context, adapter *dresources.Adapter, id string, savedState any, otherID string) { + saved, _ := adapter.InternalID(savedState) + log.Warnf(ctx, "id=%q holds another object: its %s is %q, not %q as recorded, so it is not treated as this bundle's. To manage it with this bundle, run 'bundle deployment unbind' and 'bundle deployment bind'.", + id, adapter.ResourceConfig().InternalIDField, otherID, saved) +} + +// findDeleteTarget returns where to delete a resource with an internal ID, given what a read at its +// recorded ID returned: there if it is ours, else where findByInternalID finds it. An empty ID means +// the recorded one; a nil remote means it is nowhere. Someone else's resource under the name ours had +// is never the target, and neither is a resource whose identity could not be checked: a read that +// failed for any other reason than a missing resource or a permission error fails the plan. +func (b *DeploymentBundle) findDeleteTarget(ctx context.Context, resourceKey string, adapter *dresources.Adapter, id string, remote any, readErr error) (string, any, error) { + dbentry, _ := b.StateDB.GetResourceEntry(resourceKey) + if len(dbentry.State) == 0 { + // Only the ID is recorded: there is no internal ID to check. + return "", remote, nil + } + savedState, err := parseState(adapter.StateType(), dbentry.State) + if err != nil { + return "", nil, fmt.Errorf("interpreting state: %w", err) + } + if saved, err := adapter.InternalID(savedState); err != nil || saved == "" { + return "", remote, err + } + if readErr != nil { + if errors.Is(readErr, apierr.ErrPermissionDenied) { + // Deleted as before: a resource the user cannot read is left to the delete call. + return "", nil, nil + } + return "", nil, fmt.Errorf("cannot check that id=%q is still this bundle's: %w", id, readErr) + } + otherID, err := otherInternalID(adapter, savedState, remote) + if err != nil { + return "", nil, err + } + if remote != nil && otherID == "" { + return "", remote, nil + } + if otherID != "" { + warnOtherResource(ctx, adapter, id, savedState, otherID) + } + return findByInternalID(ctx, adapter, savedState) } // findByInternalID looks for a resource to be deleted that is not at its recorded ID: renamed out of -// band. Destroy has no config to give its new name, as findMoved uses, so the resource searches for -// it (DoFindByInternalID). As in findMoved, what is found is taken only if its internal ID matches. -// A failed search finds nothing: the delete then only drops the state entry, as it did before. +// band. A plan without config (destroy), or without this resource in it, has no new name to look at, +// as findMoved does, so the resource searches for it (DoFindByInternalID). As in findMoved, what is +// found is taken only if its internal ID matches. A search the user has no permission for finds +// nothing, and the delete only drops the state entry, as it did before; any other failure fails the +// plan, so the state keeps the record of the resource and the command can be run again. func findByInternalID(ctx context.Context, adapter *dresources.Adapter, savedState any) (string, any, error) { if !adapter.HasFindByInternalID() { return "", nil, nil @@ -1033,35 +1077,34 @@ func findByInternalID(ctx context.Context, adapter *dresources.Adapter, savedSta id, err := retry.With(ctx, retry.IsTransientRead, func() (string, error) { return adapter.DoFindByInternalID(ctx, savedState) }) - if err != nil { - if !apierr.IsMissing(err) { - log.Warnf(ctx, "looking for a renamed resource by its internal id %q: %s", savedInternalID, err) - } - return "", nil, nil - } - if id == "" { - return "", nil, nil + if err != nil || id == "" { + return "", nil, ignoreMissingOrForbidden(ctx, err, fmt.Sprintf("looking for a renamed resource by its internal id %q", savedInternalID)) } remote, err := retry.With(ctx, retry.IsTransientRead, func() (any, error) { return adapter.DoRead(ctx, id) }) if err != nil { - if !apierr.IsMissing(err) { - log.Warnf(ctx, "reading a renamed resource at id=%q: %s", id, err) - } - return "", nil, nil - } - found, err := adapter.RemapState(remote) - if err != nil { - return "", nil, err + return "", nil, ignoreMissingOrForbidden(ctx, err, fmt.Sprintf("reading a renamed resource at id=%q", id)) } - foundInternalID, err := adapter.InternalID(found) - if err != nil || foundInternalID != savedInternalID { + if ok, err := hasInternalIDOf(adapter, remote, savedInternalID); err != nil || !ok { return "", nil, err } return id, remote, nil } +// ignoreMissingOrForbidden returns err unless it is nil, a missing resource or a permission error +// (logged as a warning). +func ignoreMissingOrForbidden(ctx context.Context, err error, what string) error { + if err == nil || apierr.IsMissing(err) { + return nil + } + if errors.Is(err, apierr.ErrPermissionDenied) { + log.Warnf(ctx, "%s: %s", what, err) + return nil + } + return fmt.Errorf("%s: %w", what, err) +} + // missingInternalID reports whether savedState lacks the internal ID that remote, the remote state // remapped to the state type, has. func missingInternalID(adapter *dresources.Adapter, savedState, remote any) (bool, error) { diff --git a/bundle/direct/bundle_plan_test.go b/bundle/direct/bundle_plan_test.go index 9c5d208d159..5b4c541dabe 100644 --- a/bundle/direct/bundle_plan_test.go +++ b/bundle/direct/bundle_plan_test.go @@ -14,6 +14,8 @@ import ( "github.com/databricks/cli/libs/structs/structdiff" "github.com/databricks/cli/libs/structs/structpath" "github.com/databricks/cli/libs/structs/structvar" + "github.com/databricks/databricks-sdk-go/apierr" + "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/databricks/databricks-sdk-go/service/compute" "github.com/databricks/databricks-sdk-go/service/jobs" "github.com/databricks/databricks-sdk-go/service/pipelines" @@ -991,3 +993,77 @@ func TestAddParentMoves(t *testing.T) { }) } } + +func TestOtherInternalID(t *testing.T) { + adapters, err := dresources.InitAll(nil) + require.NoError(t, err) + adapter := adapters["schemas"] + + for name, tt := range map[string]struct { + saved, remote, want string + }{ + "same": {saved: "uuid-1", remote: "uuid-1", want: ""}, + "different": {saved: "uuid-1", remote: "uuid-2", want: "uuid-2"}, + "not recorded": {saved: "", remote: "uuid-2", want: ""}, + "not in remote": {saved: "uuid-1", remote: "", want: ""}, + "neither has internal": {saved: "", remote: "", want: ""}, + } { + t.Run(name, func(t *testing.T) { + saved := &dresources.SchemaState{SchemaId: tt.saved} + remote := &catalog.SchemaInfo{SchemaId: tt.remote} + got, err := otherInternalID(adapter, saved, remote) + require.NoError(t, err) + assert.Equal(t, tt.want, got) + }) + } + + got, err := otherInternalID(adapter, &dresources.SchemaState{SchemaId: "uuid-1"}, nil) + require.NoError(t, err) + assert.Empty(t, got) +} + +func TestFindDeleteTargetKeepsOwnResource(t *testing.T) { + const key = "resources.schemas.schema1" + adapters, err := dresources.InitAll(nil) + require.NoError(t, err) + remote := &catalog.SchemaInfo{FullName: "main.s1", SchemaId: "uuid-1"} + + for name, state := range map[string][]byte{ + // The deployment metadata service can hold an entry with only an ID. + "id-only entry": nil, + "same internal id": []byte(`{"catalog_name": "main", "name": "s1", "schema_id": "uuid-1"}`), + "internal id unset": []byte(`{"catalog_name": "main", "name": "s1"}`), + } { + t.Run(name, func(t *testing.T) { + b := &DeploymentBundle{Adapters: adapters} + b.StateDB.OpenWithData("state.json", dstate.Database{State: map[string]dstate.ResourceEntry{ + key: {ID: "main.s1", State: state}, + }}) + id, got, err := b.findDeleteTarget(t.Context(), key, adapters["schemas"], "main.s1", remote, nil) + require.NoError(t, err) + assert.Empty(t, id) + assert.Same(t, remote, got) + }) + } +} + +func TestFindDeleteTargetUnverifiedRead(t *testing.T) { + const key = "resources.schemas.schema1" + adapters, err := dresources.InitAll(nil) + require.NoError(t, err) + b := &DeploymentBundle{Adapters: adapters} + b.StateDB.OpenWithData("state.json", dstate.Database{State: map[string]dstate.ResourceEntry{ + key: {ID: "main.s1", State: []byte(`{"catalog_name": "main", "name": "s1", "schema_id": "uuid-1"}`)}, + }}) + + // A read that failed for another reason than a missing resource cannot show that what is at the + // recorded ID is ours: the plan fails instead of deleting it. + _, _, err = b.findDeleteTarget(t.Context(), key, adapters["schemas"], "main.s1", nil, apierr.ErrInternalError) + assert.ErrorIs(t, err, apierr.ErrInternalError) + + // A permission error leaves the delete to the recorded ID, as before. + id, got, err := b.findDeleteTarget(t.Context(), key, adapters["schemas"], "main.s1", nil, apierr.ErrPermissionDenied) + require.NoError(t, err) + assert.Empty(t, id) + assert.Nil(t, got) +} diff --git a/bundle/direct/dresources/README.md b/bundle/direct/dresources/README.md index a77df917a92..8e92e136c6e 100644 --- a/bundle/direct/dresources/README.md +++ b/bundle/direct/dresources/README.md @@ -62,7 +62,7 @@ If the ID is built from the resource's own fields (a UC full name), implement `I If the backend also has its own ID that a rename keeps (a volume's `volume_id`), add it to the state type, copy it in `RemapState`, and name it in the config as `internal_id_field`. The framework records it on every save and skips its changes in the plan. When nothing is at the recorded ID, the plan reads at the ID the config gives the resource and takes what it finds only if the internal ID matches. This covers a rename done out of band, or by a deploy that stopped before re-keying the resource. -An object at the recorded ID with a different internal ID is someone else's (ours was renamed away and another created under its name): the plan treats ours as not there, so it never updates or deletes the other object. Destroy has no config to give the new name, so to delete a resource renamed out of band it needs `DoFindByInternalID`: it searches the parent (lists the schemas in the catalog, the volumes in the schema) for the recorded internal ID. Without it, destroy only drops the state entry of a resource not found at its recorded ID. +An object at the recorded ID with a different internal ID is someone else's (ours was renamed away and another created under its name): the plan treats ours as not there, so it never updates or deletes the other object. A resource to be deleted (by destroy, or removed from config) has no config to give its new name, so to delete one renamed out of band the resource needs `DoFindByInternalID`: it searches the parent (lists the schemas in the catalog, the volumes in the schema) for the recorded internal ID. Without it, or when the parent itself was renamed, the delete only drops the state entry of a resource not found at its recorded ID. ## Empty states: IsEmptyState diff --git a/bundle/direct/dresources/adapter.go b/bundle/direct/dresources/adapter.go index 75a2729d251..46e4838b45a 100644 --- a/bundle/direct/dresources/adapter.go +++ b/bundle/direct/dresources/adapter.go @@ -128,8 +128,9 @@ type IResource interface { // [Optional] DoFindByInternalID returns the ID of the resource whose internal ID (see // internal_id_field) is the one in state, or "" if none is found. It looks where the resource - // can be without its name: destroy has no config to give the new name of a resource renamed - // out of band. The engine reads what it returns and checks the internal ID again. + // can be without its name: a resource to be deleted (by destroy, or removed from config) has + // no config to give its new name after a rename out of band. The engine reads what it returns + // and checks the internal ID again. // Example: func (r *ResourceSchema) DoFindByInternalID(ctx context.Context, state *SchemaState) (string, error) DoFindByInternalID(ctx context.Context, state any) (string, error) } diff --git a/bundle/direct/dresources/volume.go b/bundle/direct/dresources/volume.go index 15252e13976..08dd405e367 100644 --- a/bundle/direct/dresources/volume.go +++ b/bundle/direct/dresources/volume.go @@ -166,7 +166,8 @@ func (*ResourceVolume) IDFromState(state *VolumeState) string { } // DoFindByInternalID lists the volumes in the schema for the one with the recorded volume_id. A -// volume whose schema was renamed is not found: deleting the schema deletes it. +// volume whose schema was renamed is not found; when the schema is in the bundle too, deleting it +// deletes the volume. func (r *ResourceVolume) DoFindByInternalID(ctx context.Context, state *VolumeState) (string, error) { iter := r.client.Volumes.List(ctx, catalog.ListVolumesRequest{CatalogName: state.CatalogName, SchemaName: state.SchemaName}) for iter.HasNext(ctx) { diff --git a/libs/testserver/volumes.go b/libs/testserver/volumes.go index c8ce77cb753..7e860fe4d49 100644 --- a/libs/testserver/volumes.go +++ b/libs/testserver/volumes.go @@ -122,7 +122,8 @@ func (s *FakeWorkspace) VolumesList(req Request) Response { defer s.LockUnlock()() catalogName := req.URL.Query().Get("catalog_name") - schemaName := req.URL.Query().Get("schema_name") + // Stored lowercased, see VolumesCreate. + schemaName := strings.ToLower(req.URL.Query().Get("schema_name")) if _, ok := s.Schemas[catalogName+"."+schemaName]; !ok { return Response{ StatusCode: http.StatusNotFound, From 310d6e43fcf5c952782403c667208b19a6f7026c Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Wed, 25 Nov 2026 16:22:41 +0000 Subject: [PATCH 03/22] direct: delete a child under its parent's found name; warn only when ours is not found A volume in a schema renamed out of band was dropped from state and went with the schema's force delete, past its prevent_destroy. Look it up under the parent's new name and delete it as itself. Warn about the other object only when the bundle's own is not found elsewhere, and document that a resource deleted and created again out of band needs rebinding. Co-authored-by: Isaac --- .../bundles/schema-volume-identity.md | 2 +- .../output.txt | 13 +- .../foreign_at_recorded_name/output.txt | 5 +- .../databricks.yml.tmpl | 9 ++ .../recreated_at_recorded_name/out.test.toml | 2 + .../recreated_at_recorded_name/output.txt | 60 ++++++++ .../schemas/recreated_at_recorded_name/script | 33 +++++ .../recreated_at_recorded_name/test.toml | 7 + .../foreign_at_recorded_name/output.txt | 3 +- .../databricks.yml.tmpl | 15 ++ .../out.test.toml | 2 + .../output.txt | 29 ++++ .../prevent_destroy_in_renamed_schema/script | 26 ++++ .../test.toml | 7 + bundle/direct/bundle_plan.go | 140 +++++++++++++++++- 15 files changed, 341 insertions(+), 12 deletions(-) create mode 100644 acceptance/bundle/resources/schemas/recreated_at_recorded_name/databricks.yml.tmpl create mode 100644 acceptance/bundle/resources/schemas/recreated_at_recorded_name/out.test.toml create mode 100644 acceptance/bundle/resources/schemas/recreated_at_recorded_name/output.txt create mode 100644 acceptance/bundle/resources/schemas/recreated_at_recorded_name/script create mode 100644 acceptance/bundle/resources/schemas/recreated_at_recorded_name/test.toml create mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/databricks.yml.tmpl create mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/out.test.toml create mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/output.txt create mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/script create mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/test.toml diff --git a/.nextchanges/bundles/schema-volume-identity.md b/.nextchanges/bundles/schema-volume-identity.md index 1732ce00a75..f1fa28dd413 100644 --- a/.nextchanges/bundles/schema-volume-identity.md +++ b/.nextchanges/bundles/schema-volume-identity.md @@ -1 +1 @@ -* Fixed `bundle destroy` deleting a schema or volume that someone else created under the name of the bundle's own after it was renamed out of band; plan and deploy no longer take it for the bundle's, and destroy deletes the bundle's renamed schema or volume instead of leaving it behind, in the direct deployment engine. +* Fixed `bundle destroy` deleting a schema or volume that someone else created under the name of the bundle's own after it was renamed out of band; plan and deploy no longer take it for the bundle's, and destroy deletes the bundle's renamed schema or volume instead of leaving it behind, in the direct deployment engine. A schema or volume deleted and created again outside the bundle is likewise no longer the bundle's: deploy fails on its name and destroy leaves it, until it is bound again with `bundle deployment unbind` and `bundle deployment bind`. diff --git a/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/output.txt b/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/output.txt index 960536d12d9..df352651a1e 100644 --- a/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/output.txt +++ b/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/output.txt @@ -23,15 +23,26 @@ Resources: 2 created, 0 changed, 0 deleted, 0 unchanged >>> [CLI] bundle destroy --auto-approve The following resources will be deleted: delete resources.schemas.schema1 + delete resources.volumes.volume1 This action will result in the deletion of the following UC schemas. Any underlying data may be lost: delete resources.schemas.schema1 +This action will result in the deletion of the following volumes. +For managed volumes, the files stored in the volume are also deleted from your +cloud tenant within 30 days. For external volumes, the metadata about the volume +is removed from the catalog, but the underlying files are not deleted: + delete resources.volumes.volume1 + All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default -Destroy: 1 deleted +Destroy: 2 deleted >>> print_requests.py //unity-catalog +{ + "method": "DELETE", + "path": "/api/2.1/unity-catalog/volumes/main.test-schema-renamed-[UNIQUE_NAME].myvolume" +} { "method": "DELETE", "path": "/api/2.1/unity-catalog/schemas/main.test-schema-renamed-[UNIQUE_NAME]", diff --git a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt index f79fb1e0148..606a51a0bcb 100644 --- a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt +++ b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt @@ -18,7 +18,7 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged === The other schema has another schema_id, so the plan does not take it for the bundle's: it plans a create >>> [CLI] bundle plan --output json -Warn: planning resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so it is not treated as this bundle's. To manage it with this bundle, run 'bundle deployment unbind' and 'bundle deployment bind'. +Warn: planning resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it alone. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again. { "resources.schemas.schema1": { "action": "create", @@ -43,7 +43,7 @@ Warn: planning resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" ho >>> musterr [CLI] bundle deploy Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... -Warn: planning resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so it is not treated as this bundle's. To manage it with this bundle, run 'bundle deployment unbind' and 'bundle deployment bind'. +Warn: planning resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it alone. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again. Error: cannot create resources.schemas.schema1: Schema 'test-schema-[UNIQUE_NAME]' already exists (400 SCHEMA_ALREADY_EXISTS) Endpoint: POST [DATABRICKS_URL]/api/2.1/unity-catalog/schemas @@ -56,7 +56,6 @@ Files: 2 uploaded, 0 deleted === Destroy finds the bundle's schema by its schema_id and deletes it, not the other one >>> [CLI] bundle destroy --auto-approve -Warn: planning resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so it is not treated as this bundle's. To manage it with this bundle, run 'bundle deployment unbind' and 'bundle deployment bind'. The following resources will be deleted: delete resources.schemas.schema1 diff --git a/acceptance/bundle/resources/schemas/recreated_at_recorded_name/databricks.yml.tmpl b/acceptance/bundle/resources/schemas/recreated_at_recorded_name/databricks.yml.tmpl new file mode 100644 index 00000000000..d73aacf0d14 --- /dev/null +++ b/acceptance/bundle/resources/schemas/recreated_at_recorded_name/databricks.yml.tmpl @@ -0,0 +1,9 @@ +bundle: + name: test-bundle-$UNIQUE_NAME + +resources: + schemas: + schema1: + catalog_name: main + name: test-schema-$UNIQUE_NAME + comment: c1 diff --git a/acceptance/bundle/resources/schemas/recreated_at_recorded_name/out.test.toml b/acceptance/bundle/resources/schemas/recreated_at_recorded_name/out.test.toml new file mode 100644 index 00000000000..0f167effef1 --- /dev/null +++ b/acceptance/bundle/resources/schemas/recreated_at_recorded_name/out.test.toml @@ -0,0 +1,2 @@ +Cloud = true +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/schemas/recreated_at_recorded_name/output.txt b/acceptance/bundle/resources/schemas/recreated_at_recorded_name/output.txt new file mode 100644 index 00000000000..4c414c61049 --- /dev/null +++ b/acceptance/bundle/resources/schemas/recreated_at_recorded_name/output.txt @@ -0,0 +1,60 @@ + +=== Deploy a schema + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Created schemas.schema1 +Files: 2 uploaded, 0 deleted +Resources: 1 created, 0 changed, 0 deleted, 0 unchanged + +=== Out of band, drop the schema and create it again under the same name + +>>> [CLI] schemas delete main.test-schema-[UNIQUE_NAME] --force + +=== The new schema has another schema_id: the bundle does not take it for its own, and deploy fails on its name + +>>> [CLI] bundle plan +Warn: planning resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[NEW_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it alone. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again. +create schemas.schema1 + +Plan: 1 to add, 0 to change, 0 to delete, 0 unchanged + +>>> musterr [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Warn: planning resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[NEW_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it alone. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again. +Error: cannot create resources.schemas.schema1: Schema 'test-schema-[UNIQUE_NAME]' already exists (400 SCHEMA_ALREADY_EXISTS) + +Endpoint: POST [DATABRICKS_URL]/api/2.1/unity-catalog/schemas +HTTP Status: 400 Bad Request +API error_code: SCHEMA_ALREADY_EXISTS +API message: Schema 'test-schema-[UNIQUE_NAME]' already exists + +Files: 2 uploaded, 0 deleted + +=== Binding it again makes it the bundle's + +>>> [CLI] bundle deployment unbind schema1 + +>>> [CLI] bundle deployment bind schema1 main.test-schema-[UNIQUE_NAME] --auto-approve +Successfully bound schema with an id 'main.test-schema-[UNIQUE_NAME]' +Run 'bundle deploy' to deploy changes to your workspace + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Updated schemas.schema1 +Files: 1 uploaded, 0 deleted +Resources: 0 created, 1 changed, 0 deleted, 0 unchanged + +>>> [CLI] bundle plan +Plan: 0 to add, 0 to change, 0 to delete, 1 unchanged + +>>> [CLI] bundle destroy --auto-approve +The following resources will be deleted: + delete resources.schemas.schema1 + +This action will result in the deletion of the following UC schemas. Any underlying data may be lost: + delete resources.schemas.schema1 + +All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default + +Destroy: 1 deleted diff --git a/acceptance/bundle/resources/schemas/recreated_at_recorded_name/script b/acceptance/bundle/resources/schemas/recreated_at_recorded_name/script new file mode 100644 index 00000000000..b3bee2efda2 --- /dev/null +++ b/acceptance/bundle/resources/schemas/recreated_at_recorded_name/script @@ -0,0 +1,33 @@ +envsubst < databricks.yml.tmpl > databricks.yml + +SCHEMA="main.test-schema-${UNIQUE_NAME}" + +cleanup() { + trace $CLI bundle destroy --auto-approve + errcode $CLI schemas delete "$SCHEMA" --force > LOG.cleanup 2>&1 + + # Consume the destroy's requests so none are left recorded. + print_requests.py //unity-catalog &> LOG.destroy_requests +} +trap cleanup EXIT + +title "Deploy a schema" +trace $CLI bundle deploy +print_requests.py //unity-catalog &> LOG.deploy_requests +add_repl "$($CLI schemas get "$SCHEMA" | jq -r .schema_id)" SCHEMA_ID + +title "Out of band, drop the schema and create it again under the same name" +trace $CLI schemas delete "$SCHEMA" --force +add_repl "$($CLI schemas create "test-schema-${UNIQUE_NAME}" main | jq -r .schema_id)" NEW_SCHEMA_ID +print_requests.py //unity-catalog &> LOG.oob_requests + +title "The new schema has another schema_id: the bundle does not take it for its own, and deploy fails on its name" +trace $CLI bundle plan +trace musterr $CLI bundle deploy +print_requests.py //unity-catalog &> LOG.failed_deploy_requests + +title "Binding it again makes it the bundle's" +trace $CLI bundle deployment unbind schema1 +trace $CLI bundle deployment bind schema1 "$SCHEMA" --auto-approve +trace $CLI bundle deploy +trace $CLI bundle plan diff --git a/acceptance/bundle/resources/schemas/recreated_at_recorded_name/test.toml b/acceptance/bundle/resources/schemas/recreated_at_recorded_name/test.toml new file mode 100644 index 00000000000..4272a80cf61 --- /dev/null +++ b/acceptance/bundle/resources/schemas/recreated_at_recorded_name/test.toml @@ -0,0 +1,7 @@ +Cloud = true + +Ignore = [ + ".databricks", + ".gitignore", + "databricks.yml", +] diff --git a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt index 34f410819d4..61f4dd8dc80 100644 --- a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt +++ b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt @@ -18,7 +18,7 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged === The other volume has another volume_id, so the plan does not take it for the bundle's: it plans a create >>> [CLI] bundle plan -Warn: planning resources.volumes.volume1: id="main.test-schema-[UNIQUE_NAME].myvolume" holds another object: its volume_id is "[FOREIGN_VOLUME_ID]", not "[VOLUME_ID]" as recorded, so it is not treated as this bundle's. To manage it with this bundle, run 'bundle deployment unbind' and 'bundle deployment bind'. +Warn: planning resources.volumes.volume1: id="main.test-schema-[UNIQUE_NAME].myvolume" holds another object: its volume_id is "[FOREIGN_VOLUME_ID]", not "[VOLUME_ID]" as recorded, so this bundle leaves it alone. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again. create volumes.volume1 Plan: 1 to add, 0 to change, 0 to delete, 0 unchanged @@ -26,7 +26,6 @@ Plan: 1 to add, 0 to change, 0 to delete, 0 unchanged === Destroy finds the bundle's volume by its volume_id and deletes it, not the other one >>> [CLI] bundle destroy --auto-approve -Warn: planning resources.volumes.volume1: id="main.test-schema-[UNIQUE_NAME].myvolume" holds another object: its volume_id is "[FOREIGN_VOLUME_ID]", not "[VOLUME_ID]" as recorded, so it is not treated as this bundle's. To manage it with this bundle, run 'bundle deployment unbind' and 'bundle deployment bind'. The following resources will be deleted: delete resources.volumes.volume1 diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/databricks.yml.tmpl b/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/databricks.yml.tmpl new file mode 100644 index 00000000000..f2a772f1291 --- /dev/null +++ b/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/databricks.yml.tmpl @@ -0,0 +1,15 @@ +bundle: + name: test-bundle-$UNIQUE_NAME + +resources: + schemas: + schema1: + catalog_name: main + name: test-schema-$UNIQUE_NAME + volumes: + volume1: + catalog_name: main + schema_name: ${resources.schemas.schema1.name} + name: myvolume + lifecycle: + prevent_destroy: true diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/out.test.toml b/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/out.test.toml new file mode 100644 index 00000000000..0f167effef1 --- /dev/null +++ b/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/out.test.toml @@ -0,0 +1,2 @@ +Cloud = true +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/output.txt b/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/output.txt new file mode 100644 index 00000000000..e3c3b0e4393 --- /dev/null +++ b/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/output.txt @@ -0,0 +1,29 @@ + +=== Deploy a schema with a volume in it that must not be destroyed + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Created schemas.schema1 +Created volumes.volume1 +Files: 2 uploaded, 0 deleted +Resources: 2 created, 0 changed, 0 deleted, 0 unchanged + +=== Rename the schema out of band. UC moves the volume with it + +>>> [CLI] schemas update main.test-schema-[UNIQUE_NAME] --new-name test-schema-renamed-[UNIQUE_NAME] +{ + "full_name": "main.test-schema-renamed-[UNIQUE_NAME]" +} + +=== Destroy finds the volume in the renamed schema, so prevent_destroy still stops it + +>>> musterr [CLI] bundle destroy --auto-approve +Error: resources.volumes.volume1 has lifecycle.prevent_destroy set, but the plan calls for this resource to be recreated or destroyed. To avoid this error, disable lifecycle.prevent_destroy for resources.volumes.volume1 + + +>>> print_requests.py //unity-catalog + +>>> [CLI] volumes read main.test-schema-renamed-[UNIQUE_NAME].myvolume +{ + "full_name": "main.test-schema-renamed-[UNIQUE_NAME].myvolume" +} diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/script b/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/script new file mode 100644 index 00000000000..ad354bf60f7 --- /dev/null +++ b/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/script @@ -0,0 +1,26 @@ +envsubst < databricks.yml.tmpl > databricks.yml + +OLD_SCHEMA="main.test-schema-${UNIQUE_NAME}" +NEW_SCHEMA="main.test-schema-renamed-${UNIQUE_NAME}" + +cleanup() { + errcode $CLI schemas delete "$NEW_SCHEMA" --force > LOG.cleanup 2>&1 + errcode $CLI schemas delete "$OLD_SCHEMA" --force >> LOG.cleanup 2>&1 + + # Consume the destroy's requests so none are left recorded. + print_requests.py //unity-catalog &> LOG.destroy_requests +} +trap cleanup EXIT + +title "Deploy a schema with a volume in it that must not be destroyed" +trace $CLI bundle deploy +print_requests.py //unity-catalog &> LOG.deploy_requests + +title "Rename the schema out of band. UC moves the volume with it" +trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | jq '{full_name}' +print_requests.py //unity-catalog &> LOG.rename_requests + +title "Destroy finds the volume in the renamed schema, so prevent_destroy still stops it" +trace musterr $CLI bundle destroy --auto-approve +trace print_requests.py //unity-catalog +trace $CLI volumes read "$NEW_SCHEMA.myvolume" | jq '{full_name}' diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/test.toml b/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/test.toml new file mode 100644 index 00000000000..4272a80cf61 --- /dev/null +++ b/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/test.toml @@ -0,0 +1,7 @@ +Cloud = true + +Ignore = [ + ".databricks", + ".gitignore", + "databricks.yml", +] diff --git a/bundle/direct/bundle_plan.go b/bundle/direct/bundle_plan.go index a459b84a275..985ce88cdce 100644 --- a/bundle/direct/bundle_plan.go +++ b/bundle/direct/bundle_plan.go @@ -376,7 +376,6 @@ func (b *DeploymentBundle) CalculatePlan(ctx context.Context, client *databricks return false } if otherID != "" { - warnOtherResource(ctx, adapter, dbentry.ID, savedState, otherID) remoteState = nil } if remoteState == nil { @@ -386,6 +385,9 @@ func (b *DeploymentBundle) CalculatePlan(ctx context.Context, client *databricks return false } } + if otherID != "" && remoteState == nil { + warnOtherResource(ctx, adapter, dbentry.ID, savedState, otherID) + } // We have a choice whether to include remoteState or remoteStateComparable from below. // Including remoteState because in the near future remoteState is expected to become a superset struct of remoteStateComparable @@ -482,6 +484,10 @@ func (b *DeploymentBundle) CalculatePlan(ctx context.Context, client *databricks return nil, err } + if err := b.findInMovedParents(ctx, plan); err != nil { + return nil, err + } + for _, entry := range plan.Plan { if entry.Action == deployplan.Skip { entry.NewState = nil @@ -900,6 +906,128 @@ func isParentIDField(adapter *dresources.Adapter, path *structpath.PathNode) boo return ok } +// findInMovedParents handles a Delete of a resource with an internal ID that was not found (Gone) +// while its parent, as recorded in its state's depends_on, is deleted at another ID: renamed out of +// band and found there by its internal ID (see findByInternalID). The rename moved the resource with +// the parent, so it is looked for under the parent's new name and, if found there with its internal +// ID, deleted as itself before the parent. Otherwise it would go with the parent's delete unlisted, +// past its own lifecycle.prevent_destroy. +func (b *DeploymentBundle) findInMovedParents(ctx context.Context, plan *deployplan.Plan) error { + for _, resourceKey := range slices.Sorted(maps.Keys(plan.Plan)) { + entry := plan.Plan[resourceKey] + if entry.Action != deployplan.Delete || !entry.Gone { + continue + } + adapter, err := b.getAdapterForKey(resourceKey) + if err != nil { + return err + } + dbentry, ok := b.StateDB.GetResourceEntry(resourceKey) + if !adapter.HasInternalID() || !ok || len(dbentry.State) == 0 { + continue + } + state, err := parseState(adapter.StateType(), dbentry.State) + if err != nil { + return fmt.Errorf("%s: interpreting state: %w", resourceKey, err) + } + savedInternalID, err := adapter.InternalID(state) + if err != nil || savedInternalID == "" { + continue + } + + moved := false + for _, dep := range dbentry.DependsOn { + changed, err := b.followMovedParent(plan, adapter, state, dep) + if err != nil { + return fmt.Errorf("%s: %w", resourceKey, err) + } + moved = moved || changed + } + if !moved { + continue + } + + id, err := adapter.IDFromState(state) + if err != nil { + return fmt.Errorf("%s: %w", resourceKey, err) + } + remote, err := retry.With(ctx, retry.IsTransientRead, func() (any, error) { + return adapter.DoRead(ctx, id) + }) + if err != nil { + if err := ignoreMissingOrForbidden(ctx, err, fmt.Sprintf("%s: reading at id=%q under its renamed parent", resourceKey, id)); err != nil { + return err + } + continue + } + if ok, err := hasInternalIDOf(adapter, remote, savedInternalID); err != nil || !ok { + if err != nil { + return fmt.Errorf("%s: %w", resourceKey, err) + } + continue + } + entry.ID = id + entry.RemoteState = remote + entry.Gone = false + } + return nil +} + +// followMovedParent sets in state, the saved state of a resource, the new values of its parent ID +// fields if dep is a parent deleted at another ID than its own recorded one. It reports whether it +// changed anything. +func (b *DeploymentBundle) followMovedParent(plan *deployplan.Plan, adapter *dresources.Adapter, state any, dep deployplan.DependsOnEntry) (bool, error) { + parent := plan.Plan[dep.Node] + if parent == nil || parent.Action != deployplan.Delete || parent.ID == "" || parent.RemoteState == nil { + return false, nil + } + targetKey, targetField, ok, err := pureRefTarget(dep.Label) + if err != nil || !ok || targetKey != dep.Node { + return false, err + } + parentAdapter, err := b.getAdapterForKey(dep.Node) + if err != nil { + return false, err + } + parentEntry, ok := b.StateDB.GetResourceEntry(dep.Node) + if !ok || len(parentEntry.State) == 0 { + return false, nil + } + parentSaved, err := parseState(parentAdapter.StateType(), parentEntry.State) + if err != nil { + return false, fmt.Errorf("%s: interpreting state: %w", dep.Node, err) + } + parentRemote, err := parentAdapter.RemapState(parent.RemoteState) + if err != nil { + return false, err + } + oldValue, err := structaccess.Get(parentSaved, targetField) + if err != nil { + return false, nil + } + newValue, err := structaccess.Get(parentRemote, targetField) + if err != nil || structdiff.IsEqual(oldValue, newValue) { + return false, nil + } + + changed := false + for _, rule := range adapter.ResourceConfig().ParentIDFields { + path, err := structpath.ParsePath(rule.Field.String()) + if err != nil { + return false, err + } + value, err := structaccess.Get(state, path) + if err != nil || !structdiff.IsEqual(value, oldValue) { + continue + } + if err := structaccess.Set(state, path, newValue); err != nil { + return false, fmt.Errorf("setting %s: %w", path, err) + } + changed = true + } + return changed, nil +} + // recordRefTypes records, for each pure-reference field of resourceKey, the type of the resource it // references, before resolveReferences drops the references (see addParentMoves). func (b *DeploymentBundle) recordRefTypes(resourceKey string) error { @@ -1015,10 +1143,11 @@ func otherInternalID(adapter *dresources.Adapter, savedState, remote any) (strin } // warnOtherResource tells the user that what is at the recorded ID is not the bundle's and is left -// alone. A resource deleted and created again out of band looks the same, hence the way to take it over. +// alone, when the bundle's own is not found elsewhere either. A resource deleted and created again +// out of band looks the same, hence the way to take it over. func warnOtherResource(ctx context.Context, adapter *dresources.Adapter, id string, savedState any, otherID string) { saved, _ := adapter.InternalID(savedState) - log.Warnf(ctx, "id=%q holds another object: its %s is %q, not %q as recorded, so it is not treated as this bundle's. To manage it with this bundle, run 'bundle deployment unbind' and 'bundle deployment bind'.", + log.Warnf(ctx, "id=%q holds another object: its %s is %q, not %q as recorded, so this bundle leaves it alone. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again.", id, adapter.ResourceConfig().InternalIDField, otherID, saved) } @@ -1054,10 +1183,11 @@ func (b *DeploymentBundle) findDeleteTarget(ctx context.Context, resourceKey str if remote != nil && otherID == "" { return "", remote, nil } - if otherID != "" { + movedID, moved, err := findByInternalID(ctx, adapter, savedState) + if err == nil && moved == nil && otherID != "" { warnOtherResource(ctx, adapter, id, savedState, otherID) } - return findByInternalID(ctx, adapter, savedState) + return movedID, moved, err } // findByInternalID looks for a resource to be deleted that is not at its recorded ID: renamed out of From 50ee46a95da1922d324349b83760bac04bfff4f6 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Wed, 25 Nov 2026 16:34:27 +0000 Subject: [PATCH 04/22] direct: list a child of a deleted, moved parent by its ID under the parent's new one Address review: map the child's ID by its parent's ID prefix instead of by field value (a schema named like its catalog); follow a parent re-keyed in a deploy too; and when the parent is deleted, delete the child at its new ID even if not found there, so prevent_destroy applies. Co-authored-by: Isaac --- .../output.txt | 18 +++ .../prevent_destroy_in_renamed_schema/script | 7 + bundle/direct/bundle_plan.go | 131 +++++++----------- bundle/direct/bundle_plan_test.go | 26 ++++ bundle/direct/dresources/README.md | 2 +- 5 files changed, 100 insertions(+), 84 deletions(-) diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/output.txt b/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/output.txt index e3c3b0e4393..4d5c9ebc906 100644 --- a/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/output.txt +++ b/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/output.txt @@ -27,3 +27,21 @@ Error: resources.volumes.volume1 has lifecycle.prevent_destroy set, but the plan { "full_name": "main.test-schema-renamed-[UNIQUE_NAME].myvolume" } + +=== With the volume renamed out of band too, it is not found, but destroy still lists it, so prevent_destroy still stops it + +>>> [CLI] volumes update main.test-schema-renamed-[UNIQUE_NAME].myvolume --new-name myvolume2 +{ + "full_name": "main.test-schema-renamed-[UNIQUE_NAME].myvolume2" +} + +>>> musterr [CLI] bundle destroy --auto-approve +Error: resources.volumes.volume1 has lifecycle.prevent_destroy set, but the plan calls for this resource to be recreated or destroyed. To avoid this error, disable lifecycle.prevent_destroy for resources.volumes.volume1 + + +>>> print_requests.py //unity-catalog + +>>> [CLI] volumes read main.test-schema-renamed-[UNIQUE_NAME].myvolume2 +{ + "full_name": "main.test-schema-renamed-[UNIQUE_NAME].myvolume2" +} diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/script b/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/script index ad354bf60f7..5770a63a49f 100644 --- a/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/script +++ b/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/script @@ -24,3 +24,10 @@ title "Destroy finds the volume in the renamed schema, so prevent_destroy still trace musterr $CLI bundle destroy --auto-approve trace print_requests.py //unity-catalog trace $CLI volumes read "$NEW_SCHEMA.myvolume" | jq '{full_name}' + +title "With the volume renamed out of band too, it is not found, but destroy still lists it, so prevent_destroy still stops it" +trace $CLI volumes update "$NEW_SCHEMA.myvolume" --new-name myvolume2 | jq '{full_name}' +print_requests.py //unity-catalog &> LOG.rename2_requests +trace musterr $CLI bundle destroy --auto-approve +trace print_requests.py //unity-catalog +trace $CLI volumes read "$NEW_SCHEMA.myvolume2" | jq '{full_name}' diff --git a/bundle/direct/bundle_plan.go b/bundle/direct/bundle_plan.go index 985ce88cdce..98b583b2b3f 100644 --- a/bundle/direct/bundle_plan.go +++ b/bundle/direct/bundle_plan.go @@ -906,64 +906,36 @@ func isParentIDField(adapter *dresources.Adapter, path *structpath.PathNode) boo return ok } -// findInMovedParents handles a Delete of a resource with an internal ID that was not found (Gone) -// while its parent, as recorded in its state's depends_on, is deleted at another ID: renamed out of -// band and found there by its internal ID (see findByInternalID). The rename moved the resource with -// the parent, so it is looked for under the parent's new name and, if found there with its internal -// ID, deleted as itself before the parent. Otherwise it would go with the parent's delete unlisted, -// past its own lifecycle.prevent_destroy. +// findInMovedParents handles a Delete of a resource not found at its recorded ID (Gone) while its +// parent, as recorded in its state's depends_on, was found at another ID in this plan: renamed out of +// band (see findMoved and findByInternalID). The rename moved the resource with the parent, so it is +// looked for under the parent's new ID and, if found there with its internal ID, deleted as itself, +// before the parent. When the parent is deleted too, the resource is deleted there even if it is not +// found: it is in the parent, whose delete would take it along unlisted, past its own +// lifecycle.prevent_destroy. func (b *DeploymentBundle) findInMovedParents(ctx context.Context, plan *deployplan.Plan) error { for _, resourceKey := range slices.Sorted(maps.Keys(plan.Plan)) { entry := plan.Plan[resourceKey] if entry.Action != deployplan.Delete || !entry.Gone { continue } - adapter, err := b.getAdapterForKey(resourceKey) - if err != nil { - return err - } dbentry, ok := b.StateDB.GetResourceEntry(resourceKey) - if !adapter.HasInternalID() || !ok || len(dbentry.State) == 0 { + if !ok { continue } - state, err := parseState(adapter.StateType(), dbentry.State) - if err != nil { - return fmt.Errorf("%s: interpreting state: %w", resourceKey, err) - } - savedInternalID, err := adapter.InternalID(state) - if err != nil || savedInternalID == "" { + id, parentDeleted := b.idUnderMovedParent(plan, dbentry) + if id == "" { continue } - - moved := false - for _, dep := range dbentry.DependsOn { - changed, err := b.followMovedParent(plan, adapter, state, dep) - if err != nil { - return fmt.Errorf("%s: %w", resourceKey, err) - } - moved = moved || changed - } - if !moved { - continue - } - - id, err := adapter.IDFromState(state) + adapter, err := b.getAdapterForKey(resourceKey) if err != nil { - return fmt.Errorf("%s: %w", resourceKey, err) + return err } - remote, err := retry.With(ctx, retry.IsTransientRead, func() (any, error) { - return adapter.DoRead(ctx, id) - }) + remote, err := b.readIfOwn(ctx, adapter, dbentry, id) if err != nil { - if err := ignoreMissingOrForbidden(ctx, err, fmt.Sprintf("%s: reading at id=%q under its renamed parent", resourceKey, id)); err != nil { - return err - } - continue + return fmt.Errorf("%s: %w", resourceKey, err) } - if ok, err := hasInternalIDOf(adapter, remote, savedInternalID); err != nil || !ok { - if err != nil { - return fmt.Errorf("%s: %w", resourceKey, err) - } + if remote == nil && !parentDeleted { continue } entry.ID = id @@ -973,59 +945,52 @@ func (b *DeploymentBundle) findInMovedParents(ctx context.Context, plan *deployp return nil } -// followMovedParent sets in state, the saved state of a resource, the new values of its parent ID -// fields if dep is a parent deleted at another ID than its own recorded one. It reports whether it -// changed anything. -func (b *DeploymentBundle) followMovedParent(plan *deployplan.Plan, adapter *dresources.Adapter, state any, dep deployplan.DependsOnEntry) (bool, error) { - parent := plan.Plan[dep.Node] - if parent == nil || parent.Action != deployplan.Delete || parent.ID == "" || parent.RemoteState == nil { - return false, nil - } - targetKey, targetField, ok, err := pureRefTarget(dep.Label) - if err != nil || !ok || targetKey != dep.Node { - return false, err - } - parentAdapter, err := b.getAdapterForKey(dep.Node) - if err != nil { - return false, err - } - parentEntry, ok := b.StateDB.GetResourceEntry(dep.Node) - if !ok || len(parentEntry.State) == 0 { - return false, nil +// readIfOwn reads the resource recorded in dbentry at id and returns it if it has its recorded +// internal ID; nil if it is not there, is another one, or there is no internal ID to tell. +func (b *DeploymentBundle) readIfOwn(ctx context.Context, adapter *dresources.Adapter, dbentry dstate.ResourceEntry, id string) (any, error) { + if !adapter.HasInternalID() || len(dbentry.State) == 0 { + return nil, nil } - parentSaved, err := parseState(parentAdapter.StateType(), parentEntry.State) + state, err := parseState(adapter.StateType(), dbentry.State) if err != nil { - return false, fmt.Errorf("%s: interpreting state: %w", dep.Node, err) + return nil, fmt.Errorf("interpreting state: %w", err) } - parentRemote, err := parentAdapter.RemapState(parent.RemoteState) - if err != nil { - return false, err + savedInternalID, err := adapter.InternalID(state) + if err != nil || savedInternalID == "" { + return nil, err } - oldValue, err := structaccess.Get(parentSaved, targetField) + remote, err := retry.With(ctx, retry.IsTransientRead, func() (any, error) { + return adapter.DoRead(ctx, id) + }) if err != nil { - return false, nil + return nil, ignoreMissingOrForbidden(ctx, err, fmt.Sprintf("reading at id=%q under its renamed parent", id)) } - newValue, err := structaccess.Get(parentRemote, targetField) - if err != nil || structdiff.IsEqual(oldValue, newValue) { - return false, nil + ok, err := hasInternalIDOf(adapter, remote, savedInternalID) + if err != nil || !ok { + return nil, err } + return remote, nil +} - changed := false - for _, rule := range adapter.ResourceConfig().ParentIDFields { - path, err := structpath.ParsePath(rule.Field.String()) - if err != nil { - return false, err +// idUnderMovedParent returns the ID of the resource recorded in dbentry under the new ID of a parent +// (from its depends_on) found at another ID in this plan, "" if there is none, and whether that parent +// is deleted. A UC full name starts with its parent's: main.s.v is in main.s, so in main.t after the +// schema's rename. +func (b *DeploymentBundle) idUnderMovedParent(plan *deployplan.Plan, dbentry dstate.ResourceEntry) (string, bool) { + for _, dep := range dbentry.DependsOn { + parent := plan.Plan[dep.Node] + if parent == nil || parent.ID == "" { + continue } - value, err := structaccess.Get(state, path) - if err != nil || !structdiff.IsEqual(value, oldValue) { + parentID := b.StateDB.GetResourceID(dep.Node) + if parentID == "" || parentID == parent.ID { continue } - if err := structaccess.Set(state, path, newValue); err != nil { - return false, fmt.Errorf("setting %s: %w", path, err) + if rest, ok := strings.CutPrefix(dbentry.ID, parentID+"."); ok { + return parent.ID + "." + rest, parent.Action == deployplan.Delete && !parent.Gone } - changed = true } - return changed, nil + return "", false } // recordRefTypes records, for each pure-reference field of resourceKey, the type of the resource it diff --git a/bundle/direct/bundle_plan_test.go b/bundle/direct/bundle_plan_test.go index 5b4c541dabe..8b5b69d2b2a 100644 --- a/bundle/direct/bundle_plan_test.go +++ b/bundle/direct/bundle_plan_test.go @@ -1067,3 +1067,29 @@ func TestFindDeleteTargetUnverifiedRead(t *testing.T) { assert.Empty(t, id) assert.Nil(t, got) } + +func TestIDUnderMovedParent(t *testing.T) { + const schemaKey = "resources.schemas.schema1" + dep := deployplan.DependsOnEntry{Node: schemaKey, Label: "${resources.schemas.schema1.name}"} + + for name, tt := range map[string]struct { + schemaID, foundID, volumeID, want string + }{ + "moved": {schemaID: "main.s", foundID: "main.t", volumeID: "main.s.v", want: "main.t.v"}, + "schema named as catalog": {schemaID: "main.main", foundID: "main.moved", volumeID: "main.main.v", want: "main.moved.v"}, + "parent not moved": {schemaID: "main.s", foundID: "", volumeID: "main.s.v", want: ""}, + "not under the parent": {schemaID: "main.s", foundID: "main.t", volumeID: "main.other.v", want: ""}, + } { + t.Run(name, func(t *testing.T) { + plan := deployplan.NewPlanDirect() + plan.Plan[schemaKey] = &deployplan.PlanEntry{Action: deployplan.Delete, ID: tt.foundID} + b := &DeploymentBundle{} + b.StateDB.OpenWithData("state.json", dstate.Database{State: map[string]dstate.ResourceEntry{ + schemaKey: {ID: tt.schemaID}, + }}) + got, parentDeleted := b.idUnderMovedParent(plan, dstate.ResourceEntry{ID: tt.volumeID, DependsOn: []deployplan.DependsOnEntry{dep}}) + assert.Equal(t, tt.want, got) + assert.Equal(t, tt.want != "", parentDeleted) + }) + } +} diff --git a/bundle/direct/dresources/README.md b/bundle/direct/dresources/README.md index 8e92e136c6e..bd0497a9486 100644 --- a/bundle/direct/dresources/README.md +++ b/bundle/direct/dresources/README.md @@ -62,7 +62,7 @@ If the ID is built from the resource's own fields (a UC full name), implement `I If the backend also has its own ID that a rename keeps (a volume's `volume_id`), add it to the state type, copy it in `RemapState`, and name it in the config as `internal_id_field`. The framework records it on every save and skips its changes in the plan. When nothing is at the recorded ID, the plan reads at the ID the config gives the resource and takes what it finds only if the internal ID matches. This covers a rename done out of band, or by a deploy that stopped before re-keying the resource. -An object at the recorded ID with a different internal ID is someone else's (ours was renamed away and another created under its name): the plan treats ours as not there, so it never updates or deletes the other object. A resource to be deleted (by destroy, or removed from config) has no config to give its new name, so to delete one renamed out of band the resource needs `DoFindByInternalID`: it searches the parent (lists the schemas in the catalog, the volumes in the schema) for the recorded internal ID. Without it, or when the parent itself was renamed, the delete only drops the state entry of a resource not found at its recorded ID. +An object at the recorded ID with a different internal ID is someone else's (ours was renamed away and another created under its name): the plan treats ours as not there, so it never updates or deletes the other object. A resource to be deleted (by destroy, or removed from config) has no config to give its new name, so to delete one renamed out of band the resource needs `DoFindByInternalID`: it searches the parent (lists the schemas in the catalog, the volumes in the schema) for the recorded internal ID. Without it, the delete only drops the state entry of a resource not found at its recorded ID. A child whose parent the plan found at a new ID (a volume in a renamed schema) is looked for under the parent's new ID; when the parent is deleted too, the child is deleted there as itself, so its `prevent_destroy` applies. A parent outside the bundle that was renamed is not followed. ## Empty states: IsEmptyState From 580748470b4b65c5e675786e044017adbef39e37 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Thu, 26 Nov 2026 13:27:51 +0000 Subject: [PATCH 05/22] Add the PR link to the changelog fragment aws: TestAccept/bundle/resources/schemas/foreign_at_recorded_name TestAccept/bundle/resources/schemas/destroy_after_rename_out_of_band TestAccept/bundle/resources/schemas/recreated_at_recorded_name TestAccept/bundle/resources/volumes/foreign_at_recorded_name TestAccept/bundle/resources/volumes/prevent_destroy_in_renamed_schema Co-authored-by: Isaac --- .nextchanges/bundles/schema-volume-identity.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.nextchanges/bundles/schema-volume-identity.md b/.nextchanges/bundles/schema-volume-identity.md index f1fa28dd413..9513ed3e751 100644 --- a/.nextchanges/bundles/schema-volume-identity.md +++ b/.nextchanges/bundles/schema-volume-identity.md @@ -1 +1 @@ -* Fixed `bundle destroy` deleting a schema or volume that someone else created under the name of the bundle's own after it was renamed out of band; plan and deploy no longer take it for the bundle's, and destroy deletes the bundle's renamed schema or volume instead of leaving it behind, in the direct deployment engine. A schema or volume deleted and created again outside the bundle is likewise no longer the bundle's: deploy fails on its name and destroy leaves it, until it is bound again with `bundle deployment unbind` and `bundle deployment bind`. +* Fixed `bundle destroy` deleting a schema or volume that someone else created under the name of the bundle's own after it was renamed out of band; plan and deploy no longer take it for the bundle's, and destroy deletes the bundle's renamed schema or volume instead of leaving it behind, in the direct deployment engine. A schema or volume deleted and created again outside the bundle is likewise no longer the bundle's: deploy fails on its name and destroy leaves it, until it is bound again with `bundle deployment unbind` and `bundle deployment bind`. ([#8056](https://github.com/databricks/cli/pull/8056)) From 8db1a2816abbbeb6c3ae93ee9bfce1e548f6fc1e Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Thu, 26 Nov 2026 13:58:31 +0000 Subject: [PATCH 06/22] direct: set every field of the schema and volume list requests (exhaustruct) aws: TestAccept/bundle/resources/schemas/foreign_at_recorded_name TestAccept/bundle/resources/schemas/destroy_after_rename_out_of_band TestAccept/bundle/resources/schemas/recreated_at_recorded_name TestAccept/bundle/resources/volumes/foreign_at_recorded_name TestAccept/bundle/resources/volumes/prevent_destroy_in_renamed_schema Co-authored-by: Isaac --- bundle/direct/dresources/schema.go | 8 +++++++- bundle/direct/dresources/volume.go | 9 ++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/bundle/direct/dresources/schema.go b/bundle/direct/dresources/schema.go index f053a3642db..ce3b9b6a6dd 100644 --- a/bundle/direct/dresources/schema.go +++ b/bundle/direct/dresources/schema.go @@ -166,7 +166,13 @@ func (*ResourceSchema) IDFromState(state *SchemaState) string { // DoFindByInternalID lists the schemas in the catalog for the one with the recorded schema_id. func (r *ResourceSchema) DoFindByInternalID(ctx context.Context, state *SchemaState) (string, error) { - iter := r.client.Schemas.List(ctx, catalog.ListSchemasRequest{CatalogName: state.CatalogName}) + iter := r.client.Schemas.List(ctx, catalog.ListSchemasRequest{ + CatalogName: state.CatalogName, + IncludeBrowse: false, + MaxResults: 0, // The server's page size; the iterator follows the pages. + PageToken: "", + ForceSendFields: nil, + }) for iter.HasNext(ctx) { schema, err := iter.Next(ctx) if err != nil { diff --git a/bundle/direct/dresources/volume.go b/bundle/direct/dresources/volume.go index 08dd405e367..a31160676b3 100644 --- a/bundle/direct/dresources/volume.go +++ b/bundle/direct/dresources/volume.go @@ -169,7 +169,14 @@ func (*ResourceVolume) IDFromState(state *VolumeState) string { // volume whose schema was renamed is not found; when the schema is in the bundle too, deleting it // deletes the volume. func (r *ResourceVolume) DoFindByInternalID(ctx context.Context, state *VolumeState) (string, error) { - iter := r.client.Volumes.List(ctx, catalog.ListVolumesRequest{CatalogName: state.CatalogName, SchemaName: state.SchemaName}) + iter := r.client.Volumes.List(ctx, catalog.ListVolumesRequest{ + CatalogName: state.CatalogName, + SchemaName: state.SchemaName, + IncludeBrowse: false, + MaxResults: 0, // The server's page size; the iterator follows the pages. + PageToken: "", + ForceSendFields: nil, + }) for iter.HasNext(ctx) { volume, err := iter.Next(ctx) if err != nil { From addc8e067e74d361e332db331a3461f835708027 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Thu, 26 Nov 2026 16:25:37 +0000 Subject: [PATCH 07/22] direct: never send an unverified child delete; find moved parents by full name Address review on #8056: - a child under a parent deleted at a moved ID that cannot be verified there (models and secrets included) gets with_parent: listed, counted and checked for prevent_destroy, but no delete is sent at the guessed name; - parents are matched by case-insensitive full-name prefix over every UC resource the plan found at a moved ID, not through depends_on; - warnings are diagnostics, a Delete's is told after the parent pass, and errors name schema_id/volume_id; - comments, README and changelog updated; schema_fields/volume_fields helpers for the new tests. Co-authored-by: Isaac --- .../bundles/schema-volume-identity.md | 2 +- acceptance/bin/replay_tfstate.py | 2 +- .../destroy_after_rename_out_of_band/script | 2 +- .../databricks.yml.tmpl | 23 +++ .../out.test.toml | 2 + .../output.txt | 43 ++++ .../script | 24 +++ .../test.toml | 7 + .../schemas/destroy_search_fails/output.txt | 8 +- .../schemas/destroy_search_fails/script | 4 +- .../foreign_at_recorded_name/output.txt | 6 +- .../schemas/foreign_at_recorded_name/script | 4 +- .../recreated_at_recorded_name/output.txt | 6 +- .../bundle/resources/schemas/script.prepare | 6 + .../foreign_at_recorded_name/output.txt | 3 +- .../volumes/foreign_at_recorded_name/script | 4 +- .../output.txt | 12 +- .../prevent_destroy_in_renamed_schema/script | 8 +- .../databricks.yml.tmpl | 15 ++ .../out.test.toml | 2 + .../output.txt | 51 +++++ .../prevent_destroy_mixed_case_schema/script | 32 +++ .../test.toml | 7 + .../databricks-schema-only.yml.tmpl | 8 + .../databricks.yml.tmpl | 15 ++ .../out.test.toml | 2 + .../output.txt | 36 ++++ .../script | 28 +++ .../test.toml | 7 + ...databricks-renamed-without-volume.yml.tmpl | 8 + .../databricks.yml.tmpl | 13 ++ .../removed_from_renamed_schema/out.test.toml | 2 + .../removed_from_renamed_schema/output.txt | 56 ++++++ .../removed_from_renamed_schema/script | 28 +++ .../removed_from_renamed_schema/test.toml | 7 + .../bundle/resources/volumes/script.prepare | 11 + bundle/deployplan/action.go | 6 +- bundle/deployplan/plan.go | 33 ++- bundle/deployplan/plan_test.go | 18 ++ bundle/direct/apply.go | 2 +- bundle/direct/bundle_apply.go | 9 +- bundle/direct/bundle_plan.go | 188 +++++++++++++----- bundle/direct/bundle_plan_test.go | 36 ++-- bundle/direct/dresources/README.md | 2 +- bundle/direct/dresources/volume.go | 4 +- bundle/direct/pkg.go | 4 + 46 files changed, 681 insertions(+), 115 deletions(-) create mode 100644 acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/databricks.yml.tmpl create mode 100644 acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/out.test.toml create mode 100644 acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/output.txt create mode 100644 acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/script create mode 100644 acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/test.toml create mode 100644 acceptance/bundle/resources/schemas/script.prepare create mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/databricks.yml.tmpl create mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/out.test.toml create mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/output.txt create mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/script create mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/test.toml create mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/databricks-schema-only.yml.tmpl create mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/databricks.yml.tmpl create mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/out.test.toml create mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/output.txt create mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/script create mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/test.toml create mode 100644 acceptance/bundle/resources/volumes/removed_from_renamed_schema/databricks-renamed-without-volume.yml.tmpl create mode 100644 acceptance/bundle/resources/volumes/removed_from_renamed_schema/databricks.yml.tmpl create mode 100644 acceptance/bundle/resources/volumes/removed_from_renamed_schema/out.test.toml create mode 100644 acceptance/bundle/resources/volumes/removed_from_renamed_schema/output.txt create mode 100644 acceptance/bundle/resources/volumes/removed_from_renamed_schema/script create mode 100644 acceptance/bundle/resources/volumes/removed_from_renamed_schema/test.toml create mode 100644 acceptance/bundle/resources/volumes/script.prepare diff --git a/.nextchanges/bundles/schema-volume-identity.md b/.nextchanges/bundles/schema-volume-identity.md index 9513ed3e751..7f3ccbef27e 100644 --- a/.nextchanges/bundles/schema-volume-identity.md +++ b/.nextchanges/bundles/schema-volume-identity.md @@ -1 +1 @@ -* Fixed `bundle destroy` deleting a schema or volume that someone else created under the name of the bundle's own after it was renamed out of band; plan and deploy no longer take it for the bundle's, and destroy deletes the bundle's renamed schema or volume instead of leaving it behind, in the direct deployment engine. A schema or volume deleted and created again outside the bundle is likewise no longer the bundle's: deploy fails on its name and destroy leaves it, until it is bound again with `bundle deployment unbind` and `bundle deployment bind`. ([#8056](https://github.com/databricks/cli/pull/8056)) +* Fixed `bundle destroy` deleting a schema or volume that someone else created under the name of the bundle's own after it was renamed out of band, in the direct deployment engine. Plan and deploy no longer take such an object for the bundle's, and destroy deletes the bundle's renamed schema or volume instead of leaving it behind. A deploy that removes such a renamed schema or volume from the config now deletes it at its new name too (a schema with its contents); before, it only dropped it from the state. A schema or volume deleted and created again outside the bundle is no longer the bundle's either: deploy fails on its name and destroy leaves it, until it is bound again with `bundle deployment unbind` and `bundle deployment bind`. ([#8056](https://github.com/databricks/cli/pull/8056)) diff --git a/acceptance/bin/replay_tfstate.py b/acceptance/bin/replay_tfstate.py index 6d6fa691498..4b4ee67a979 100755 --- a/acceptance/bin/replay_tfstate.py +++ b/acceptance/bin/replay_tfstate.py @@ -127,7 +127,7 @@ def main(): if attrs.get(nf): name_to_attrs[str(attrs[nf])] = attrs # UC lowercases names (a schema created as MySchema is stored as myschema), so the - # tfstate can hold the lowercased name of the create request's. + # tfstate may hold the lowercased form of the name the create request sent. name_to_attrs.setdefault(str(attrs[nf]).lower(), attrs) id_map = {} diff --git a/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/script b/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/script index 53ea2924014..6cd24237b09 100644 --- a/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/script +++ b/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/script @@ -18,7 +18,7 @@ print_requests.py //unity-catalog &> LOG.deploy_requests add_repl "$($CLI schemas get "$OLD_SCHEMA" | jq -r .schema_id)" SCHEMA_ID title "Rename the schema out of band, and the config to match" -trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | jq '{full_name, schema_id}' +trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields print_requests.py //unity-catalog &> LOG.rename_requests trace update_file.py databricks.yml test-schema- test-schema-renamed- diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/databricks.yml.tmpl b/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/databricks.yml.tmpl new file mode 100644 index 00000000000..b8ea3af2c01 --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/databricks.yml.tmpl @@ -0,0 +1,23 @@ +bundle: + name: test-bundle-$UNIQUE_NAME + +variables: + secret_value: + description: The secret's value + +resources: + schemas: + schema1: + catalog_name: main + name: test-schema-$UNIQUE_NAME + registered_models: + model1: + catalog_name: main + schema_name: ${resources.schemas.schema1.name} + name: mymodel + secrets: + secret1: + catalog_name: main + schema_name: ${resources.schemas.schema1.name} + name: mysecret + value: ${var.secret_value} diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/out.test.toml b/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/out.test.toml new file mode 100644 index 00000000000..0f167effef1 --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/out.test.toml @@ -0,0 +1,2 @@ +Cloud = true +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/output.txt b/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/output.txt new file mode 100644 index 00000000000..b19f72f8b30 --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/output.txt @@ -0,0 +1,43 @@ + +=== Deploy a schema with a registered model and a secret in it, and rename the schema out of band + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Created registered_models.model1 +Created schemas.schema1 +Created secrets.secret1 +Files: 2 uploaded, 0 deleted +Resources: 3 created, 0 changed, 0 deleted, 0 unchanged + +>>> [CLI] schemas update main.test-schema-[UNIQUE_NAME] --new-name test-schema-renamed-[UNIQUE_NAME] +{ + "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", + "schema_id": "[SCHEMA_ID]" +} + +=== The model and the secret have no internal ID to find them by, so destroy lists them as going with the schema it found, and sends a delete only for the schema + +>>> [CLI] bundle destroy --auto-approve +The following resources will be deleted: + delete resources.registered_models.model1 + delete resources.schemas.schema1 + delete resources.secrets.secret1 + +This action will result in the deletion of the following UC schemas. Any underlying data may be lost: + delete resources.schemas.schema1 + +All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default + +Destroy: 3 deleted + +>>> print_requests.py //unity-catalog +{ + "method": "DELETE", + "path": "/api/2.1/unity-catalog/schemas/main.test-schema-renamed-[UNIQUE_NAME]", + "q": { + "force": "true" + } +} + +>>> musterr [CLI] schemas get main.test-schema-renamed-[UNIQUE_NAME] +Error: Schema 'main.test-schema-renamed-[UNIQUE_NAME]' does not exist. diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/script b/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/script new file mode 100644 index 00000000000..ad91de3a978 --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/script @@ -0,0 +1,24 @@ +envsubst < databricks.yml.tmpl > databricks.yml +export BUNDLE_VAR_secret_value=secret-value + +OLD_SCHEMA="main.test-schema-${UNIQUE_NAME}" +NEW_SCHEMA="main.test-schema-renamed-${UNIQUE_NAME}" + +cleanup() { + errcode $CLI schemas delete "$NEW_SCHEMA" --force > LOG.cleanup 2>&1 + + # Consume the destroy's requests so none are left recorded. + print_requests.py //unity-catalog &> LOG.destroy_requests +} +trap cleanup EXIT + +title "Deploy a schema with a registered model and a secret in it, and rename the schema out of band" +trace $CLI bundle deploy +trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields +print_requests.py //unity-catalog &> LOG.deploy_requests +add_repl "$($CLI schemas get "$NEW_SCHEMA" | jq -r .schema_id)" SCHEMA_ID + +title "The model and the secret have no internal ID to find them by, so destroy lists them as going with the schema it found, and sends a delete only for the schema" +trace $CLI bundle destroy --auto-approve +trace print_requests.py //unity-catalog +trace musterr $CLI schemas get "$NEW_SCHEMA" diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/test.toml b/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/test.toml new file mode 100644 index 00000000000..4272a80cf61 --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/test.toml @@ -0,0 +1,7 @@ +Cloud = true + +Ignore = [ + ".databricks", + ".gitignore", + "databricks.yml", +] diff --git a/acceptance/bundle/resources/schemas/destroy_search_fails/output.txt b/acceptance/bundle/resources/schemas/destroy_search_fails/output.txt index e280669aacd..3e33a5d1058 100644 --- a/acceptance/bundle/resources/schemas/destroy_search_fails/output.txt +++ b/acceptance/bundle/resources/schemas/destroy_search_fails/output.txt @@ -10,13 +10,14 @@ Resources: 2 created, 0 changed, 0 deleted, 0 unchanged >>> [CLI] schemas update main.test-schema-[UNIQUE_NAME] --new-name test-schema-renamed-[UNIQUE_NAME] { - "full_name": "main.test-schema-renamed-[UNIQUE_NAME]" + "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", + "schema_id": "[UUID]" } === The search for the renamed schema fails, so destroy fails without deleting anything or dropping the state >>> musterr [CLI] bundle destroy --auto-approve -Error: cannot plan resources.schemas.schema1: looking for a renamed resource by its internal id "[UUID]": injected (500 INTERNAL_ERROR) +Error: cannot plan resources.schemas.schema1: looking for schema_id "[UUID]" after a rename outside the bundle: injected (500 INTERNAL_ERROR) Endpoint: GET [DATABRICKS_URL]/api/2.1/unity-catalog/schemas?catalog_name=main&max_results=0 HTTP Status: 500 Internal Server Error @@ -40,5 +41,6 @@ Error: planning failed >>> [CLI] schemas get main.test-schema-renamed-[UNIQUE_NAME] { - "full_name": "main.test-schema-renamed-[UNIQUE_NAME]" + "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", + "schema_id": "[UUID]" } diff --git a/acceptance/bundle/resources/schemas/destroy_search_fails/script b/acceptance/bundle/resources/schemas/destroy_search_fails/script index 96bd00736d7..2b395f0d731 100644 --- a/acceptance/bundle/resources/schemas/destroy_search_fails/script +++ b/acceptance/bundle/resources/schemas/destroy_search_fails/script @@ -5,11 +5,11 @@ NEW_SCHEMA="main.test-schema-renamed-${UNIQUE_NAME}" title "Deploy a schema with a volume in it, and rename the schema out of band" trace $CLI bundle deploy -trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | jq '{full_name}' +trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields print_requests.py //unity-catalog &> LOG.deploy_requests title "The search for the renamed schema fails, so destroy fails without deleting anything or dropping the state" trace musterr $CLI bundle destroy --auto-approve trace print_requests.py //unity-catalog trace $CLI bundle summary --output json | jq '.resources | map_values(map_values(.id))' -trace $CLI schemas get "$NEW_SCHEMA" | jq '{full_name}' +trace $CLI schemas get "$NEW_SCHEMA" | schema_fields diff --git a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt index 606a51a0bcb..46e8fb7a17e 100644 --- a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt +++ b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt @@ -18,7 +18,8 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged === The other schema has another schema_id, so the plan does not take it for the bundle's: it plans a create >>> [CLI] bundle plan --output json -Warn: planning resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it alone. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again. +Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it in place. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again. + { "resources.schemas.schema1": { "action": "create", @@ -43,7 +44,8 @@ Warn: planning resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" ho >>> musterr [CLI] bundle deploy Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... -Warn: planning resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it alone. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again. +Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it in place. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again. + Error: cannot create resources.schemas.schema1: Schema 'test-schema-[UNIQUE_NAME]' already exists (400 SCHEMA_ALREADY_EXISTS) Endpoint: POST [DATABRICKS_URL]/api/2.1/unity-catalog/schemas diff --git a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script index 91ee1bfbb67..ac68043644e 100644 --- a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script +++ b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script @@ -24,7 +24,7 @@ print_requests.py //unity-catalog &> LOG.deploy_requests add_repl "$($CLI schemas get "$SCHEMA" | jq -r .schema_id)" SCHEMA_ID title "Out of band, rename the schema away, and create another one under its name" -trace $CLI schemas update "$SCHEMA" --new-name "test-schema-moved-${UNIQUE_NAME}" | jq '{full_name, schema_id}' +trace $CLI schemas update "$SCHEMA" --new-name "test-schema-moved-${UNIQUE_NAME}" | schema_fields add_repl "$($CLI schemas create "test-schema-${UNIQUE_NAME}" main | jq -r .schema_id)" FOREIGN_SCHEMA_ID print_requests.py //unity-catalog &> LOG.oob_requests @@ -41,4 +41,4 @@ trace print_requests.py //unity-catalog trace musterr $CLI schemas get "$MOVED_SCHEMA" title "The other schema is left alone" -trace $CLI schemas get "$SCHEMA" | jq '{full_name, schema_id}' +trace $CLI schemas get "$SCHEMA" | schema_fields diff --git a/acceptance/bundle/resources/schemas/recreated_at_recorded_name/output.txt b/acceptance/bundle/resources/schemas/recreated_at_recorded_name/output.txt index 4c414c61049..87e8c756a0e 100644 --- a/acceptance/bundle/resources/schemas/recreated_at_recorded_name/output.txt +++ b/acceptance/bundle/resources/schemas/recreated_at_recorded_name/output.txt @@ -14,14 +14,16 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged === The new schema has another schema_id: the bundle does not take it for its own, and deploy fails on its name >>> [CLI] bundle plan -Warn: planning resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[NEW_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it alone. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again. +Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[NEW_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it in place. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again. + create schemas.schema1 Plan: 1 to add, 0 to change, 0 to delete, 0 unchanged >>> musterr [CLI] bundle deploy Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... -Warn: planning resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[NEW_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it alone. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again. +Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[NEW_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it in place. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again. + Error: cannot create resources.schemas.schema1: Schema 'test-schema-[UNIQUE_NAME]' already exists (400 SCHEMA_ALREADY_EXISTS) Endpoint: POST [DATABRICKS_URL]/api/2.1/unity-catalog/schemas diff --git a/acceptance/bundle/resources/schemas/script.prepare b/acceptance/bundle/resources/schemas/script.prepare new file mode 100644 index 00000000000..4332cddbd8c --- /dev/null +++ b/acceptance/bundle/resources/schemas/script.prepare @@ -0,0 +1,6 @@ +# schema_fields is the allow-list of schema fields the acceptance tests assert on. Anything the +# backend returns outside this list is dropped, so a new field in the API response does not break +# the golden. +schema_fields() { + jq '{full_name, schema_id} | with_entries(select(.value != null))' +} diff --git a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt index 61f4dd8dc80..ae7b2f481dd 100644 --- a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt +++ b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt @@ -18,7 +18,8 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged === The other volume has another volume_id, so the plan does not take it for the bundle's: it plans a create >>> [CLI] bundle plan -Warn: planning resources.volumes.volume1: id="main.test-schema-[UNIQUE_NAME].myvolume" holds another object: its volume_id is "[FOREIGN_VOLUME_ID]", not "[VOLUME_ID]" as recorded, so this bundle leaves it alone. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again. +Warning: resources.volumes.volume1: id="main.test-schema-[UNIQUE_NAME].myvolume" holds another object: its volume_id is "[FOREIGN_VOLUME_ID]", not "[VOLUME_ID]" as recorded, so this bundle leaves it in place. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again. + create volumes.volume1 Plan: 1 to add, 0 to change, 0 to delete, 0 unchanged diff --git a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script index 27ff93af5bb..880a8cf30fe 100644 --- a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script +++ b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script @@ -19,7 +19,7 @@ print_requests.py //unity-catalog &> LOG.deploy_requests add_repl "$($CLI volumes read "$SCHEMA.myvolume" | jq -r .volume_id)" VOLUME_ID title "Out of band, rename the volume away, and create another one under its name" -trace $CLI volumes update "$SCHEMA.myvolume" --new-name myvolume-moved | jq '{full_name, volume_id}' +trace $CLI volumes update "$SCHEMA.myvolume" --new-name myvolume-moved | volume_fields add_repl "$($CLI volumes create main "test-schema-${UNIQUE_NAME}" myvolume MANAGED | jq -r .volume_id)" FOREIGN_VOLUME_ID print_requests.py //unity-catalog &> LOG.oob_requests @@ -32,4 +32,4 @@ trace print_requests.py //unity-catalog trace musterr $CLI volumes read "$SCHEMA.myvolume-moved" title "The other volume is left alone" -trace $CLI volumes read "$SCHEMA.myvolume" | jq '{full_name, volume_id}' +trace $CLI volumes read "$SCHEMA.myvolume" | volume_fields diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/output.txt b/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/output.txt index 4d5c9ebc906..d82c830d346 100644 --- a/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/output.txt +++ b/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/output.txt @@ -12,7 +12,8 @@ Resources: 2 created, 0 changed, 0 deleted, 0 unchanged >>> [CLI] schemas update main.test-schema-[UNIQUE_NAME] --new-name test-schema-renamed-[UNIQUE_NAME] { - "full_name": "main.test-schema-renamed-[UNIQUE_NAME]" + "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", + "schema_id": "[UUID][0]" } === Destroy finds the volume in the renamed schema, so prevent_destroy still stops it @@ -25,14 +26,16 @@ Error: resources.volumes.volume1 has lifecycle.prevent_destroy set, but the plan >>> [CLI] volumes read main.test-schema-renamed-[UNIQUE_NAME].myvolume { - "full_name": "main.test-schema-renamed-[UNIQUE_NAME].myvolume" + "full_name": "main.test-schema-renamed-[UNIQUE_NAME].myvolume", + "volume_id": "[UUID][1]" } === With the volume renamed out of band too, it is not found, but destroy still lists it, so prevent_destroy still stops it >>> [CLI] volumes update main.test-schema-renamed-[UNIQUE_NAME].myvolume --new-name myvolume2 { - "full_name": "main.test-schema-renamed-[UNIQUE_NAME].myvolume2" + "full_name": "main.test-schema-renamed-[UNIQUE_NAME].myvolume2", + "volume_id": "[UUID][1]" } >>> musterr [CLI] bundle destroy --auto-approve @@ -43,5 +46,6 @@ Error: resources.volumes.volume1 has lifecycle.prevent_destroy set, but the plan >>> [CLI] volumes read main.test-schema-renamed-[UNIQUE_NAME].myvolume2 { - "full_name": "main.test-schema-renamed-[UNIQUE_NAME].myvolume2" + "full_name": "main.test-schema-renamed-[UNIQUE_NAME].myvolume2", + "volume_id": "[UUID][1]" } diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/script b/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/script index 5770a63a49f..ee03e585849 100644 --- a/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/script +++ b/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/script @@ -17,17 +17,17 @@ trace $CLI bundle deploy print_requests.py //unity-catalog &> LOG.deploy_requests title "Rename the schema out of band. UC moves the volume with it" -trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | jq '{full_name}' +trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields print_requests.py //unity-catalog &> LOG.rename_requests title "Destroy finds the volume in the renamed schema, so prevent_destroy still stops it" trace musterr $CLI bundle destroy --auto-approve trace print_requests.py //unity-catalog -trace $CLI volumes read "$NEW_SCHEMA.myvolume" | jq '{full_name}' +trace $CLI volumes read "$NEW_SCHEMA.myvolume" | volume_fields title "With the volume renamed out of band too, it is not found, but destroy still lists it, so prevent_destroy still stops it" -trace $CLI volumes update "$NEW_SCHEMA.myvolume" --new-name myvolume2 | jq '{full_name}' +trace $CLI volumes update "$NEW_SCHEMA.myvolume" --new-name myvolume2 | volume_fields print_requests.py //unity-catalog &> LOG.rename2_requests trace musterr $CLI bundle destroy --auto-approve trace print_requests.py //unity-catalog -trace $CLI volumes read "$NEW_SCHEMA.myvolume2" | jq '{full_name}' +trace $CLI volumes read "$NEW_SCHEMA.myvolume2" | volume_fields diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/databricks.yml.tmpl b/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/databricks.yml.tmpl new file mode 100644 index 00000000000..f2a772f1291 --- /dev/null +++ b/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/databricks.yml.tmpl @@ -0,0 +1,15 @@ +bundle: + name: test-bundle-$UNIQUE_NAME + +resources: + schemas: + schema1: + catalog_name: main + name: test-schema-$UNIQUE_NAME + volumes: + volume1: + catalog_name: main + schema_name: ${resources.schemas.schema1.name} + name: myvolume + lifecycle: + prevent_destroy: true diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/out.test.toml b/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/out.test.toml new file mode 100644 index 00000000000..0f167effef1 --- /dev/null +++ b/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/out.test.toml @@ -0,0 +1,2 @@ +Cloud = true +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/output.txt b/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/output.txt new file mode 100644 index 00000000000..784fd07007c --- /dev/null +++ b/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/output.txt @@ -0,0 +1,51 @@ + +=== Deploy a schema with a volume in it that must not be destroyed + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Created schemas.schema1 +Created volumes.volume1 +Files: 2 uploaded, 0 deleted +Resources: 2 created, 0 changed, 0 deleted, 0 unchanged + +=== Rename the schema in the config to a mixed-case name. UC stores it in lower case; the state records the volume as the config names it + +>>> update_file.py databricks.yml name: test-schema- name: Test-Schema-Mixed- + +>>> [CLI] bundle deploy --auto-approve +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Updated schemas.schema1 +Files: 2 uploaded, 0 deleted +Resources: 0 created, 1 changed, 0 deleted, 1 unchanged + +>>> [CLI] bundle summary --output json +{ + "schemas": { + "schema1": "main.test-schema-mixed-[UNIQUE_NAME]" + }, + "volumes": { + "volume1": "main.Test-Schema-Mixed-[UNIQUE_NAME].myvolume" + } +} + +=== Rename the schema out of band. UC moves the volume with it + +>>> [CLI] schemas update main.test-schema-mixed-[UNIQUE_NAME] --new-name test-schema-renamed-[UNIQUE_NAME] +{ + "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", + "schema_id": "[UUID][0]" +} + +=== Destroy finds the volume under the schema's new name, so prevent_destroy still stops it + +>>> musterr [CLI] bundle destroy --auto-approve +Error: resources.volumes.volume1 has lifecycle.prevent_destroy set, but the plan calls for this resource to be recreated or destroyed. To avoid this error, disable lifecycle.prevent_destroy for resources.volumes.volume1 + + +>>> print_requests.py //unity-catalog + +>>> [CLI] volumes read main.test-schema-renamed-[UNIQUE_NAME].myvolume +{ + "full_name": "main.test-schema-renamed-[UNIQUE_NAME].myvolume", + "volume_id": "[UUID][1]" +} diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/script b/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/script new file mode 100644 index 00000000000..728d76a760b --- /dev/null +++ b/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/script @@ -0,0 +1,32 @@ +envsubst < databricks.yml.tmpl > databricks.yml + +OLD_SCHEMA="main.test-schema-${UNIQUE_NAME}" +NEW_SCHEMA="main.test-schema-renamed-${UNIQUE_NAME}" + +cleanup() { + errcode $CLI schemas delete "$NEW_SCHEMA" --force > LOG.cleanup 2>&1 + errcode $CLI schemas delete "main.test-schema-mixed-${UNIQUE_NAME}" --force >> LOG.cleanup 2>&1 + + # Consume the destroy's requests so none are left recorded. + print_requests.py //unity-catalog &> LOG.destroy_requests +} +trap cleanup EXIT + +title "Deploy a schema with a volume in it that must not be destroyed" +trace $CLI bundle deploy +print_requests.py //unity-catalog &> LOG.deploy_requests + +title "Rename the schema in the config to a mixed-case name. UC stores it in lower case; the state records the volume as the config names it" +trace update_file.py databricks.yml "name: test-schema-" "name: Test-Schema-Mixed-" +trace $CLI bundle deploy --auto-approve +print_requests.py //unity-catalog &> LOG.mixed_requests +trace $CLI bundle summary --output json | jq '.resources | map_values(map_values(.id))' + +title "Rename the schema out of band. UC moves the volume with it" +trace $CLI schemas update "main.test-schema-mixed-${UNIQUE_NAME}" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields +print_requests.py //unity-catalog &> LOG.rename_requests + +title "Destroy finds the volume under the schema's new name, so prevent_destroy still stops it" +trace musterr $CLI bundle destroy --auto-approve +trace print_requests.py //unity-catalog +trace $CLI volumes read "$NEW_SCHEMA.myvolume" | volume_fields diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/test.toml b/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/test.toml new file mode 100644 index 00000000000..4272a80cf61 --- /dev/null +++ b/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/test.toml @@ -0,0 +1,7 @@ +Cloud = true + +Ignore = [ + ".databricks", + ".gitignore", + "databricks.yml", +] diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/databricks-schema-only.yml.tmpl b/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/databricks-schema-only.yml.tmpl new file mode 100644 index 00000000000..6f97649e503 --- /dev/null +++ b/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/databricks-schema-only.yml.tmpl @@ -0,0 +1,8 @@ +bundle: + name: test-bundle-$UNIQUE_NAME + +resources: + schemas: + schema1: + catalog_name: main + name: Test-Schema-$UNIQUE_NAME diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/databricks.yml.tmpl b/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/databricks.yml.tmpl new file mode 100644 index 00000000000..084b982c3de --- /dev/null +++ b/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/databricks.yml.tmpl @@ -0,0 +1,15 @@ +bundle: + name: test-bundle-$UNIQUE_NAME + +resources: + schemas: + schema1: + catalog_name: main + name: Test-Schema-$UNIQUE_NAME + volumes: + volume1: + catalog_name: main + schema_name: test-schema-$UNIQUE_NAME + name: myvolume + lifecycle: + prevent_destroy: true diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/out.test.toml b/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/out.test.toml new file mode 100644 index 00000000000..0f167effef1 --- /dev/null +++ b/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/out.test.toml @@ -0,0 +1,2 @@ +Cloud = true +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/output.txt b/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/output.txt new file mode 100644 index 00000000000..e413387fd5b --- /dev/null +++ b/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/output.txt @@ -0,0 +1,36 @@ + +=== Deploy a schema, then a volume that names it in lower case, as UC stores it: the volume has no dependency on the schema + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Created schemas.schema1 +Files: 3 uploaded, 0 deleted +Resources: 1 created, 0 changed, 0 deleted, 0 unchanged + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Created volumes.volume1 +Files: 1 uploaded, 0 deleted +Resources: 1 created, 0 changed, 0 deleted, 1 unchanged + +=== Rename the schema out of band. UC moves the volume with it + +>>> [CLI] schemas update main.test-schema-[UNIQUE_NAME] --new-name test-schema-renamed-[UNIQUE_NAME] +{ + "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", + "schema_id": "[UUID][0]" +} + +=== Destroy finds the volume under the schema's new name, so prevent_destroy still stops it + +>>> musterr [CLI] bundle destroy --auto-approve +Error: resources.volumes.volume1 has lifecycle.prevent_destroy set, but the plan calls for this resource to be recreated or destroyed. To avoid this error, disable lifecycle.prevent_destroy for resources.volumes.volume1 + + +>>> print_requests.py //unity-catalog + +>>> [CLI] volumes read main.test-schema-renamed-[UNIQUE_NAME].myvolume +{ + "full_name": "main.test-schema-renamed-[UNIQUE_NAME].myvolume", + "volume_id": "[UUID][1]" +} diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/script b/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/script new file mode 100644 index 00000000000..8aa7d09d46c --- /dev/null +++ b/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/script @@ -0,0 +1,28 @@ + +OLD_SCHEMA="main.test-schema-${UNIQUE_NAME}" +NEW_SCHEMA="main.test-schema-renamed-${UNIQUE_NAME}" + +cleanup() { + errcode $CLI schemas delete "$NEW_SCHEMA" --force > LOG.cleanup 2>&1 + errcode $CLI schemas delete "$OLD_SCHEMA" --force >> LOG.cleanup 2>&1 + + # Consume the destroy's requests so none are left recorded. + print_requests.py //unity-catalog &> LOG.destroy_requests +} +trap cleanup EXIT + +title "Deploy a schema, then a volume that names it in lower case, as UC stores it: the volume has no dependency on the schema" +envsubst < databricks-schema-only.yml.tmpl > databricks.yml +trace $CLI bundle deploy +envsubst < databricks.yml.tmpl > databricks.yml +trace $CLI bundle deploy +print_requests.py //unity-catalog &> LOG.deploy_requests + +title "Rename the schema out of band. UC moves the volume with it" +trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields +print_requests.py //unity-catalog &> LOG.rename_requests + +title "Destroy finds the volume under the schema's new name, so prevent_destroy still stops it" +trace musterr $CLI bundle destroy --auto-approve +trace print_requests.py //unity-catalog +trace $CLI volumes read "$NEW_SCHEMA.myvolume" | volume_fields diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/test.toml b/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/test.toml new file mode 100644 index 00000000000..4272a80cf61 --- /dev/null +++ b/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/test.toml @@ -0,0 +1,7 @@ +Cloud = true + +Ignore = [ + ".databricks", + ".gitignore", + "databricks.yml", +] diff --git a/acceptance/bundle/resources/volumes/removed_from_renamed_schema/databricks-renamed-without-volume.yml.tmpl b/acceptance/bundle/resources/volumes/removed_from_renamed_schema/databricks-renamed-without-volume.yml.tmpl new file mode 100644 index 00000000000..911125f013b --- /dev/null +++ b/acceptance/bundle/resources/volumes/removed_from_renamed_schema/databricks-renamed-without-volume.yml.tmpl @@ -0,0 +1,8 @@ +bundle: + name: test-bundle-$UNIQUE_NAME + +resources: + schemas: + schema1: + catalog_name: main + name: test-schema-renamed-$UNIQUE_NAME diff --git a/acceptance/bundle/resources/volumes/removed_from_renamed_schema/databricks.yml.tmpl b/acceptance/bundle/resources/volumes/removed_from_renamed_schema/databricks.yml.tmpl new file mode 100644 index 00000000000..2234fdf2e39 --- /dev/null +++ b/acceptance/bundle/resources/volumes/removed_from_renamed_schema/databricks.yml.tmpl @@ -0,0 +1,13 @@ +bundle: + name: test-bundle-$UNIQUE_NAME + +resources: + schemas: + schema1: + catalog_name: main + name: test-schema-$UNIQUE_NAME + volumes: + volume1: + catalog_name: main + schema_name: ${resources.schemas.schema1.name} + name: myvolume diff --git a/acceptance/bundle/resources/volumes/removed_from_renamed_schema/out.test.toml b/acceptance/bundle/resources/volumes/removed_from_renamed_schema/out.test.toml new file mode 100644 index 00000000000..0f167effef1 --- /dev/null +++ b/acceptance/bundle/resources/volumes/removed_from_renamed_schema/out.test.toml @@ -0,0 +1,2 @@ +Cloud = true +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/volumes/removed_from_renamed_schema/output.txt b/acceptance/bundle/resources/volumes/removed_from_renamed_schema/output.txt new file mode 100644 index 00000000000..d67a9191ac7 --- /dev/null +++ b/acceptance/bundle/resources/volumes/removed_from_renamed_schema/output.txt @@ -0,0 +1,56 @@ + +=== Deploy a schema with a volume in it, and rename the schema out of band + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Created schemas.schema1 +Created volumes.volume1 +Files: 3 uploaded, 0 deleted +Resources: 2 created, 0 changed, 0 deleted, 0 unchanged + +>>> [CLI] schemas update main.test-schema-[UNIQUE_NAME] --new-name test-schema-renamed-[UNIQUE_NAME] +{ + "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", + "schema_id": "[SCHEMA_ID]" +} + +=== Follow the rename in the config and remove the volume from it: the plan keeps the schema and finds the volume under its new name + +>>> [CLI] bundle plan +delete volumes.volume1 + +Plan: 0 to add, 0 to change, 1 to delete, 1 unchanged + +=== Deploy deletes the volume at its new name + +>>> [CLI] bundle deploy --auto-approve +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... + +This action will result in the deletion or recreation of the following volumes. +For managed volumes, the files stored in the volume are also deleted from your +cloud tenant within 30 days. For external volumes, the metadata about the volume +is removed from the catalog, but the underlying files are not deleted: + delete resources.volumes.volume1 +Deleted volumes.volume1 +Files: 2 uploaded, 0 deleted +Resources: 0 created, 0 changed, 1 deleted, 1 unchanged + +>>> print_requests.py //unity-catalog +{ + "method": "DELETE", + "path": "/api/2.1/unity-catalog/volumes/main.test-schema-renamed-[UNIQUE_NAME].myvolume" +} + +>>> musterr [CLI] volumes read main.test-schema-renamed-[UNIQUE_NAME].myvolume +Error: Volume 'main.test-schema-renamed-[UNIQUE_NAME].myvolume' does not exist. + +>>> [CLI] bundle destroy --auto-approve +The following resources will be deleted: + delete resources.schemas.schema1 + +This action will result in the deletion of the following UC schemas. Any underlying data may be lost: + delete resources.schemas.schema1 + +All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default + +Destroy: 1 deleted diff --git a/acceptance/bundle/resources/volumes/removed_from_renamed_schema/script b/acceptance/bundle/resources/volumes/removed_from_renamed_schema/script new file mode 100644 index 00000000000..6e768e643cb --- /dev/null +++ b/acceptance/bundle/resources/volumes/removed_from_renamed_schema/script @@ -0,0 +1,28 @@ +envsubst < databricks.yml.tmpl > databricks.yml + +OLD_SCHEMA="main.test-schema-${UNIQUE_NAME}" +NEW_SCHEMA="main.test-schema-renamed-${UNIQUE_NAME}" + +cleanup() { + trace $CLI bundle destroy --auto-approve + + # Consume the destroy's requests so none are left recorded. + print_requests.py //unity-catalog &> LOG.destroy_requests +} +trap cleanup EXIT + +title "Deploy a schema with a volume in it, and rename the schema out of band" +trace $CLI bundle deploy +trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields +print_requests.py //unity-catalog &> LOG.deploy_requests +add_repl "$($CLI schemas get "$NEW_SCHEMA" | jq -r .schema_id)" SCHEMA_ID +add_repl "$($CLI volumes read "$NEW_SCHEMA.myvolume" | jq -r .volume_id)" VOLUME_ID + +title "Follow the rename in the config and remove the volume from it: the plan keeps the schema and finds the volume under its new name" +envsubst < databricks-renamed-without-volume.yml.tmpl > databricks.yml +trace $CLI bundle plan + +title "Deploy deletes the volume at its new name" +trace $CLI bundle deploy --auto-approve +trace print_requests.py //unity-catalog +trace musterr $CLI volumes read "$NEW_SCHEMA.myvolume" diff --git a/acceptance/bundle/resources/volumes/removed_from_renamed_schema/test.toml b/acceptance/bundle/resources/volumes/removed_from_renamed_schema/test.toml new file mode 100644 index 00000000000..4272a80cf61 --- /dev/null +++ b/acceptance/bundle/resources/volumes/removed_from_renamed_schema/test.toml @@ -0,0 +1,7 @@ +Cloud = true + +Ignore = [ + ".databricks", + ".gitignore", + "databricks.yml", +] diff --git a/acceptance/bundle/resources/volumes/script.prepare b/acceptance/bundle/resources/volumes/script.prepare new file mode 100644 index 00000000000..6f1a8f182ad --- /dev/null +++ b/acceptance/bundle/resources/volumes/script.prepare @@ -0,0 +1,11 @@ +# volume_fields is the allow-list of volume fields the acceptance tests assert on. Anything the +# backend returns outside this list is dropped, so a new field in the API response does not break +# the golden. +volume_fields() { + jq '{full_name, volume_id} | with_entries(select(.value != null))' +} + +# schema_fields is the same allow-list for the schemas these tests create and rename. +schema_fields() { + jq '{full_name, schema_id} | with_entries(select(.value != null))' +} diff --git a/bundle/deployplan/action.go b/bundle/deployplan/action.go index 698c11e56b1..27cda9b80e7 100644 --- a/bundle/deployplan/action.go +++ b/bundle/deployplan/action.go @@ -15,6 +15,8 @@ type Action struct { // StateOnly mirrors PlanEntry.StateOnly: applying the action only changes the state // (a delete of a resource without DoDelete, or a re-key). StateOnly bool + // WithParent mirrors PlanEntry.WithParent: the resource goes with its parent's delete. + WithParent bool } func (a Action) String() string { @@ -23,9 +25,9 @@ func (a Action) String() string { // IsStateOnly reports whether applying this action makes no backend call and only changes the // state (see PlanEntry.IsStateOnly). Such actions are omitted from human output and need no -// approval. +// approval. An action with WithParent is not: its parent's delete removes the resource. func (a Action) IsStateOnly() bool { - return a.Gone || a.StateOnly + return (a.Gone || a.StateOnly) && !a.WithParent } func (a Action) IsChildResource() bool { diff --git a/bundle/deployplan/plan.go b/bundle/deployplan/plan.go index f63522b0a2e..eb52ba62945 100644 --- a/bundle/deployplan/plan.go +++ b/bundle/deployplan/plan.go @@ -125,18 +125,27 @@ func LoadPlanFromFile(path string) (*Plan, error) { type PlanEntry struct { // ID is set when the resource is at another ID than the one in state: a rename moved it there - // (see findMoved, and findByInternalID for a Delete), or a parent renamed in this plan moves it - // before it is recreated (see addParentMoves). Applying the entry addresses the resource at ID. + // (see findMoved, and findByInternalID and findInMovedParents for a Delete), or a parent renamed + // in this plan moves it before it is recreated (see addParentMoves). Applying the entry + // addresses the resource at ID. ID string `json:"id,omitempty"` DependsOn []DependsOnEntry `json:"depends_on,omitempty"` Action ActionType `json:"action,omitempty"` // Gone is set on Delete entries when the resource no longer exists remotely: planning // confirmed it with a read (for a resource with an internal ID: found neither at its ID, where - // another object may be, nor by its internal ID), or the state entry has an empty ID (left by a recreate in - // an older CLI whose create step failed after the delete). Applying such an entry only - // removes it from the state, without calling the delete API, and approval prompts do - // not list it as a deletion. + // another object may be, nor by its internal ID, nor under a renamed parent; see + // findInMovedParents), or the state entry has an empty ID (left by a recreate in an older CLI + // whose create step failed after the delete). Applying such an entry only removes it from the + // state, without calling the delete API, and approval prompts do not list it as a deletion + // unless WithParent is set. Gone bool `json:"gone,omitempty"` + // WithParent is set on a Gone Delete entry whose resource is inside a parent this plan deletes + // at the new ID a rename out of band gave it (a volume in a renamed schema), but could not be + // verified there by its internal ID. Applying it sends no delete for it, which could hit another + // object under the guessed name, and only removes it from the state; the parent's delete + // removes it with everything else in the parent. So approval prompts, lifecycle.prevent_destroy + // and the counts treat it as a deletion. It holds the parent's resource key. + WithParent string `json:"with_parent,omitempty"` // StateOnly marks an entry whose apply has no backend effect and only changes the state: // - a Delete of a resource that implements no DoDelete. Like Gone, applying it only // removes it from the state and it is excluded from destructive-action prompts, @@ -155,10 +164,17 @@ type PlanEntry struct { Changes Changes `json:"changes,omitempty"` } -// IsStateOnly reports whether applying this entry makes no backend call and only changes the +// IsStateOnly reports whether applying this entry has no backend effect and only changes the // state: a delete of a resource already gone remotely (Gone) or without a delete operation, or a -// re-key (StateOnly). Such entries are omitted from human output and need no approval. +// re-key (StateOnly). Such entries are omitted from human output and need no approval. A Gone +// entry with WithParent is not: its parent's delete removes it. func (e *PlanEntry) IsStateOnly() bool { + return (e.Gone || e.StateOnly) && e.WithParent == "" +} + +// SendsNoDelete reports whether applying this Delete entry only removes it from the state, without +// calling the delete API: it is state-only, or its parent's delete removes it (WithParent). +func (e *PlanEntry) SendsNoDelete() bool { return e.Gone || e.StateOnly } @@ -274,6 +290,7 @@ func (p *Plan) GetActions() []Action { ActionType: entry.Action, Gone: entry.Gone, StateOnly: entry.StateOnly, + WithParent: entry.WithParent != "", }) } diff --git a/bundle/deployplan/plan_test.go b/bundle/deployplan/plan_test.go index 1d93583514a..2d1bfc9561a 100644 --- a/bundle/deployplan/plan_test.go +++ b/bundle/deployplan/plan_test.go @@ -113,3 +113,21 @@ func TestHasOnlySingleReason(t *testing.T) { }) } } + +func TestWithParentIsADeleteThatSendsNoCall(t *testing.T) { + gone := &deployplan.PlanEntry{Action: deployplan.Delete, Gone: true} + withParent := &deployplan.PlanEntry{Action: deployplan.Delete, Gone: true, WithParent: "resources.schemas.s"} + + // A resource already gone is hidden and needs no call. + assert.True(t, gone.IsStateOnly()) + assert.True(t, gone.SendsNoDelete()) + + // One that goes with its parent's delete is shown and approved as a deletion, but no delete is + // sent for it. + assert.False(t, withParent.IsStateOnly()) + assert.True(t, withParent.SendsNoDelete()) + + plan := &deployplan.Plan{Plan: map[string]*deployplan.PlanEntry{"resources.volumes.v": withParent}} + actions := plan.GetActions() + assert.False(t, actions[0].IsStateOnly()) +} diff --git a/bundle/direct/apply.go b/bundle/direct/apply.go index ed7a418ccab..8d0ba0cefac 100644 --- a/bundle/direct/apply.go +++ b/bundle/direct/apply.go @@ -25,7 +25,7 @@ func (d *DeploymentUnit) withResourceKey(ctx context.Context) context.Context { } // Destroy deletes the resource at its recorded ID, or at movedID when the plan found it there (see -// findByInternalID). +// findByInternalID and findInMovedParents). func (d *DeploymentUnit) Destroy(ctx context.Context, db *dstate.DeploymentState, movedID string) error { ctx = log.WithPrefix(ctx, "destroying "+d.ResourceKey) ctx = d.withResourceKey(ctx) diff --git a/bundle/direct/bundle_apply.go b/bundle/direct/bundle_apply.go index 01cf08e3ebf..dad1bf6fabb 100644 --- a/bundle/direct/bundle_apply.go +++ b/bundle/direct/bundle_apply.go @@ -115,10 +115,11 @@ func (b *DeploymentBundle) Apply(ctx context.Context, client *databricks.Workspa } if action == deployplan.Delete { - if entry.IsStateOnly() { - // The resource is already deleted remotely (Gone) or has no delete - // operation (StateOnly); either way only remove it from the state, - // without calling the delete API. + if entry.SendsNoDelete() { + // The resource is already deleted remotely (Gone), has no delete + // operation (StateOnly), or goes with its parent's delete (WithParent); + // in each case only remove it from the state, without calling the + // delete API. err = b.StateDB.DeleteState(ctx, resourceKey, false) } else { err = d.Destroy(ctx, &b.StateDB, entry.ID) diff --git a/bundle/direct/bundle_plan.go b/bundle/direct/bundle_plan.go index 98b583b2b3f..4555e556ef8 100644 --- a/bundle/direct/bundle_plan.go +++ b/bundle/direct/bundle_plan.go @@ -17,6 +17,7 @@ import ( "github.com/databricks/cli/bundle/direct/dresources" "github.com/databricks/cli/bundle/direct/dstate" "github.com/databricks/cli/bundle/terraform_dabs_map" + "github.com/databricks/cli/libs/diag" "github.com/databricks/cli/libs/dyn" "github.com/databricks/cli/libs/dyn/dynvar" "github.com/databricks/cli/libs/log" @@ -250,10 +251,12 @@ func (b *DeploymentBundle) CalculatePlan(ctx context.Context, client *databricks if apierr.IsMissing(err) { missing = true remoteState = nil - } else { + } else if !adapter.HasInternalID() { log.Warnf(ctx, "reading %s id=%q: %s", resourceKey, id, err) // This is not an error during deletion, so don't return false here } + // For a resource with an internal ID, findDeleteTarget decides: the read is how + // the plan tells the bundle's resource from another one under its name. } else if adapter.IsGone(remoteState) { // The resource is in a transient terminal-teardown state (e.g. an app in // DELETING) that a GET still returns but a second delete would reject. @@ -386,7 +389,8 @@ func (b *DeploymentBundle) CalculatePlan(ctx context.Context, client *databricks } } if otherID != "" && remoteState == nil { - warnOtherResource(ctx, adapter, dbentry.ID, savedState, otherID) + logdiag.LogDiag(ctx, diag.Warningf("%s: %s. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again.", + resourceKey, otherObjectMessage(adapter, dbentry.ID, savedState, otherID))[0]) } // We have a choice whether to include remoteState or remoteStateComparable from below. @@ -487,6 +491,7 @@ func (b *DeploymentBundle) CalculatePlan(ctx context.Context, client *databricks if err := b.findInMovedParents(ctx, plan); err != nil { return nil, err } + b.warnOtherObjects(ctx, plan) for _, entry := range plan.Plan { if entry.Action == deployplan.Skip { @@ -907,46 +912,117 @@ func isParentIDField(adapter *dresources.Adapter, path *structpath.PathNode) boo } // findInMovedParents handles a Delete of a resource not found at its recorded ID (Gone) while its -// parent, as recorded in its state's depends_on, was found at another ID in this plan: renamed out of -// band (see findMoved and findByInternalID). The rename moved the resource with the parent, so it is -// looked for under the parent's new ID and, if found there with its internal ID, deleted as itself, -// before the parent. When the parent is deleted too, the resource is deleted there even if it is not -// found: it is in the parent, whose delete would take it along unlisted, past its own -// lifecycle.prevent_destroy. +// parent was found at another ID in this plan: renamed out of band (see findMoved and +// findByInternalID). The rename moved the resource with the parent, so it is looked for under the +// parent's new ID, and deleted there as itself if it has its recorded internal ID. When it cannot be +// verified there (it was deleted or renamed too, another object holds the name, or it has no internal +// ID) and the parent is deleted, it is marked WithParent: no delete is sent for it, but approval and +// lifecycle.prevent_destroy see that the parent's delete takes it along. func (b *DeploymentBundle) findInMovedParents(ctx context.Context, plan *deployplan.Plan) error { - for _, resourceKey := range slices.Sorted(maps.Keys(plan.Plan)) { + parents, err := b.movedParents(plan) + if err != nil || len(parents) == 0 { + return err + } + + // A parent's own Delete may be found here too (a schema in a renamed catalog), so the shallower + // resources go first. + keys := slices.Collect(maps.Keys(plan.Plan)) + slices.SortFunc(keys, func(x, y string) int { + return cmp.Or( + cmp.Compare(strings.Count(b.StateDB.GetResourceID(x), "."), strings.Count(b.StateDB.GetResourceID(y), ".")), + cmp.Compare(x, y), + ) + }) + + for _, resourceKey := range keys { entry := plan.Plan[resourceKey] if entry.Action != deployplan.Delete || !entry.Gone { continue } + adapter, err := b.getAdapterForKey(resourceKey) + if err != nil { + return err + } dbentry, ok := b.StateDB.GetResourceEntry(resourceKey) - if !ok { + if !ok || !adapter.HasIDFromState() { continue } - id, parentDeleted := b.idUnderMovedParent(plan, dbentry) - if id == "" { + parent, id := underMovedParent(parents, dbentry.ID) + if parent == nil { continue } - adapter, err := b.getAdapterForKey(resourceKey) - if err != nil { - return err - } remote, err := b.readIfOwn(ctx, adapter, dbentry, id) if err != nil { return fmt.Errorf("%s: %w", resourceKey, err) } - if remote == nil && !parentDeleted { - continue + switch { + case remote != nil: + entry.ID = id + entry.RemoteState = remote + entry.Gone = false + if parent.deleted && !slices.ContainsFunc(entry.DependsOn, func(d deployplan.DependsOnEntry) bool { return d.Node == parent.key }) { + // Deleted before its parent, also when the config named the parent literally. + entry.DependsOn = append(entry.DependsOn, deployplan.DependsOnEntry{Node: parent.key}) + } + parents = append(parents, &movedParent{key: resourceKey, recordedID: dbentry.ID, id: id, deleted: true}) + case parent.deleted: + entry.WithParent = parent.key } - entry.ID = id - entry.RemoteState = remote - entry.Gone = false } return nil } +// movedParent is a UC resource this plan found at another ID than its recorded one. +type movedParent struct { + key string + recordedID string + id string + // deleted reports whether the plan deletes it there, taking everything in it along. + deleted bool +} + +// movedParents returns the UC resources (their ID is a full name, see IDFromState) the plan found at +// another ID than their recorded one: deleted there (findByInternalID), or re-keyed (findMoved). +func (b *DeploymentBundle) movedParents(plan *deployplan.Plan) ([]*movedParent, error) { + var parents []*movedParent + for _, key := range slices.Sorted(maps.Keys(plan.Plan)) { + entry := plan.Plan[key] + if entry.ID == "" || entry.Action == deployplan.Recreate || (entry.Action == deployplan.Delete && entry.Gone) { + continue + } + adapter, err := b.getAdapterForKey(key) + if err != nil { + return nil, err + } + recordedID := b.StateDB.GetResourceID(key) + if !adapter.HasIDFromState() || recordedID == "" || strings.EqualFold(recordedID, entry.ID) { + continue + } + parents = append(parents, &movedParent{key: key, recordedID: recordedID, id: entry.ID, deleted: entry.Action == deployplan.Delete}) + } + return parents, nil +} + +// underMovedParent returns the parent whose recorded ID is the longest prefix of id (main.s for +// main.s.v), and id under the parent's new ID (main.t.v); nil if there is none. UC full names are +// case-insensitive, and the bundle can record a child in config case and its parent in UC's lower case. +func underMovedParent(parents []*movedParent, id string) (*movedParent, string) { + var best *movedParent + for _, parent := range parents { + prefix := parent.recordedID + "." + if len(id) > len(prefix) && strings.EqualFold(id[:len(prefix)], prefix) && (best == nil || len(parent.recordedID) > len(best.recordedID)) { + best = parent + } + } + if best == nil { + return nil, "" + } + return best, best.id + "." + id[len(best.recordedID)+1:] +} + // readIfOwn reads the resource recorded in dbentry at id and returns it if it has its recorded -// internal ID; nil if it is not there, is another one, or there is no internal ID to tell. +// internal ID; nil if it is not there, is another object, or has no internal ID to tell. A read that +// fails other than with a missing resource or a permission error fails the plan. func (b *DeploymentBundle) readIfOwn(ctx context.Context, adapter *dresources.Adapter, dbentry dstate.ResourceEntry, id string) (any, error) { if !adapter.HasInternalID() || len(dbentry.State) == 0 { return nil, nil @@ -963,7 +1039,7 @@ func (b *DeploymentBundle) readIfOwn(ctx context.Context, adapter *dresources.Ad return adapter.DoRead(ctx, id) }) if err != nil { - return nil, ignoreMissingOrForbidden(ctx, err, fmt.Sprintf("reading at id=%q under its renamed parent", id)) + return nil, ignoreMissingOrForbidden(ctx, err, fmt.Sprintf("reading id=%q under its renamed parent", id)) } ok, err := hasInternalIDOf(adapter, remote, savedInternalID) if err != nil || !ok { @@ -972,27 +1048,6 @@ func (b *DeploymentBundle) readIfOwn(ctx context.Context, adapter *dresources.Ad return remote, nil } -// idUnderMovedParent returns the ID of the resource recorded in dbentry under the new ID of a parent -// (from its depends_on) found at another ID in this plan, "" if there is none, and whether that parent -// is deleted. A UC full name starts with its parent's: main.s.v is in main.s, so in main.t after the -// schema's rename. -func (b *DeploymentBundle) idUnderMovedParent(plan *deployplan.Plan, dbentry dstate.ResourceEntry) (string, bool) { - for _, dep := range dbentry.DependsOn { - parent := plan.Plan[dep.Node] - if parent == nil || parent.ID == "" { - continue - } - parentID := b.StateDB.GetResourceID(dep.Node) - if parentID == "" || parentID == parent.ID { - continue - } - if rest, ok := strings.CutPrefix(dbentry.ID, parentID+"."); ok { - return parent.ID + "." + rest, parent.Action == deployplan.Delete && !parent.Gone - } - } - return "", false -} - // recordRefTypes records, for each pure-reference field of resourceKey, the type of the resource it // references, before resolveReferences drops the references (see addParentMoves). func (b *DeploymentBundle) recordRefTypes(resourceKey string) error { @@ -1107,24 +1162,42 @@ func otherInternalID(adapter *dresources.Adapter, savedState, remote any) (strin return found, nil } -// warnOtherResource tells the user that what is at the recorded ID is not the bundle's and is left -// alone, when the bundle's own is not found elsewhere either. A resource deleted and created again -// out of band looks the same, hence the way to take it over. -func warnOtherResource(ctx context.Context, adapter *dresources.Adapter, id string, savedState any, otherID string) { +// otherObjectMessage says that what is at the recorded ID id is not the bundle's: the plan leaves it +// alone. It is told when the bundle's own is not found elsewhere either; a resource deleted and +// created again out of band looks the same, hence the advice the callers add. +func otherObjectMessage(adapter *dresources.Adapter, id string, savedState any, otherID string) string { saved, _ := adapter.InternalID(savedState) - log.Warnf(ctx, "id=%q holds another object: its %s is %q, not %q as recorded, so this bundle leaves it alone. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again.", + return fmt.Sprintf("id=%q holds another object: its %s is %q, not %q as recorded, so this bundle leaves it in place", id, adapter.ResourceConfig().InternalIDField, otherID, saved) } +// warnOtherObjects warns about each Delete that leaves another object in place at its recorded ID +// (see findDeleteTarget), after findInMovedParents had its chance to find the bundle's own. +func (b *DeploymentBundle) warnOtherObjects(ctx context.Context, plan *deployplan.Plan) { + for _, resourceKey := range slices.Sorted(maps.Keys(plan.Plan)) { + message, ok := b.otherObjects.Load(resourceKey) + if !ok || plan.Plan[resourceKey].ID != "" { + continue + } + logdiag.LogDiag(ctx, diag.Warningf("%s: %s, and drops %s from the deployment state. If it is the bundle's (deleted and created again outside the bundle), bind it with 'bundle deployment bind' before destroying, or delete it outside the bundle.", + resourceKey, message, resourceKey)[0]) + } +} + // findDeleteTarget returns where to delete a resource with an internal ID, given what a read at its // recorded ID returned: there if it is ours, else where findByInternalID finds it. An empty ID means -// the recorded one; a nil remote means it is nowhere. Someone else's resource under the name ours had -// is never the target, and neither is a resource whose identity could not be checked: a read that -// failed for any other reason than a missing resource or a permission error fails the plan. +// the recorded one. A nil remote means it is nowhere, except after a read that failed with a +// permission error (readErr), when the caller leaves the delete to the recorded ID, as before. +// Someone else's resource under the name ours had is never the target, and neither is a resource +// whose identity could not be checked: a read that failed for any other reason than a missing +// resource or a permission error fails the plan. func (b *DeploymentBundle) findDeleteTarget(ctx context.Context, resourceKey string, adapter *dresources.Adapter, id string, remote any, readErr error) (string, any, error) { dbentry, _ := b.StateDB.GetResourceEntry(resourceKey) if len(dbentry.State) == 0 { // Only the ID is recorded: there is no internal ID to check. + if readErr != nil { + log.Warnf(ctx, "reading %s id=%q: %s", resourceKey, id, readErr) + } return "", remote, nil } savedState, err := parseState(adapter.StateType(), dbentry.State) @@ -1132,11 +1205,15 @@ func (b *DeploymentBundle) findDeleteTarget(ctx context.Context, resourceKey str return "", nil, fmt.Errorf("interpreting state: %w", err) } if saved, err := adapter.InternalID(savedState); err != nil || saved == "" { + if readErr != nil { + log.Warnf(ctx, "reading %s id=%q: %s", resourceKey, id, readErr) + } return "", remote, err } if readErr != nil { if errors.Is(readErr, apierr.ErrPermissionDenied) { // Deleted as before: a resource the user cannot read is left to the delete call. + log.Warnf(ctx, "reading %s id=%q: %s", resourceKey, id, readErr) return "", nil, nil } return "", nil, fmt.Errorf("cannot check that id=%q is still this bundle's: %w", id, readErr) @@ -1150,7 +1227,8 @@ func (b *DeploymentBundle) findDeleteTarget(ctx context.Context, resourceKey str } movedID, moved, err := findByInternalID(ctx, adapter, savedState) if err == nil && moved == nil && otherID != "" { - warnOtherResource(ctx, adapter, id, savedState, otherID) + // Told after findInMovedParents, which may still find it (see warnOtherObjects). + b.otherObjects.Store(resourceKey, otherObjectMessage(adapter, id, savedState, otherID)) } return movedID, moved, err } @@ -1173,13 +1251,13 @@ func findByInternalID(ctx context.Context, adapter *dresources.Adapter, savedSta return adapter.DoFindByInternalID(ctx, savedState) }) if err != nil || id == "" { - return "", nil, ignoreMissingOrForbidden(ctx, err, fmt.Sprintf("looking for a renamed resource by its internal id %q", savedInternalID)) + return "", nil, ignoreMissingOrForbidden(ctx, err, fmt.Sprintf("looking for %s %q after a rename outside the bundle", adapter.ResourceConfig().InternalIDField, savedInternalID)) } remote, err := retry.With(ctx, retry.IsTransientRead, func() (any, error) { return adapter.DoRead(ctx, id) }) if err != nil { - return "", nil, ignoreMissingOrForbidden(ctx, err, fmt.Sprintf("reading a renamed resource at id=%q", id)) + return "", nil, ignoreMissingOrForbidden(ctx, err, fmt.Sprintf("reading id=%q, where %s %q was found", id, adapter.ResourceConfig().InternalIDField, savedInternalID)) } if ok, err := hasInternalIDOf(adapter, remote, savedInternalID); err != nil || !ok { return "", nil, err diff --git a/bundle/direct/bundle_plan_test.go b/bundle/direct/bundle_plan_test.go index 8b5b69d2b2a..5847a82803a 100644 --- a/bundle/direct/bundle_plan_test.go +++ b/bundle/direct/bundle_plan_test.go @@ -1068,28 +1068,30 @@ func TestFindDeleteTargetUnverifiedRead(t *testing.T) { assert.Nil(t, got) } -func TestIDUnderMovedParent(t *testing.T) { - const schemaKey = "resources.schemas.schema1" - dep := deployplan.DependsOnEntry{Node: schemaKey, Label: "${resources.schemas.schema1.name}"} +func TestUnderMovedParent(t *testing.T) { + catalog := &movedParent{key: "resources.catalogs.c", recordedID: "main", id: "archive"} + schema := &movedParent{key: "resources.schemas.s", recordedID: "main.s", id: "main.t", deleted: true} + sameAsCatalog := &movedParent{key: "resources.schemas.main", recordedID: "main.main", id: "main.moved", deleted: true} for name, tt := range map[string]struct { - schemaID, foundID, volumeID, want string + parents []*movedParent + id, wantID string + want *movedParent }{ - "moved": {schemaID: "main.s", foundID: "main.t", volumeID: "main.s.v", want: "main.t.v"}, - "schema named as catalog": {schemaID: "main.main", foundID: "main.moved", volumeID: "main.main.v", want: "main.moved.v"}, - "parent not moved": {schemaID: "main.s", foundID: "", volumeID: "main.s.v", want: ""}, - "not under the parent": {schemaID: "main.s", foundID: "main.t", volumeID: "main.other.v", want: ""}, + "moved": {parents: []*movedParent{schema}, id: "main.s.v", wantID: "main.t.v", want: schema}, + "schema named like its catalog": {parents: []*movedParent{sameAsCatalog}, id: "main.main.v", wantID: "main.moved.v", want: sameAsCatalog}, + // The bundle records a child in config case and its renamed parent in UC's lower case. + "recorded in another case": {parents: []*movedParent{schema}, id: "main.S.v", wantID: "main.t.v", want: schema}, + "longest prefix": {parents: []*movedParent{catalog, schema}, id: "main.s.v", wantID: "main.t.v", want: schema}, + "only the catalog moved": {parents: []*movedParent{catalog}, id: "main.s.v", wantID: "archive.s.v", want: catalog}, + "not under the parent": {parents: []*movedParent{schema}, id: "main.sx.v", wantID: "", want: nil}, + "the parent itself": {parents: []*movedParent{schema}, id: "main.s", wantID: "", want: nil}, + "no parents": {parents: nil, id: "main.s.v", wantID: "", want: nil}, } { t.Run(name, func(t *testing.T) { - plan := deployplan.NewPlanDirect() - plan.Plan[schemaKey] = &deployplan.PlanEntry{Action: deployplan.Delete, ID: tt.foundID} - b := &DeploymentBundle{} - b.StateDB.OpenWithData("state.json", dstate.Database{State: map[string]dstate.ResourceEntry{ - schemaKey: {ID: tt.schemaID}, - }}) - got, parentDeleted := b.idUnderMovedParent(plan, dstate.ResourceEntry{ID: tt.volumeID, DependsOn: []deployplan.DependsOnEntry{dep}}) - assert.Equal(t, tt.want, got) - assert.Equal(t, tt.want != "", parentDeleted) + got, gotID := underMovedParent(tt.parents, tt.id) + assert.Same(t, tt.want, got) + assert.Equal(t, tt.wantID, gotID) }) } } diff --git a/bundle/direct/dresources/README.md b/bundle/direct/dresources/README.md index bd0497a9486..56d3f197bb1 100644 --- a/bundle/direct/dresources/README.md +++ b/bundle/direct/dresources/README.md @@ -62,7 +62,7 @@ If the ID is built from the resource's own fields (a UC full name), implement `I If the backend also has its own ID that a rename keeps (a volume's `volume_id`), add it to the state type, copy it in `RemapState`, and name it in the config as `internal_id_field`. The framework records it on every save and skips its changes in the plan. When nothing is at the recorded ID, the plan reads at the ID the config gives the resource and takes what it finds only if the internal ID matches. This covers a rename done out of band, or by a deploy that stopped before re-keying the resource. -An object at the recorded ID with a different internal ID is someone else's (ours was renamed away and another created under its name): the plan treats ours as not there, so it never updates or deletes the other object. A resource to be deleted (by destroy, or removed from config) has no config to give its new name, so to delete one renamed out of band the resource needs `DoFindByInternalID`: it searches the parent (lists the schemas in the catalog, the volumes in the schema) for the recorded internal ID. Without it, the delete only drops the state entry of a resource not found at its recorded ID. A child whose parent the plan found at a new ID (a volume in a renamed schema) is looked for under the parent's new ID; when the parent is deleted too, the child is deleted there as itself, so its `prevent_destroy` applies. A parent outside the bundle that was renamed is not followed. +An object at the recorded ID with a different internal ID is someone else's (ours was renamed away and another created under its name): the plan treats ours as not there, so it never updates or deletes the other object. A resource to be deleted (by destroy, or removed from config) has no config to give its new name, so to delete one renamed out of band the resource needs `DoFindByInternalID`: it searches the parent (lists the schemas in the catalog, the volumes in the schema) for the recorded internal ID. Without it, the delete only drops the state entry of a resource not found at its recorded ID. A child whose parent the plan found at a new ID (a volume in a renamed schema, matched by full-name prefix) is looked for under the parent's new ID and deleted there as itself if its internal ID matches. If it cannot be verified there and the parent is deleted, no delete is sent for it, but the plan lists it as going with the parent (`with_parent`), so approval and its `prevent_destroy` still apply. A parent outside the bundle that was renamed is not followed. ## Empty states: IsEmptyState diff --git a/bundle/direct/dresources/volume.go b/bundle/direct/dresources/volume.go index a31160676b3..5955de8c8aa 100644 --- a/bundle/direct/dresources/volume.go +++ b/bundle/direct/dresources/volume.go @@ -166,8 +166,8 @@ func (*ResourceVolume) IDFromState(state *VolumeState) string { } // DoFindByInternalID lists the volumes in the schema for the one with the recorded volume_id. A -// volume whose schema was renamed is not found; when the schema is in the bundle too, deleting it -// deletes the volume. +// volume whose schema was renamed is not found here; when the schema is in the bundle, the plan +// looks for it under the schema's new name (findInMovedParents). func (r *ResourceVolume) DoFindByInternalID(ctx context.Context, state *VolumeState) (string, error) { iter := r.client.Volumes.List(ctx, catalog.ListVolumesRequest{ CatalogName: state.CatalogName, diff --git a/bundle/direct/pkg.go b/bundle/direct/pkg.go index 84990d0883e..f9b00cc730c 100644 --- a/bundle/direct/pkg.go +++ b/bundle/direct/pkg.go @@ -55,6 +55,10 @@ type DeploymentBundle struct { // fields references, recorded before resolveReferences drops them (see addParentMoves). refTypes sync.Map + // otherObjects maps the key of a resource to be deleted to the message that another object holds + // its recorded ID, told once the plan is complete (see warnOtherObjects). + otherObjects sync.Map + // DeployDurations maps a resource key to the time.Duration its create, update or // recreate took. Only successfully deployed resources are recorded; Apply runs // nodes in parallel, hence the sync.Map. From 718edebb56d6ffab9c28a2c08c4dce16256357a4 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Thu, 26 Nov 2026 17:00:00 +0000 Subject: [PATCH 08/22] direct: order a with_parent child after its parent; follow moved parents for any child Address the review round on #8056: - a with_parent child is applied after its parent's delete and selected with it, so its state is dropped only once that delete succeeded; - a child of any type under a moved parent is looked for there, and one without an internal ID that is not there stays Gone; - a child at its old path under a moved parent is never deleted there; - a 403 at the recorded ID searches by internal ID first; - the completed plan is checked for cycles again; doc fixes. Co-authored-by: Isaac --- .../databricks.yml.tmpl | 8 ++ .../out.test.toml | 2 + .../output.txt | 34 +++++ .../destroy_forbidden_at_recorded_name/script | 14 ++ .../test.toml | 13 ++ .../output.txt | 25 +++- .../script | 10 +- .../databricks-empty.yml.tmpl | 2 + .../databricks-schema-only.yml.tmpl | 8 ++ .../select_renamed_schema/databricks.yml.tmpl | 13 ++ .../select_renamed_schema/out.test.toml | 2 + .../volumes/select_renamed_schema/output.txt | 61 +++++++++ .../volumes/select_renamed_schema/script | 29 ++++ .../volumes/select_renamed_schema/test.toml | 7 + .../databricks.yml.tmpl | 13 ++ .../with_parent_delete_fails/out.test.toml | 2 + .../with_parent_delete_fails/output.txt | 68 ++++++++++ .../volumes/with_parent_delete_fails/script | 15 +++ .../with_parent_delete_fails/test.toml | 12 ++ bundle/deployplan/plan.go | 12 +- bundle/deployplan/plan_filter_test.go | 15 +++ bundle/direct/bundle_plan.go | 127 ++++++++++++------ bundle/direct/dresources/README.md | 2 +- bundle/direct/dresources/adapter.go | 4 +- bundle/direct/graph.go | 6 +- bundle/direct/graph_test.go | 52 +++++++ 26 files changed, 507 insertions(+), 49 deletions(-) create mode 100644 acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/databricks.yml.tmpl create mode 100644 acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/out.test.toml create mode 100644 acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/output.txt create mode 100644 acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/script create mode 100644 acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/test.toml create mode 100644 acceptance/bundle/resources/volumes/select_renamed_schema/databricks-empty.yml.tmpl create mode 100644 acceptance/bundle/resources/volumes/select_renamed_schema/databricks-schema-only.yml.tmpl create mode 100644 acceptance/bundle/resources/volumes/select_renamed_schema/databricks.yml.tmpl create mode 100644 acceptance/bundle/resources/volumes/select_renamed_schema/out.test.toml create mode 100644 acceptance/bundle/resources/volumes/select_renamed_schema/output.txt create mode 100644 acceptance/bundle/resources/volumes/select_renamed_schema/script create mode 100644 acceptance/bundle/resources/volumes/select_renamed_schema/test.toml create mode 100644 acceptance/bundle/resources/volumes/with_parent_delete_fails/databricks.yml.tmpl create mode 100644 acceptance/bundle/resources/volumes/with_parent_delete_fails/out.test.toml create mode 100644 acceptance/bundle/resources/volumes/with_parent_delete_fails/output.txt create mode 100644 acceptance/bundle/resources/volumes/with_parent_delete_fails/script create mode 100644 acceptance/bundle/resources/volumes/with_parent_delete_fails/test.toml create mode 100644 bundle/direct/graph_test.go diff --git a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/databricks.yml.tmpl b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/databricks.yml.tmpl new file mode 100644 index 00000000000..925877124dd --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/databricks.yml.tmpl @@ -0,0 +1,8 @@ +bundle: + name: test-bundle-$UNIQUE_NAME + +resources: + schemas: + schema1: + catalog_name: main + name: myschema diff --git a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/out.test.toml b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/out.test.toml new file mode 100644 index 00000000000..a927a5fbc06 --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/output.txt b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/output.txt new file mode 100644 index 00000000000..53dc78b9427 --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/output.txt @@ -0,0 +1,34 @@ + +=== Deploy a schema and rename it out of band; the recorded name is not readable + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Created schemas.schema1 +Files: 2 uploaded, 0 deleted +Resources: 1 created, 0 changed, 0 deleted, 0 unchanged + +=== Destroy finds the renamed schema by its schema_id and deletes it there + +>>> [CLI] bundle destroy --auto-approve +Warn: planning resources.schemas.schema1: reading resources.schemas.schema1 id="main.myschema": User does not have USE SCHEMA on the schema +The following resources will be deleted: + delete resources.schemas.schema1 + +This action will result in the deletion of the following UC schemas. Any underlying data may be lost: + delete resources.schemas.schema1 + +All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default + +Destroy: 1 deleted + +>>> print_requests.py //unity-catalog +{ + "method": "DELETE", + "path": "/api/2.1/unity-catalog/schemas/main.moved_schema", + "q": { + "force": "true" + } +} + +>>> musterr [CLI] schemas get main.moved_schema +Error: Schema 'main.moved_schema' does not exist. diff --git a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/script b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/script new file mode 100644 index 00000000000..ab7eed60b68 --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/script @@ -0,0 +1,14 @@ +envsubst < databricks.yml.tmpl > databricks.yml + +OLD_SCHEMA="main.myschema" +NEW_SCHEMA="main.moved_schema" + +title "Deploy a schema and rename it out of band; the recorded name is not readable" +trace $CLI bundle deploy +$CLI schemas update "$OLD_SCHEMA" --new-name moved_schema > /dev/null +print_requests.py //unity-catalog &> LOG.deploy_requests + +title "Destroy finds the renamed schema by its schema_id and deletes it there" +trace $CLI bundle destroy --auto-approve +trace print_requests.py //unity-catalog +trace musterr $CLI schemas get "$NEW_SCHEMA" diff --git a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/test.toml b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/test.toml new file mode 100644 index 00000000000..8f221db5b3e --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/test.toml @@ -0,0 +1,13 @@ +# Local only: the 403 at the recorded name is injected, as for another user's schema the +# deployer cannot read. + +Ignore = [ + ".databricks", + ".gitignore", + "databricks.yml", +] + +[[Server]] +Pattern = "GET /api/2.1/unity-catalog/schemas/main.myschema" +Response.StatusCode = 403 +Response.Body = '{"error_code": "PERMISSION_DENIED", "message": "User does not have USE SCHEMA on the schema"}' diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/output.txt b/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/output.txt index b19f72f8b30..62292cf7679 100644 --- a/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/output.txt +++ b/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/output.txt @@ -15,20 +15,34 @@ Resources: 3 created, 0 changed, 0 deleted, 0 unchanged "schema_id": "[SCHEMA_ID]" } -=== The model and the secret have no internal ID to find them by, so destroy lists them as going with the schema it found, and sends a delete only for the schema +=== Out of band, delete the secret, and create another schema under the old name with a model of the same name + +>>> [CLI] secrets-uc delete-secret main.test-schema-renamed-[UNIQUE_NAME].mysecret + +>>> [CLI] schemas create test-schema-[UNIQUE_NAME] main +{ + "full_name": "main.test-schema-[UNIQUE_NAME]", + "schema_id": "[UUID]" +} + +>>> [CLI] registered-models create --catalog-name main --schema-name test-schema-[UNIQUE_NAME] --name mymodel +{ + "full_name": "main.test-schema-[UNIQUE_NAME].mymodel" +} + +=== The model has no internal ID to find it by, so destroy lists it as going with the schema it found, and sends a delete only for that schema. The secret is gone, and nothing at the old name is touched >>> [CLI] bundle destroy --auto-approve The following resources will be deleted: delete resources.registered_models.model1 delete resources.schemas.schema1 - delete resources.secrets.secret1 This action will result in the deletion of the following UC schemas. Any underlying data may be lost: delete resources.schemas.schema1 All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default -Destroy: 3 deleted +Destroy: 2 deleted >>> print_requests.py //unity-catalog { @@ -41,3 +55,8 @@ Destroy: 3 deleted >>> musterr [CLI] schemas get main.test-schema-renamed-[UNIQUE_NAME] Error: Schema 'main.test-schema-renamed-[UNIQUE_NAME]' does not exist. + +>>> [CLI] registered-models get main.test-schema-[UNIQUE_NAME].mymodel +{ + "full_name": "main.test-schema-[UNIQUE_NAME].mymodel" +} diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/script b/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/script index ad91de3a978..222384f1d9f 100644 --- a/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/script +++ b/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/script @@ -6,6 +6,7 @@ NEW_SCHEMA="main.test-schema-renamed-${UNIQUE_NAME}" cleanup() { errcode $CLI schemas delete "$NEW_SCHEMA" --force > LOG.cleanup 2>&1 + errcode $CLI schemas delete "$OLD_SCHEMA" --force >> LOG.cleanup 2>&1 # Consume the destroy's requests so none are left recorded. print_requests.py //unity-catalog &> LOG.destroy_requests @@ -18,7 +19,14 @@ trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE print_requests.py //unity-catalog &> LOG.deploy_requests add_repl "$($CLI schemas get "$NEW_SCHEMA" | jq -r .schema_id)" SCHEMA_ID -title "The model and the secret have no internal ID to find them by, so destroy lists them as going with the schema it found, and sends a delete only for the schema" +title "Out of band, delete the secret, and create another schema under the old name with a model of the same name" +trace $CLI secrets-uc delete-secret "$NEW_SCHEMA.mysecret" +trace $CLI schemas create "test-schema-${UNIQUE_NAME}" main | schema_fields +trace $CLI registered-models create --catalog-name main --schema-name "test-schema-${UNIQUE_NAME}" --name mymodel | jq '{full_name}' +print_requests.py //unity-catalog &> LOG.oob_requests + +title "The model has no internal ID to find it by, so destroy lists it as going with the schema it found, and sends a delete only for that schema. The secret is gone, and nothing at the old name is touched" trace $CLI bundle destroy --auto-approve trace print_requests.py //unity-catalog trace musterr $CLI schemas get "$NEW_SCHEMA" +trace $CLI registered-models get "$OLD_SCHEMA.mymodel" | jq '{full_name}' diff --git a/acceptance/bundle/resources/volumes/select_renamed_schema/databricks-empty.yml.tmpl b/acceptance/bundle/resources/volumes/select_renamed_schema/databricks-empty.yml.tmpl new file mode 100644 index 00000000000..244a11badf3 --- /dev/null +++ b/acceptance/bundle/resources/volumes/select_renamed_schema/databricks-empty.yml.tmpl @@ -0,0 +1,2 @@ +bundle: + name: test-bundle-$UNIQUE_NAME diff --git a/acceptance/bundle/resources/volumes/select_renamed_schema/databricks-schema-only.yml.tmpl b/acceptance/bundle/resources/volumes/select_renamed_schema/databricks-schema-only.yml.tmpl new file mode 100644 index 00000000000..6f97649e503 --- /dev/null +++ b/acceptance/bundle/resources/volumes/select_renamed_schema/databricks-schema-only.yml.tmpl @@ -0,0 +1,8 @@ +bundle: + name: test-bundle-$UNIQUE_NAME + +resources: + schemas: + schema1: + catalog_name: main + name: Test-Schema-$UNIQUE_NAME diff --git a/acceptance/bundle/resources/volumes/select_renamed_schema/databricks.yml.tmpl b/acceptance/bundle/resources/volumes/select_renamed_schema/databricks.yml.tmpl new file mode 100644 index 00000000000..278bc421df8 --- /dev/null +++ b/acceptance/bundle/resources/volumes/select_renamed_schema/databricks.yml.tmpl @@ -0,0 +1,13 @@ +bundle: + name: test-bundle-$UNIQUE_NAME + +resources: + schemas: + schema1: + catalog_name: main + name: Test-Schema-$UNIQUE_NAME + volumes: + volume1: + catalog_name: main + schema_name: test-schema-$UNIQUE_NAME + name: myvolume diff --git a/acceptance/bundle/resources/volumes/select_renamed_schema/out.test.toml b/acceptance/bundle/resources/volumes/select_renamed_schema/out.test.toml new file mode 100644 index 00000000000..0f167effef1 --- /dev/null +++ b/acceptance/bundle/resources/volumes/select_renamed_schema/out.test.toml @@ -0,0 +1,2 @@ +Cloud = true +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/volumes/select_renamed_schema/output.txt b/acceptance/bundle/resources/volumes/select_renamed_schema/output.txt new file mode 100644 index 00000000000..52229a8a6d5 --- /dev/null +++ b/acceptance/bundle/resources/volumes/select_renamed_schema/output.txt @@ -0,0 +1,61 @@ + +=== Deploy a schema, then a volume that names it in lower case: the volume has no dependency on the schema + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Created schemas.schema1 +Files: 4 uploaded, 0 deleted +Resources: 1 created, 0 changed, 0 deleted, 0 unchanged + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Created volumes.volume1 +Files: 1 uploaded, 0 deleted +Resources: 1 created, 0 changed, 0 deleted, 1 unchanged + +=== Rename the schema and the volume out of band, and remove both from the config + +>>> [CLI] schemas update main.test-schema-[UNIQUE_NAME] --new-name test-schema-renamed-[UNIQUE_NAME] +{ + "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", + "schema_id": "[UUID][0]" +} + +>>> [CLI] volumes update main.test-schema-renamed-[UNIQUE_NAME].myvolume --new-name myvolume2 +{ + "full_name": "main.test-schema-renamed-[UNIQUE_NAME].myvolume2", + "volume_id": "[UUID][1]" +} + +=== Selecting only the schema still lists the volume, which its delete takes along + +>>> [CLI] bundle deploy --select schemas.schema1 --auto-approve +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... + +This action will result in the deletion or recreation of the following UC schemas. Any underlying data may be lost: + delete resources.schemas.schema1 + +This action will result in the deletion or recreation of the following volumes. +For managed volumes, the files stored in the volume are also deleted from your +cloud tenant within 30 days. For external volumes, the metadata about the volume +is removed from the catalog, but the underlying files are not deleted: + delete resources.volumes.volume1 +Deleted schemas.schema1 +Deleted volumes.volume1 +Files: 3 uploaded, 0 deleted +Resources: 0 created, 0 changed, 2 deleted, 0 unchanged + +>>> print_requests.py //unity-catalog +{ + "method": "DELETE", + "path": "/api/2.1/unity-catalog/schemas/main.test-schema-renamed-[UNIQUE_NAME]", + "q": { + "force": "true" + } +} + +>>> musterr [CLI] volumes read main.test-schema-renamed-[UNIQUE_NAME].myvolume2 +Error: Volume 'main.test-schema-renamed-[UNIQUE_NAME].myvolume2' does not exist. + +>>> [CLI] bundle summary --output json +{} diff --git a/acceptance/bundle/resources/volumes/select_renamed_schema/script b/acceptance/bundle/resources/volumes/select_renamed_schema/script new file mode 100644 index 00000000000..170e32cebca --- /dev/null +++ b/acceptance/bundle/resources/volumes/select_renamed_schema/script @@ -0,0 +1,29 @@ +OLD_SCHEMA="main.test-schema-${UNIQUE_NAME}" +NEW_SCHEMA="main.test-schema-renamed-${UNIQUE_NAME}" + +cleanup() { + errcode $CLI schemas delete "$NEW_SCHEMA" --force > LOG.cleanup 2>&1 + + # Consume the destroy's requests so none are left recorded. + print_requests.py //unity-catalog &> LOG.destroy_requests +} +trap cleanup EXIT + +title "Deploy a schema, then a volume that names it in lower case: the volume has no dependency on the schema" +envsubst < databricks-schema-only.yml.tmpl > databricks.yml +trace $CLI bundle deploy +envsubst < databricks.yml.tmpl > databricks.yml +trace $CLI bundle deploy +print_requests.py //unity-catalog &> LOG.deploy_requests + +title "Rename the schema and the volume out of band, and remove both from the config" +trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields +trace $CLI volumes update "$NEW_SCHEMA.myvolume" --new-name myvolume2 | volume_fields +envsubst < databricks-empty.yml.tmpl > databricks.yml +print_requests.py //unity-catalog &> LOG.rename_requests + +title "Selecting only the schema still lists the volume, which its delete takes along" +trace $CLI bundle deploy --select schemas.schema1 --auto-approve +trace print_requests.py //unity-catalog +trace musterr $CLI volumes read "$NEW_SCHEMA.myvolume2" +trace $CLI bundle summary --output json | jq '.resources // {}' diff --git a/acceptance/bundle/resources/volumes/select_renamed_schema/test.toml b/acceptance/bundle/resources/volumes/select_renamed_schema/test.toml new file mode 100644 index 00000000000..4272a80cf61 --- /dev/null +++ b/acceptance/bundle/resources/volumes/select_renamed_schema/test.toml @@ -0,0 +1,7 @@ +Cloud = true + +Ignore = [ + ".databricks", + ".gitignore", + "databricks.yml", +] diff --git a/acceptance/bundle/resources/volumes/with_parent_delete_fails/databricks.yml.tmpl b/acceptance/bundle/resources/volumes/with_parent_delete_fails/databricks.yml.tmpl new file mode 100644 index 00000000000..2234fdf2e39 --- /dev/null +++ b/acceptance/bundle/resources/volumes/with_parent_delete_fails/databricks.yml.tmpl @@ -0,0 +1,13 @@ +bundle: + name: test-bundle-$UNIQUE_NAME + +resources: + schemas: + schema1: + catalog_name: main + name: test-schema-$UNIQUE_NAME + volumes: + volume1: + catalog_name: main + schema_name: ${resources.schemas.schema1.name} + name: myvolume diff --git a/acceptance/bundle/resources/volumes/with_parent_delete_fails/out.test.toml b/acceptance/bundle/resources/volumes/with_parent_delete_fails/out.test.toml new file mode 100644 index 00000000000..a927a5fbc06 --- /dev/null +++ b/acceptance/bundle/resources/volumes/with_parent_delete_fails/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/volumes/with_parent_delete_fails/output.txt b/acceptance/bundle/resources/volumes/with_parent_delete_fails/output.txt new file mode 100644 index 00000000000..4bb994bfd5e --- /dev/null +++ b/acceptance/bundle/resources/volumes/with_parent_delete_fails/output.txt @@ -0,0 +1,68 @@ + +=== Deploy a schema with a volume in it, and rename both out of band + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Created schemas.schema1 +Created volumes.volume1 +Files: 2 uploaded, 0 deleted +Resources: 2 created, 0 changed, 0 deleted, 0 unchanged + +>>> [CLI] schemas update main.test-schema-[UNIQUE_NAME] --new-name test-schema-renamed-[UNIQUE_NAME] +{ + "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", + "schema_id": "[UUID][0]" +} + +>>> [CLI] volumes update main.test-schema-renamed-[UNIQUE_NAME].myvolume --new-name myvolume2 +{ + "full_name": "main.test-schema-renamed-[UNIQUE_NAME].myvolume2", + "volume_id": "[UUID][1]" +} + +=== The volume goes with the schema's delete, which fails: the volume stays in the state with the schema + +>>> musterr [CLI] bundle destroy --auto-approve +The following resources will be deleted: + delete resources.schemas.schema1 + delete resources.volumes.volume1 + +This action will result in the deletion of the following UC schemas. Any underlying data may be lost: + delete resources.schemas.schema1 + +This action will result in the deletion of the following volumes. +For managed volumes, the files stored in the volume are also deleted from your +cloud tenant within 30 days. For external volumes, the metadata about the volume +is removed from the catalog, but the underlying files are not deleted: + delete resources.volumes.volume1 + +All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default + +Error: cannot delete resources.schemas.schema1: deleting id=main.test-schema-renamed-[UNIQUE_NAME]: injected (400 INVALID_PARAMETER_VALUE) + +Endpoint: DELETE [DATABRICKS_URL]/api/2.1/unity-catalog/schemas/main.test-schema-renamed-[UNIQUE_NAME]?force=true +HTTP Status: 400 Bad Request +API error_code: INVALID_PARAMETER_VALUE +API message: injected + +Error: cannot delete resources.volumes.volume1: dependency failed: resources.schemas.schema1 + + +>>> print_requests.py //unity-catalog +{ + "method": "DELETE", + "path": "/api/2.1/unity-catalog/schemas/main.test-schema-renamed-[UNIQUE_NAME]", + "q": { + "force": "true" + } +} + +>>> [CLI] bundle summary --output json +{ + "schemas": { + "schema1": "main.test-schema-[UNIQUE_NAME]" + }, + "volumes": { + "volume1": "main.test-schema-[UNIQUE_NAME].myvolume" + } +} diff --git a/acceptance/bundle/resources/volumes/with_parent_delete_fails/script b/acceptance/bundle/resources/volumes/with_parent_delete_fails/script new file mode 100644 index 00000000000..7b97e2a90e7 --- /dev/null +++ b/acceptance/bundle/resources/volumes/with_parent_delete_fails/script @@ -0,0 +1,15 @@ +envsubst < databricks.yml.tmpl > databricks.yml + +OLD_SCHEMA="main.test-schema-${UNIQUE_NAME}" +NEW_SCHEMA="main.test-schema-renamed-${UNIQUE_NAME}" + +title "Deploy a schema with a volume in it, and rename both out of band" +trace $CLI bundle deploy +trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields +trace $CLI volumes update "$NEW_SCHEMA.myvolume" --new-name myvolume2 | volume_fields +print_requests.py //unity-catalog &> LOG.deploy_requests + +title "The volume goes with the schema's delete, which fails: the volume stays in the state with the schema" +trace musterr $CLI bundle destroy --auto-approve +trace print_requests.py //unity-catalog +trace $CLI bundle summary --output json | jq '.resources | map_values(map_values(.id))' diff --git a/acceptance/bundle/resources/volumes/with_parent_delete_fails/test.toml b/acceptance/bundle/resources/volumes/with_parent_delete_fails/test.toml new file mode 100644 index 00000000000..f135f75f4a0 --- /dev/null +++ b/acceptance/bundle/resources/volumes/with_parent_delete_fails/test.toml @@ -0,0 +1,12 @@ +# Local only: the failure of the schema's delete is injected. + +Ignore = [ + ".databricks", + ".gitignore", + "databricks.yml", +] + +[[Server]] +Pattern = "DELETE /api/2.1/unity-catalog/schemas/{full_name}" +Response.StatusCode = 400 +Response.Body = '{"error_code": "INVALID_PARAMETER_VALUE", "message": "injected"}' diff --git a/bundle/deployplan/plan.go b/bundle/deployplan/plan.go index eb52ba62945..b85b2c4ce82 100644 --- a/bundle/deployplan/plan.go +++ b/bundle/deployplan/plan.go @@ -131,10 +131,10 @@ type PlanEntry struct { ID string `json:"id,omitempty"` DependsOn []DependsOnEntry `json:"depends_on,omitempty"` Action ActionType `json:"action,omitempty"` - // Gone is set on Delete entries when the resource no longer exists remotely: planning - // confirmed it with a read (for a resource with an internal ID: found neither at its ID, where - // another object may be, nor by its internal ID, nor under a renamed parent; see - // findInMovedParents), or the state entry has an empty ID (left by a recreate in an older CLI + // Gone is set on Delete entries when planning did not find the resource remotely: a read at its + // ID returned nothing (for a resource with an internal ID: found neither at its ID, where another + // object may be, nor by its internal ID, nor under a renamed parent; see findInMovedParents; a + // search the user may not run finds nothing too), or the state entry has an empty ID (left by a recreate in an older CLI // whose create step failed after the delete). Applying such an entry only removes it from the // state, without calling the delete API, and approval prompts do not list it as a deletion // unless WithParent is set. @@ -433,6 +433,10 @@ func (p *Plan) FilterToSelected(_ context.Context, selected []string) error { key := queue[0] queue = queue[1:] if p.Plan[key].Action == Delete { + if parent := p.Plan[key].WithParent; parent != "" { + // Its parent's delete is what removes it. + p.enqueueReachable(reachable, &queue, parent) + } for _, dependent := range dependents[key] { // Sub-nodes (grants/permissions, 4-part key) are not user-facing dependents // and never trigger the live-dependent error. If they are themselves being diff --git a/bundle/deployplan/plan_filter_test.go b/bundle/deployplan/plan_filter_test.go index 8c6dd30240e..f19cc927c3c 100644 --- a/bundle/deployplan/plan_filter_test.go +++ b/bundle/deployplan/plan_filter_test.go @@ -283,3 +283,18 @@ func TestFilterToSelected_DeletionLeavesDependencies(t *testing.T) { assert.Contains(t, p.Plan, "resources.jobs.gone") assert.NotContains(t, p.Plan, "resources.jobs.lib") } + +func TestFilterToSelected_WithParentBringsItsParent(t *testing.T) { + p := deployplan.NewPlanDirect() + schemaDep := deployplan.DependsOnEntry{Node: "resources.schemas.s"} + p.Plan["resources.schemas.s"] = &deployplan.PlanEntry{Action: deployplan.Delete, ID: "main.moved"} + p.Plan["resources.volumes.v"] = &deployplan.PlanEntry{Action: deployplan.Delete, Gone: true, WithParent: "resources.schemas.s", DependsOn: []deployplan.DependsOnEntry{schemaDep}} + p.Plan["resources.volumes.other"] = &deployplan.PlanEntry{Action: deployplan.Delete} + + // Its parent's delete is what removes it, so selecting it brings the parent, and the parent brings + // it back. + require.NoError(t, p.FilterToSelected(t.Context(), []string{"volumes.v"})) + assert.Contains(t, p.Plan, "resources.schemas.s") + assert.Contains(t, p.Plan, "resources.volumes.v") + assert.NotContains(t, p.Plan, "resources.volumes.other") +} diff --git a/bundle/direct/bundle_plan.go b/bundle/direct/bundle_plan.go index 4555e556ef8..1aa2b06c2ee 100644 --- a/bundle/direct/bundle_plan.go +++ b/bundle/direct/bundle_plan.go @@ -493,6 +493,13 @@ func (b *DeploymentBundle) CalculatePlan(ctx context.Context, client *databricks } b.warnOtherObjects(ctx, plan) + // findInMovedParents can add dependencies on a moved parent. + if g, err := makeGraph(plan); err != nil { + return nil, err + } else if err := g.DetectCycle(); err != nil { + return nil, err + } + for _, entry := range plan.Plan { if entry.Action == deployplan.Skip { entry.NewState = nil @@ -914,18 +921,20 @@ func isParentIDField(adapter *dresources.Adapter, path *structpath.PathNode) boo // findInMovedParents handles a Delete of a resource not found at its recorded ID (Gone) while its // parent was found at another ID in this plan: renamed out of band (see findMoved and // findByInternalID). The rename moved the resource with the parent, so it is looked for under the -// parent's new ID, and deleted there as itself if it has its recorded internal ID. When it cannot be -// verified there (it was deleted or renamed too, another object holds the name, or it has no internal -// ID) and the parent is deleted, it is marked WithParent: no delete is sent for it, but approval and -// lifecycle.prevent_destroy see that the parent's delete takes it along. +// parent's new ID, and deleted there as itself if it has its recorded internal ID. When the parent is +// deleted and the resource is there but cannot be verified (it has no internal ID: a model, a secret, +// a feature), or may be there (it has one, and a 404 cannot tell a renamed resource from a deleted +// one), it is marked WithParent: no delete is sent for it, but approval and lifecycle.prevent_destroy +// see that the parent's delete takes it along. A resource without an internal ID that is not there +// stays Gone. func (b *DeploymentBundle) findInMovedParents(ctx context.Context, plan *deployplan.Plan) error { parents, err := b.movedParents(plan) if err != nil || len(parents) == 0 { return err } - // A parent's own Delete may be found here too (a schema in a renamed catalog), so the shallower - // resources go first. + // A verified child is a parent for deeper ones (a resource in a volume, should one exist), so the + // shallower resources go first. keys := slices.Collect(maps.Keys(plan.Plan)) slices.SortFunc(keys, func(x, y string) int { return cmp.Or( @@ -936,7 +945,7 @@ func (b *DeploymentBundle) findInMovedParents(ctx context.Context, plan *deployp for _, resourceKey := range keys { entry := plan.Plan[resourceKey] - if entry.Action != deployplan.Delete || !entry.Gone { + if entry.Action != deployplan.Delete || entry.ID != "" || (entry.StateOnly && !entry.Gone) { continue } adapter, err := b.getAdapterForKey(resourceKey) @@ -944,29 +953,45 @@ func (b *DeploymentBundle) findInMovedParents(ctx context.Context, plan *deployp return err } dbentry, ok := b.StateDB.GetResourceEntry(resourceKey) - if !ok || !adapter.HasIDFromState() { + if !ok { continue } + if !entry.Gone && hasRecordedInternalID(adapter, dbentry) { + // Verified at its recorded ID by its internal ID (see findDeleteTarget): it did not move. + continue + } + // Any resource whose ID is a full name under the parent's can be in it, with or without + // IDFromState (a feature's ID is its full name too). parent, id := underMovedParent(parents, dbentry.ID) if parent == nil { continue } - remote, err := b.readIfOwn(ctx, adapter, dbentry, id) + remote, verified, maybeThere, err := b.readUnderMovedParent(ctx, adapter, dbentry, id) if err != nil { return fmt.Errorf("%s: %w", resourceKey, err) } + // Whatever is at the recorded ID now is not in the bundle's parent, so it is never the target. + entry.Gone = true + entry.RemoteState = nil switch { - case remote != nil: + case verified || (remote != nil && !parent.deleted): + // Verified by its internal ID; or, without one to check, found at the name the rename gave it + // inside the bundle's own parent, which stays: deleted there as itself. entry.ID = id entry.RemoteState = remote entry.Gone = false - if parent.deleted && !slices.ContainsFunc(entry.DependsOn, func(d deployplan.DependsOnEntry) bool { return d.Node == parent.key }) { - // Deleted before its parent, also when the config named the parent literally. - entry.DependsOn = append(entry.DependsOn, deployplan.DependsOnEntry{Node: parent.key}) + if verified && adapter.HasIDFromState() { + parents = append(parents, &movedParent{key: resourceKey, recordedID: dbentry.ID, id: id, deleted: true}) } - parents = append(parents, &movedParent{key: resourceKey, recordedID: dbentry.ID, id: id, deleted: true}) - case parent.deleted: + case parent.deleted && maybeThere: entry.WithParent = parent.key + default: + continue + } + if parent.deleted && !slices.ContainsFunc(entry.DependsOn, func(d deployplan.DependsOnEntry) bool { return d.Node == parent.key }) { + // Ordered with its parent (see makeGraph), and selected with it (see FilterToSelected), + // also when the config named the parent literally. + entry.DependsOn = append(entry.DependsOn, deployplan.DependsOnEntry{Node: parent.key}) } } return nil @@ -1020,32 +1045,56 @@ func underMovedParent(parents []*movedParent, id string) (*movedParent, string) return best, best.id + "." + id[len(best.recordedID)+1:] } -// readIfOwn reads the resource recorded in dbentry at id and returns it if it has its recorded -// internal ID; nil if it is not there, is another object, or has no internal ID to tell. A read that -// fails other than with a missing resource or a permission error fails the plan. -func (b *DeploymentBundle) readIfOwn(ctx context.Context, adapter *dresources.Adapter, dbentry dstate.ResourceEntry, id string) (any, error) { - if !adapter.HasInternalID() || len(dbentry.State) == 0 { - return nil, nil - } - state, err := parseState(adapter.StateType(), dbentry.State) - if err != nil { - return nil, fmt.Errorf("interpreting state: %w", err) - } - savedInternalID, err := adapter.InternalID(state) - if err != nil || savedInternalID == "" { - return nil, err +// readUnderMovedParent reads the resource recorded in dbentry at id, under its parent's new ID. It +// returns what it read there (nil if nothing), and whether that is verified: it has the recorded +// internal ID. Else maybeThere reports whether the resource can be there: found, or not readable, or, +// for a resource with an internal ID, not found (a 404 cannot tell a resource renamed too from one +// deleted). A read that fails other than with a missing resource or a permission error fails the +// plan. +func (b *DeploymentBundle) readUnderMovedParent(ctx context.Context, adapter *dresources.Adapter, dbentry dstate.ResourceEntry, id string) (remote any, verified, maybeThere bool, err error) { + savedInternalID := "" + if hasRecordedInternalID(adapter, dbentry) { + state, err := parseState(adapter.StateType(), dbentry.State) + if err != nil { + return nil, false, false, fmt.Errorf("interpreting state: %w", err) + } + if savedInternalID, err = adapter.InternalID(state); err != nil { + return nil, false, false, err + } } - remote, err := retry.With(ctx, retry.IsTransientRead, func() (any, error) { + remote, err = retry.With(ctx, retry.IsTransientRead, func() (any, error) { return adapter.DoRead(ctx, id) }) if err != nil { - return nil, ignoreMissingOrForbidden(ctx, err, fmt.Sprintf("reading id=%q under its renamed parent", id)) + if apierr.IsMissing(err) { + return nil, false, savedInternalID != "", nil + } + return nil, false, true, ignoreMissingOrForbidden(ctx, err, fmt.Sprintf("reading id=%q under its renamed parent", id)) + } + if savedInternalID == "" { + return remote, false, true, nil } ok, err := hasInternalIDOf(adapter, remote, savedInternalID) - if err != nil || !ok { - return nil, err + if err != nil { + return nil, false, false, err + } + if !ok { + return nil, false, true, nil + } + return remote, true, true, nil +} + +// hasRecordedInternalID reports whether the state in dbentry has the resource's internal ID. +func hasRecordedInternalID(adapter *dresources.Adapter, dbentry dstate.ResourceEntry) bool { + if !adapter.HasInternalID() || len(dbentry.State) == 0 { + return false + } + state, err := parseState(adapter.StateType(), dbentry.State) + if err != nil { + return false } - return remote, nil + id, err := adapter.InternalID(state) + return err == nil && id != "" } // recordRefTypes records, for each pure-reference field of resourceKey, the type of the resource it @@ -1179,7 +1228,7 @@ func (b *DeploymentBundle) warnOtherObjects(ctx context.Context, plan *deploypla if !ok || plan.Plan[resourceKey].ID != "" { continue } - logdiag.LogDiag(ctx, diag.Warningf("%s: %s, and drops %s from the deployment state. If it is the bundle's (deleted and created again outside the bundle), bind it with 'bundle deployment bind' before destroying, or delete it outside the bundle.", + logdiag.LogDiag(ctx, diag.Warningf("%s: %s, and drops %s from the deployment state. If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle, or once the state entry is dropped, bind it again with 'bundle deployment bind' and destroy.", resourceKey, message, resourceKey)[0]) } } @@ -1212,9 +1261,11 @@ func (b *DeploymentBundle) findDeleteTarget(ctx context.Context, resourceKey str } if readErr != nil { if errors.Is(readErr, apierr.ErrPermissionDenied) { - // Deleted as before: a resource the user cannot read is left to the delete call. + // What the user cannot read may be another object under the name ours had (whose owner + // did not grant access): look for ours first. Found nowhere, it is left to the delete + // call at the recorded ID, as before. log.Warnf(ctx, "reading %s id=%q: %s", resourceKey, id, readErr) - return "", nil, nil + return findByInternalID(ctx, adapter, savedState) } return "", nil, fmt.Errorf("cannot check that id=%q is still this bundle's: %w", id, readErr) } @@ -1272,7 +1323,7 @@ func ignoreMissingOrForbidden(ctx context.Context, err error, what string) error return nil } if errors.Is(err, apierr.ErrPermissionDenied) { - log.Warnf(ctx, "%s: %s", what, err) + logdiag.LogDiag(ctx, diag.Warningf("%s: %s", what, err)[0]) return nil } return fmt.Errorf("%s: %w", what, err) diff --git a/bundle/direct/dresources/README.md b/bundle/direct/dresources/README.md index 56d3f197bb1..5bc04d79706 100644 --- a/bundle/direct/dresources/README.md +++ b/bundle/direct/dresources/README.md @@ -62,7 +62,7 @@ If the ID is built from the resource's own fields (a UC full name), implement `I If the backend also has its own ID that a rename keeps (a volume's `volume_id`), add it to the state type, copy it in `RemapState`, and name it in the config as `internal_id_field`. The framework records it on every save and skips its changes in the plan. When nothing is at the recorded ID, the plan reads at the ID the config gives the resource and takes what it finds only if the internal ID matches. This covers a rename done out of band, or by a deploy that stopped before re-keying the resource. -An object at the recorded ID with a different internal ID is someone else's (ours was renamed away and another created under its name): the plan treats ours as not there, so it never updates or deletes the other object. A resource to be deleted (by destroy, or removed from config) has no config to give its new name, so to delete one renamed out of band the resource needs `DoFindByInternalID`: it searches the parent (lists the schemas in the catalog, the volumes in the schema) for the recorded internal ID. Without it, the delete only drops the state entry of a resource not found at its recorded ID. A child whose parent the plan found at a new ID (a volume in a renamed schema, matched by full-name prefix) is looked for under the parent's new ID and deleted there as itself if its internal ID matches. If it cannot be verified there and the parent is deleted, no delete is sent for it, but the plan lists it as going with the parent (`with_parent`), so approval and its `prevent_destroy` still apply. A parent outside the bundle that was renamed is not followed. +An object at the recorded ID with a different internal ID is someone else's (ours was renamed away and another created under its name): the plan treats ours as not there, so it never updates or deletes the other object. A resource to be deleted (by destroy, or removed from config) has no config to give its new name, so to delete one renamed out of band the resource needs `DoFindByInternalID`: it searches the parent (lists the schemas in the catalog, the volumes in the schema) for the recorded internal ID. Without it, the delete only drops the state entry of a resource not found at its recorded ID. A child whose parent the plan found at a new ID (a volume in a renamed schema, matched by full-name prefix) is looked for under the parent's new ID and deleted there as itself if its internal ID matches. If it cannot be verified there and the parent is deleted, no delete is sent for it, but the plan lists it as going with the parent (`with_parent`), so approval and its `prevent_destroy` still apply. A parent outside the bundle that was renamed is not followed. The search is a listing: one list of the schemas in the catalog (or the volumes in the schema) per resource not found at its recorded ID, so a destroy in a catalog with many schemas pages through all of them. ## Empty states: IsEmptyState diff --git a/bundle/direct/dresources/adapter.go b/bundle/direct/dresources/adapter.go index 46e4838b45a..c95c31662df 100644 --- a/bundle/direct/dresources/adapter.go +++ b/bundle/direct/dresources/adapter.go @@ -122,7 +122,9 @@ type IResource interface { // [Optional] IDFromState returns the ID the state gives the resource, for resource types whose ID // is built from their own fields (a UC full name). With it, an update_id that only follows a - // parent's rename is applied without calling DoUpdateWithID (see PlanEntry.StateOnly). + // parent's rename is applied without calling DoUpdateWithID (see PlanEntry.StateOnly). The ID + // must be a UC full name: dot-joined, parent first, compared case-insensitively; the plan finds + // the resources in a renamed parent by it (see findInMovedParents). // Example: func (*ResourceVolume) IDFromState(state *VolumeState) string IDFromState(state any) string diff --git a/bundle/direct/graph.go b/bundle/direct/graph.go index 386c590f53c..728547bd62e 100644 --- a/bundle/direct/graph.go +++ b/bundle/direct/graph.go @@ -28,7 +28,11 @@ func makeGraph(plan *deployplan.Plan) (*dagrun.Graph, error) { for _, dep := range entry.DependsOn { if _, exists := plan.Plan[dep.Node]; exists { - if isDelete { + if isDelete && dep.Node == entry.WithParent { + // No delete is sent for it: its state is dropped only once its parent's + // delete, which removes it, has succeeded. + g.AddDirectedEdge(dep.Node, resourceKey, dep.Label) + } else if isDelete { g.AddDirectedEdge(resourceKey, dep.Node, dep.Label) } else { g.AddDirectedEdge(dep.Node, resourceKey, dep.Label) diff --git a/bundle/direct/graph_test.go b/bundle/direct/graph_test.go new file mode 100644 index 00000000000..c20333d5d11 --- /dev/null +++ b/bundle/direct/graph_test.go @@ -0,0 +1,52 @@ +package direct + +import ( + "sync" + "testing" + + "github.com/databricks/cli/bundle/deployplan" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestMakeGraphDeletesWithParentAfterItsParent(t *testing.T) { + const ( + schemaKey = "resources.schemas.schema1" + volumeKey = "resources.volumes.volume1" + modelKey = "resources.registered_models.model1" + ) + schemaDep := deployplan.DependsOnEntry{Node: schemaKey, Label: "${resources.schemas.schema1.name}"} + plan := deployplan.NewPlanDirect() + plan.Plan[schemaKey] = &deployplan.PlanEntry{Action: deployplan.Delete, ID: "main.moved"} + // Verified under the moved schema: deleted as itself, before the schema. + plan.Plan[volumeKey] = &deployplan.PlanEntry{Action: deployplan.Delete, ID: "main.moved.v", DependsOn: []deployplan.DependsOnEntry{schemaDep}} + // Goes with the schema's delete: its state is dropped only once that delete succeeded. + plan.Plan[modelKey] = &deployplan.PlanEntry{Action: deployplan.Delete, Gone: true, WithParent: schemaKey, DependsOn: []deployplan.DependsOnEntry{schemaDep}} + + for _, schemaFails := range []bool{false, true} { + g, err := makeGraph(plan) + require.NoError(t, err) + require.NoError(t, g.DetectCycle()) + + var mu sync.Mutex + var order []string + failed := map[string]bool{} + g.Run(1, func(key string, failedDependency *string) bool { + mu.Lock() + defer mu.Unlock() + if failedDependency != nil { + failed[key] = true + return false + } + order = append(order, key) + return !(schemaFails && key == schemaKey) + }) + + if schemaFails { + assert.Equal(t, []string{volumeKey, schemaKey}, order) + assert.Equal(t, map[string]bool{modelKey: true}, failed) + } else { + assert.Equal(t, []string{volumeKey, schemaKey, modelKey}, order) + } + } +} From d3c60aa5ca6b18dafa5de515571e179b3775cd80 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Thu, 26 Nov 2026 17:13:28 +0000 Subject: [PATCH 09/22] direct: test fixes for the 403 search and staticcheck Co-authored-by: Isaac --- bundle/direct/bundle_plan_test.go | 6 +----- bundle/direct/graph_test.go | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/bundle/direct/bundle_plan_test.go b/bundle/direct/bundle_plan_test.go index 5847a82803a..27991b0a4aa 100644 --- a/bundle/direct/bundle_plan_test.go +++ b/bundle/direct/bundle_plan_test.go @@ -1061,11 +1061,7 @@ func TestFindDeleteTargetUnverifiedRead(t *testing.T) { _, _, err = b.findDeleteTarget(t.Context(), key, adapters["schemas"], "main.s1", nil, apierr.ErrInternalError) assert.ErrorIs(t, err, apierr.ErrInternalError) - // A permission error leaves the delete to the recorded ID, as before. - id, got, err := b.findDeleteTarget(t.Context(), key, adapters["schemas"], "main.s1", nil, apierr.ErrPermissionDenied) - require.NoError(t, err) - assert.Empty(t, id) - assert.Nil(t, got) + // A permission error searches by internal ID first: schemas/destroy_forbidden_at_recorded_name. } func TestUnderMovedParent(t *testing.T) { diff --git a/bundle/direct/graph_test.go b/bundle/direct/graph_test.go index c20333d5d11..1fc5b7cf552 100644 --- a/bundle/direct/graph_test.go +++ b/bundle/direct/graph_test.go @@ -39,7 +39,7 @@ func TestMakeGraphDeletesWithParentAfterItsParent(t *testing.T) { return false } order = append(order, key) - return !(schemaFails && key == schemaKey) + return !schemaFails || key != schemaKey }) if schemaFails { From 0003d749daecee23610ef9b0a2acb4c7fb88ec87 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Thu, 26 Nov 2026 17:23:45 +0000 Subject: [PATCH 10/22] direct: re-target only unverified UC children under a moved parent A child whose recorded read was forbidden and not found by its internal ID is looked for under the moved parent too; an entry found at its recorded ID is re-targeted only for UC full-name types, so a secret scope named like a schema is deleted where it is. Co-authored-by: Isaac --- .../databricks.yml.tmpl | 5 +++ .../output.txt | 21 +++++++-- .../destroy_forbidden_at_recorded_name/script | 4 +- .../test.toml | 5 +++ .../databricks.yml | 11 +++++ .../out.test.toml | 2 + .../output.txt | 43 +++++++++++++++++++ .../script | 9 ++++ .../test.toml | 5 +++ bundle/direct/bundle_plan.go | 7 ++- 10 files changed, 105 insertions(+), 7 deletions(-) create mode 100644 acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/databricks.yml create mode 100644 acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/out.test.toml create mode 100644 acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/output.txt create mode 100644 acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/script create mode 100644 acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/test.toml diff --git a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/databricks.yml.tmpl b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/databricks.yml.tmpl index 925877124dd..192f6d3b8b3 100644 --- a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/databricks.yml.tmpl +++ b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/databricks.yml.tmpl @@ -6,3 +6,8 @@ resources: schema1: catalog_name: main name: myschema + volumes: + volume1: + catalog_name: main + schema_name: ${resources.schemas.schema1.name} + name: myvolume diff --git a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/output.txt b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/output.txt index 53dc78b9427..9062926de43 100644 --- a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/output.txt +++ b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/output.txt @@ -1,27 +1,40 @@ -=== Deploy a schema and rename it out of band; the recorded name is not readable +=== Deploy a schema with a volume in it and rename the schema out of band; the recorded names are not readable >>> [CLI] bundle deploy Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... Created schemas.schema1 +Created volumes.volume1 Files: 2 uploaded, 0 deleted -Resources: 1 created, 0 changed, 0 deleted, 0 unchanged +Resources: 2 created, 0 changed, 0 deleted, 0 unchanged -=== Destroy finds the renamed schema by its schema_id and deletes it there +=== Destroy finds the renamed schema by its schema_id, and the volume in it by its volume_id, and deletes both there >>> [CLI] bundle destroy --auto-approve +Warn: planning resources.volumes.volume1: reading resources.volumes.volume1 id="main.myschema.myvolume": User does not have USE SCHEMA on the schema Warn: planning resources.schemas.schema1: reading resources.schemas.schema1 id="main.myschema": User does not have USE SCHEMA on the schema The following resources will be deleted: delete resources.schemas.schema1 + delete resources.volumes.volume1 This action will result in the deletion of the following UC schemas. Any underlying data may be lost: delete resources.schemas.schema1 +This action will result in the deletion of the following volumes. +For managed volumes, the files stored in the volume are also deleted from your +cloud tenant within 30 days. For external volumes, the metadata about the volume +is removed from the catalog, but the underlying files are not deleted: + delete resources.volumes.volume1 + All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default -Destroy: 1 deleted +Destroy: 2 deleted >>> print_requests.py //unity-catalog +{ + "method": "DELETE", + "path": "/api/2.1/unity-catalog/volumes/main.moved_schema.myvolume" +} { "method": "DELETE", "path": "/api/2.1/unity-catalog/schemas/main.moved_schema", diff --git a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/script b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/script index ab7eed60b68..43b1b4a3da8 100644 --- a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/script +++ b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/script @@ -3,12 +3,12 @@ envsubst < databricks.yml.tmpl > databricks.yml OLD_SCHEMA="main.myschema" NEW_SCHEMA="main.moved_schema" -title "Deploy a schema and rename it out of band; the recorded name is not readable" +title "Deploy a schema with a volume in it and rename the schema out of band; the recorded names are not readable" trace $CLI bundle deploy $CLI schemas update "$OLD_SCHEMA" --new-name moved_schema > /dev/null print_requests.py //unity-catalog &> LOG.deploy_requests -title "Destroy finds the renamed schema by its schema_id and deletes it there" +title "Destroy finds the renamed schema by its schema_id, and the volume in it by its volume_id, and deletes both there" trace $CLI bundle destroy --auto-approve trace print_requests.py //unity-catalog trace musterr $CLI schemas get "$NEW_SCHEMA" diff --git a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/test.toml b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/test.toml index 8f221db5b3e..626afeea1eb 100644 --- a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/test.toml +++ b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/test.toml @@ -11,3 +11,8 @@ Ignore = [ Pattern = "GET /api/2.1/unity-catalog/schemas/main.myschema" Response.StatusCode = 403 Response.Body = '{"error_code": "PERMISSION_DENIED", "message": "User does not have USE SCHEMA on the schema"}' + +[[Server]] +Pattern = "GET /api/2.1/unity-catalog/volumes/main.myschema.myvolume" +Response.StatusCode = 403 +Response.Body = '{"error_code": "PERMISSION_DENIED", "message": "User does not have USE SCHEMA on the schema"}' diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/databricks.yml b/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/databricks.yml new file mode 100644 index 00000000000..bd70982d43e --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/databricks.yml @@ -0,0 +1,11 @@ +bundle: + name: test-bundle + +resources: + schemas: + schema1: + catalog_name: main + name: myschema + secret_scopes: + scope1: + name: main.myschema.scope diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/out.test.toml b/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/out.test.toml new file mode 100644 index 00000000000..a927a5fbc06 --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/output.txt b/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/output.txt new file mode 100644 index 00000000000..c090430d5b7 --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/output.txt @@ -0,0 +1,43 @@ + +=== Deploy a schema and a secret scope whose name starts with the schema's full name, and rename the schema out of band + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files... +Created schemas.schema1 +Created secret_scopes.scope1 +Created secret_scopes.scope1.permissions +Files: 1 uploaded, 0 deleted +Resources: 3 created, 0 changed, 0 deleted, 0 unchanged + +=== The scope is not in the schema: destroy deletes it where it is, and the schema at its new name + +>>> [CLI] bundle destroy --auto-approve +The following resources will be deleted: + delete resources.schemas.schema1 + delete resources.secret_scopes.scope1 + +This action will result in the deletion of the following UC schemas. Any underlying data may be lost: + delete resources.schemas.schema1 + +All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle/default + +Destroy: 2 deleted + +>>> print_requests.py --sort //unity-catalog //secrets +{ + "method": "DELETE", + "path": "/api/2.1/unity-catalog/schemas/main.moved_schema", + "q": { + "force": "true" + } +} +{ + "method": "POST", + "path": "/api/2.0/secrets/scopes/delete", + "body": { + "scope": "main.myschema.scope" + } +} + +>>> musterr [CLI] secrets list-secrets main.myschema.scope +Error: Scope main.myschema.scope does not exist diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/script b/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/script new file mode 100644 index 00000000000..a98d8afe765 --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/script @@ -0,0 +1,9 @@ +title "Deploy a schema and a secret scope whose name starts with the schema's full name, and rename the schema out of band" +trace $CLI bundle deploy +$CLI schemas update main.myschema --new-name moved_schema > /dev/null +print_requests.py //unity-catalog //secrets &> LOG.deploy_requests + +title "The scope is not in the schema: destroy deletes it where it is, and the schema at its new name" +trace $CLI bundle destroy --auto-approve +trace print_requests.py --sort //unity-catalog //secrets +trace musterr $CLI secrets list-secrets main.myschema.scope diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/test.toml b/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/test.toml new file mode 100644 index 00000000000..00fdd96b767 --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/test.toml @@ -0,0 +1,5 @@ +# Local only: names are fixed so that the scope's name starts with the schema's full name. + +Ignore = [ + ".databricks", +] diff --git a/bundle/direct/bundle_plan.go b/bundle/direct/bundle_plan.go index 1aa2b06c2ee..d531492a678 100644 --- a/bundle/direct/bundle_plan.go +++ b/bundle/direct/bundle_plan.go @@ -956,10 +956,15 @@ func (b *DeploymentBundle) findInMovedParents(ctx context.Context, plan *deployp if !ok { continue } - if !entry.Gone && hasRecordedInternalID(adapter, dbentry) { + if !entry.Gone && entry.RemoteState != nil && hasRecordedInternalID(adapter, dbentry) { // Verified at its recorded ID by its internal ID (see findDeleteTarget): it did not move. continue } + if !entry.Gone && !adapter.HasIDFromState() { + // Found at its recorded ID, and its ID is not known to be a UC full name (a secret scope may + // be named main.s.x): it is deleted there. Only a Gone one is looked for by prefix. + continue + } // Any resource whose ID is a full name under the parent's can be in it, with or without // IDFromState (a feature's ID is its full name too). parent, id := underMovedParent(parents, dbentry.ID) From 51d86e463c6a431729f4744fe5074a00e1a60242 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Thu, 26 Nov 2026 17:36:51 +0000 Subject: [PATCH 11/22] direct: a with_parent child keeps only its edge to its parent It sends no request, so its other delete edges are not needed; one to a sibling in the same parent closed a cycle through the parent. aws: TestAccept/bundle/resources/schemas/foreign_at_recorded_name TestAccept/bundle/resources/schemas/destroy_after_rename_out_of_band TestAccept/bundle/resources/schemas/recreated_at_recorded_name TestAccept/bundle/resources/schemas/destroy_renamed_with_model_and_secret TestAccept/bundle/resources/volumes/foreign_at_recorded_name TestAccept/bundle/resources/volumes/prevent_destroy_in_renamed_schema TestAccept/bundle/resources/volumes/removed_from_renamed_schema TestAccept/bundle/resources/volumes/prevent_destroy_schema_without_dependency TestAccept/bundle/resources/volumes/prevent_destroy_mixed_case_schema TestAccept/bundle/resources/volumes/select_renamed_schema Co-authored-by: Isaac --- .../volumes/select_renamed_schema/output.txt | 4 +-- .../volumes/select_renamed_schema/script | 2 +- bundle/direct/graph.go | 16 +++++++---- bundle/direct/graph_test.go | 28 +++++++++++++++++++ 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/acceptance/bundle/resources/volumes/select_renamed_schema/output.txt b/acceptance/bundle/resources/volumes/select_renamed_schema/output.txt index 52229a8a6d5..7a3512c51fd 100644 --- a/acceptance/bundle/resources/volumes/select_renamed_schema/output.txt +++ b/acceptance/bundle/resources/volumes/select_renamed_schema/output.txt @@ -54,8 +54,8 @@ Resources: 0 created, 0 changed, 2 deleted, 0 unchanged } } ->>> musterr [CLI] volumes read main.test-schema-renamed-[UNIQUE_NAME].myvolume2 -Error: Volume 'main.test-schema-renamed-[UNIQUE_NAME].myvolume2' does not exist. +>>> musterr [CLI] schemas get main.test-schema-renamed-[UNIQUE_NAME] +Error: Schema 'main.test-schema-renamed-[UNIQUE_NAME]' does not exist. >>> [CLI] bundle summary --output json {} diff --git a/acceptance/bundle/resources/volumes/select_renamed_schema/script b/acceptance/bundle/resources/volumes/select_renamed_schema/script index 170e32cebca..47cdfd6c320 100644 --- a/acceptance/bundle/resources/volumes/select_renamed_schema/script +++ b/acceptance/bundle/resources/volumes/select_renamed_schema/script @@ -25,5 +25,5 @@ print_requests.py //unity-catalog &> LOG.rename_requests title "Selecting only the schema still lists the volume, which its delete takes along" trace $CLI bundle deploy --select schemas.schema1 --auto-approve trace print_requests.py //unity-catalog -trace musterr $CLI volumes read "$NEW_SCHEMA.myvolume2" +trace musterr $CLI schemas get "$NEW_SCHEMA" trace $CLI bundle summary --output json | jq '.resources // {}' diff --git a/bundle/direct/graph.go b/bundle/direct/graph.go index 728547bd62e..9419dcc5e24 100644 --- a/bundle/direct/graph.go +++ b/bundle/direct/graph.go @@ -26,13 +26,19 @@ func makeGraph(plan *deployplan.Plan) (*dagrun.Graph, error) { isDelete := entry.Action == deployplan.Delete + if isDelete && entry.WithParent != "" { + // No delete is sent for it, so nothing has to wait for it: its state is dropped only + // once its parent's delete, which removes it, has succeeded. Its other edges are left + // out, as one to a sibling in the same parent would close a cycle through the parent. + if parent := slices.IndexFunc(entry.DependsOn, func(d deployplan.DependsOnEntry) bool { return d.Node == entry.WithParent }); parent >= 0 && plan.Plan[entry.WithParent] != nil { + g.AddDirectedEdge(entry.WithParent, resourceKey, entry.DependsOn[parent].Label) + } + continue + } + for _, dep := range entry.DependsOn { if _, exists := plan.Plan[dep.Node]; exists { - if isDelete && dep.Node == entry.WithParent { - // No delete is sent for it: its state is dropped only once its parent's - // delete, which removes it, has succeeded. - g.AddDirectedEdge(dep.Node, resourceKey, dep.Label) - } else if isDelete { + if isDelete { g.AddDirectedEdge(resourceKey, dep.Node, dep.Label) } else { g.AddDirectedEdge(dep.Node, resourceKey, dep.Label) diff --git a/bundle/direct/graph_test.go b/bundle/direct/graph_test.go index 1fc5b7cf552..73c304946d9 100644 --- a/bundle/direct/graph_test.go +++ b/bundle/direct/graph_test.go @@ -50,3 +50,31 @@ func TestMakeGraphDeletesWithParentAfterItsParent(t *testing.T) { } } } + +func TestMakeGraphWithParentReferencingASibling(t *testing.T) { + const ( + schemaKey = "resources.schemas.schema1" + volume1Key = "resources.volumes.volume1" + volume2Key = "resources.volumes.volume2" + ) + plan := deployplan.NewPlanDirect() + plan.Plan[schemaKey] = &deployplan.PlanEntry{Action: deployplan.Delete, ID: "main.moved"} + plan.Plan[volume1Key] = &deployplan.PlanEntry{Action: deployplan.Delete, ID: "main.moved.v1", DependsOn: []deployplan.DependsOnEntry{{Node: schemaKey}}} + // volume2 names its schema through volume1: deleted before volume1 as usual, it would close a + // cycle through the schema it goes with. + plan.Plan[volume2Key] = &deployplan.PlanEntry{ + Action: deployplan.Delete, Gone: true, WithParent: schemaKey, + DependsOn: []deployplan.DependsOnEntry{{Node: volume1Key}, {Node: schemaKey}}, + } + + g, err := makeGraph(plan) + require.NoError(t, err) + require.NoError(t, g.DetectCycle()) + + var order []string + g.Run(1, func(key string, failedDependency *string) bool { + order = append(order, key) + return true + }) + assert.Equal(t, []string{volume1Key, schemaKey, volume2Key}, order) +} From 4d0a3bbb28f88214a355801c8971f3f2dfaa0eea Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Thu, 26 Nov 2026 18:09:38 +0000 Subject: [PATCH 12/22] acc: drop a trailing space in prevent_destroy_mixed_case_schema aws: TestAccept/bundle/resources/schemas/foreign_at_recorded_name TestAccept/bundle/resources/schemas/destroy_after_rename_out_of_band TestAccept/bundle/resources/schemas/recreated_at_recorded_name TestAccept/bundle/resources/schemas/destroy_renamed_with_model_and_secret TestAccept/bundle/resources/volumes/foreign_at_recorded_name TestAccept/bundle/resources/volumes/prevent_destroy_in_renamed_schema TestAccept/bundle/resources/volumes/removed_from_renamed_schema TestAccept/bundle/resources/volumes/prevent_destroy_schema_without_dependency TestAccept/bundle/resources/volumes/prevent_destroy_mixed_case_schema TestAccept/bundle/resources/volumes/select_renamed_schema Co-authored-by: Isaac --- .../resources/volumes/prevent_destroy_mixed_case_schema/script | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/script b/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/script index 728d76a760b..b174ffe2ded 100644 --- a/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/script +++ b/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/script @@ -20,7 +20,7 @@ title "Rename the schema in the config to a mixed-case name. UC stores it in low trace update_file.py databricks.yml "name: test-schema-" "name: Test-Schema-Mixed-" trace $CLI bundle deploy --auto-approve print_requests.py //unity-catalog &> LOG.mixed_requests -trace $CLI bundle summary --output json | jq '.resources | map_values(map_values(.id))' +trace $CLI bundle summary --output json | jq '.resources | map_values(map_values(.id))' title "Rename the schema out of band. UC moves the volume with it" trace $CLI schemas update "main.test-schema-mixed-${UNIQUE_NAME}" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields From 7b284f5c890889abcc5bf98413ab1f0b9a168609 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Thu, 26 Nov 2026 20:56:57 +0000 Subject: [PATCH 13/22] direct: limit the moved-parent lookup to UC children; order and select with_parent deletes with their parent - Only resources with parent_id_fields or moves_with_parent (features, feature specs) are looked for under a renamed parent, so a secret scope named like a schema is never re-targeted. - A delete that depends on a with_parent entry also waits for, and blocks, its parent's delete. - --select of a with_parent entry without its parent is an error instead of pulling in a forced parent delete. - One advice sentence for both "another object" warnings; comment fixes. - Tests: shared helpers in acceptance/bundle/resources/script.prepare; fault.py instead of [[Server]] stubs. aws: TestAccept/bundle/resources/schemas/foreign_at_recorded_name TestAccept/bundle/resources/schemas/destroy_after_rename_out_of_band TestAccept/bundle/resources/schemas/recreated_at_recorded_name TestAccept/bundle/resources/schemas/destroy_renamed_with_model_and_secret TestAccept/bundle/resources/volumes/foreign_at_recorded_name TestAccept/bundle/resources/volumes/prevent_destroy_in_renamed_schema TestAccept/bundle/resources/volumes/removed_from_renamed_schema TestAccept/bundle/resources/volumes/prevent_destroy_schema_without_dependency TestAccept/bundle/resources/volumes/prevent_destroy_mixed_case_schema TestAccept/bundle/resources/volumes/select_renamed_schema Co-authored-by: Isaac --- .../script | 4 +- .../schemas/destroy_search_fails/output.txt | 6 +-- .../schemas/destroy_search_fails/script | 3 +- .../schemas/destroy_search_fails/test.toml | 6 +-- .../foreign_at_recorded_name/output.txt | 4 +- .../recreated_at_recorded_name/output.txt | 4 +- .../databricks-initial.yml | 11 +++++ .../databricks-renamed.yml | 8 ++++ .../out.test.toml | 2 + .../output.txt | 34 ++++++++++++++++ .../script | 17 ++++++++ .../test.toml | 6 +++ .../bundle/resources/schemas/script.prepare | 6 --- acceptance/bundle/resources/script.prepare | 17 ++++++++ .../foreign_at_recorded_name/output.txt | 2 +- .../output.txt | 2 +- .../prevent_destroy_mixed_case_schema/script | 2 +- .../bundle/resources/volumes/script.prepare | 11 ----- .../volumes/select_renamed_schema/output.txt | 14 ++++++- .../volumes/select_renamed_schema/script | 6 ++- .../with_parent_delete_fails/output.txt | 6 +-- .../volumes/with_parent_delete_fails/script | 3 +- .../with_parent_delete_fails/test.toml | 6 +-- bundle/deployplan/plan.go | 25 +++++++----- bundle/deployplan/plan_filter_test.go | 34 ++++++++++------ bundle/direct/bundle_plan.go | 36 +++++++++-------- bundle/direct/dresources/README.md | 2 +- bundle/direct/dresources/config.go | 7 ++++ .../dresources/configs/feature_specs.yml | 2 + bundle/direct/dresources/configs/features.yml | 2 + bundle/direct/graph.go | 6 +++ bundle/direct/graph_test.go | 40 +++++++++++++++++++ 32 files changed, 247 insertions(+), 87 deletions(-) create mode 100644 acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/databricks-initial.yml create mode 100644 acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/databricks-renamed.yml create mode 100644 acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/out.test.toml create mode 100644 acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/output.txt create mode 100644 acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/script create mode 100644 acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/test.toml delete mode 100644 acceptance/bundle/resources/schemas/script.prepare create mode 100644 acceptance/bundle/resources/script.prepare delete mode 100644 acceptance/bundle/resources/volumes/script.prepare diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/script b/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/script index 222384f1d9f..8f032853d06 100644 --- a/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/script +++ b/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/script @@ -22,11 +22,11 @@ add_repl "$($CLI schemas get "$NEW_SCHEMA" | jq -r .schema_id)" SCHEMA_ID title "Out of band, delete the secret, and create another schema under the old name with a model of the same name" trace $CLI secrets-uc delete-secret "$NEW_SCHEMA.mysecret" trace $CLI schemas create "test-schema-${UNIQUE_NAME}" main | schema_fields -trace $CLI registered-models create --catalog-name main --schema-name "test-schema-${UNIQUE_NAME}" --name mymodel | jq '{full_name}' +trace $CLI registered-models create --catalog-name main --schema-name "test-schema-${UNIQUE_NAME}" --name mymodel | registered_model_fields print_requests.py //unity-catalog &> LOG.oob_requests title "The model has no internal ID to find it by, so destroy lists it as going with the schema it found, and sends a delete only for that schema. The secret is gone, and nothing at the old name is touched" trace $CLI bundle destroy --auto-approve trace print_requests.py //unity-catalog trace musterr $CLI schemas get "$NEW_SCHEMA" -trace $CLI registered-models get "$OLD_SCHEMA.mymodel" | jq '{full_name}' +trace $CLI registered-models get "$OLD_SCHEMA.mymodel" | registered_model_fields diff --git a/acceptance/bundle/resources/schemas/destroy_search_fails/output.txt b/acceptance/bundle/resources/schemas/destroy_search_fails/output.txt index 3e33a5d1058..8cd20a40ff8 100644 --- a/acceptance/bundle/resources/schemas/destroy_search_fails/output.txt +++ b/acceptance/bundle/resources/schemas/destroy_search_fails/output.txt @@ -17,19 +17,19 @@ Resources: 2 created, 0 changed, 0 deleted, 0 unchanged === The search for the renamed schema fails, so destroy fails without deleting anything or dropping the state >>> musterr [CLI] bundle destroy --auto-approve -Error: cannot plan resources.schemas.schema1: looking for schema_id "[UUID]" after a rename outside the bundle: injected (500 INTERNAL_ERROR) +Error: cannot plan resources.schemas.schema1: looking for schema_id "[UUID]" after a rename outside the bundle: Fault injected by test. (500 INTERNAL_ERROR) Endpoint: GET [DATABRICKS_URL]/api/2.1/unity-catalog/schemas?catalog_name=main&max_results=0 HTTP Status: 500 Internal Server Error API error_code: INTERNAL_ERROR -API message: injected +API message: Fault injected by test. Error: planning failed >>> print_requests.py //unity-catalog ->>> [CLI] bundle summary --output json +>>> summary_ids { "schemas": { "schema1": "main.test-schema-[UNIQUE_NAME]" diff --git a/acceptance/bundle/resources/schemas/destroy_search_fails/script b/acceptance/bundle/resources/schemas/destroy_search_fails/script index 2b395f0d731..780e6027756 100644 --- a/acceptance/bundle/resources/schemas/destroy_search_fails/script +++ b/acceptance/bundle/resources/schemas/destroy_search_fails/script @@ -9,7 +9,8 @@ trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE print_requests.py //unity-catalog &> LOG.deploy_requests title "The search for the renamed schema fails, so destroy fails without deleting anything or dropping the state" +fault.py "GET /api/2.1/unity-catalog/schemas" 500 0 100 INTERNAL_ERROR trace musterr $CLI bundle destroy --auto-approve trace print_requests.py //unity-catalog -trace $CLI bundle summary --output json | jq '.resources | map_values(map_values(.id))' +trace summary_ids trace $CLI schemas get "$NEW_SCHEMA" | schema_fields diff --git a/acceptance/bundle/resources/schemas/destroy_search_fails/test.toml b/acceptance/bundle/resources/schemas/destroy_search_fails/test.toml index 673a3cef990..fb72b6dda1b 100644 --- a/acceptance/bundle/resources/schemas/destroy_search_fails/test.toml +++ b/acceptance/bundle/resources/schemas/destroy_search_fails/test.toml @@ -1,12 +1,8 @@ # Local only: the failure of the search for the renamed schema is injected. +Cloud = false Ignore = [ ".databricks", ".gitignore", "databricks.yml", ] - -[[Server]] -Pattern = "GET /api/2.1/unity-catalog/schemas" -Response.StatusCode = 500 -Response.Body = '{"error_code": "INTERNAL_ERROR", "message": "injected"}' diff --git a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt index 46e8fb7a17e..488fca97772 100644 --- a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt +++ b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt @@ -18,7 +18,7 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged === The other schema has another schema_id, so the plan does not take it for the bundle's: it plans a create >>> [CLI] bundle plan --output json -Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it in place. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again. +Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it in place. If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle, or manage it again with 'bundle deployment bind' while it is in the config ('bundle deployment unbind' first if the state still has it). { "resources.schemas.schema1": { @@ -44,7 +44,7 @@ Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds an >>> musterr [CLI] bundle deploy Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... -Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it in place. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again. +Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it in place. If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle, or manage it again with 'bundle deployment bind' while it is in the config ('bundle deployment unbind' first if the state still has it). Error: cannot create resources.schemas.schema1: Schema 'test-schema-[UNIQUE_NAME]' already exists (400 SCHEMA_ALREADY_EXISTS) diff --git a/acceptance/bundle/resources/schemas/recreated_at_recorded_name/output.txt b/acceptance/bundle/resources/schemas/recreated_at_recorded_name/output.txt index 87e8c756a0e..139e1a4fcd2 100644 --- a/acceptance/bundle/resources/schemas/recreated_at_recorded_name/output.txt +++ b/acceptance/bundle/resources/schemas/recreated_at_recorded_name/output.txt @@ -14,7 +14,7 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged === The new schema has another schema_id: the bundle does not take it for its own, and deploy fails on its name >>> [CLI] bundle plan -Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[NEW_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it in place. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again. +Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[NEW_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it in place. If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle, or manage it again with 'bundle deployment bind' while it is in the config ('bundle deployment unbind' first if the state still has it). create schemas.schema1 @@ -22,7 +22,7 @@ Plan: 1 to add, 0 to change, 0 to delete, 0 unchanged >>> musterr [CLI] bundle deploy Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... -Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[NEW_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it in place. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again. +Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[NEW_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it in place. If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle, or manage it again with 'bundle deployment bind' while it is in the config ('bundle deployment unbind' first if the state still has it). Error: cannot create resources.schemas.schema1: Schema 'test-schema-[UNIQUE_NAME]' already exists (400 SCHEMA_ALREADY_EXISTS) diff --git a/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/databricks-initial.yml b/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/databricks-initial.yml new file mode 100644 index 00000000000..bd70982d43e --- /dev/null +++ b/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/databricks-initial.yml @@ -0,0 +1,11 @@ +bundle: + name: test-bundle + +resources: + schemas: + schema1: + catalog_name: main + name: myschema + secret_scopes: + scope1: + name: main.myschema.scope diff --git a/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/databricks-renamed.yml b/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/databricks-renamed.yml new file mode 100644 index 00000000000..9db36f92729 --- /dev/null +++ b/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/databricks-renamed.yml @@ -0,0 +1,8 @@ +bundle: + name: test-bundle + +resources: + schemas: + schema1: + catalog_name: main + name: moved_schema diff --git a/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/out.test.toml b/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/out.test.toml new file mode 100644 index 00000000000..a927a5fbc06 --- /dev/null +++ b/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/output.txt b/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/output.txt new file mode 100644 index 00000000000..572b2eed059 --- /dev/null +++ b/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/output.txt @@ -0,0 +1,34 @@ + +=== Deploy a schema and a secret scope whose name starts with the schema's full name + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files... +Created schemas.schema1 +Created secret_scopes.scope1 +Created secret_scopes.scope1.permissions +Files: 3 uploaded, 0 deleted +Resources: 3 created, 0 changed, 0 deleted, 0 unchanged + +=== Out of band: rename the schema, delete the scope, and create another scope named like the renamed schema + +>>> [CLI] schemas update main.myschema --new-name moved_schema +{ + "full_name": "main.moved_schema", + "schema_id": "[UUID]" +} + +>>> [CLI] secrets delete-scope main.myschema.scope + +>>> [CLI] secrets create-scope main.moved_schema.scope + +=== The config follows the rename and drops the scope: the other scope is not the bundle's, and is left alone + +>>> [CLI] bundle deploy --auto-approve +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files... +Files: 3 uploaded, 0 deleted +Resources: 0 created, 0 changed, 0 deleted, 1 unchanged + +>>> print_requests.py --sort //unity-catalog //secrets + +>>> [CLI] secrets list-scopes -o json +["main.moved_schema.scope"] diff --git a/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/script b/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/script new file mode 100644 index 00000000000..e9d77095c08 --- /dev/null +++ b/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/script @@ -0,0 +1,17 @@ +cp databricks-initial.yml databricks.yml + +title "Deploy a schema and a secret scope whose name starts with the schema's full name" +trace $CLI bundle deploy +print_requests.py //unity-catalog //secrets &> LOG.deploy_requests + +title "Out of band: rename the schema, delete the scope, and create another scope named like the renamed schema" +trace $CLI schemas update main.myschema --new-name moved_schema | schema_fields +trace $CLI secrets delete-scope main.myschema.scope +trace $CLI secrets create-scope main.moved_schema.scope +print_requests.py //unity-catalog //secrets &> LOG.out_of_band_requests + +title "The config follows the rename and drops the scope: the other scope is not the bundle's, and is left alone" +cp databricks-renamed.yml databricks.yml +trace $CLI bundle deploy --auto-approve +trace print_requests.py --sort //unity-catalog //secrets +trace $CLI secrets list-scopes -o json | jq -c '[.[].name] | sort' diff --git a/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/test.toml b/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/test.toml new file mode 100644 index 00000000000..9af916a3791 --- /dev/null +++ b/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/test.toml @@ -0,0 +1,6 @@ +# Local only: names are fixed so that the scope's name starts with the schema's full name. + +Ignore = [ + ".databricks", + "databricks.yml", +] diff --git a/acceptance/bundle/resources/schemas/script.prepare b/acceptance/bundle/resources/schemas/script.prepare deleted file mode 100644 index 4332cddbd8c..00000000000 --- a/acceptance/bundle/resources/schemas/script.prepare +++ /dev/null @@ -1,6 +0,0 @@ -# schema_fields is the allow-list of schema fields the acceptance tests assert on. Anything the -# backend returns outside this list is dropped, so a new field in the API response does not break -# the golden. -schema_fields() { - jq '{full_name, schema_id} | with_entries(select(.value != null))' -} diff --git a/acceptance/bundle/resources/script.prepare b/acceptance/bundle/resources/script.prepare new file mode 100644 index 00000000000..9e4a02d228a --- /dev/null +++ b/acceptance/bundle/resources/script.prepare @@ -0,0 +1,17 @@ +# The *_fields helpers are the allow-lists of fields the acceptance tests assert on. Anything the +# backend returns outside a list is dropped, so a new field in the API response does not break the +# golden. +schema_fields() { + jq '{full_name, schema_id} | with_entries(select(.value != null))' +} +volume_fields() { + jq '{full_name, volume_id} | with_entries(select(.value != null))' +} +registered_model_fields() { + jq '{full_name}' +} + +# summary_ids prints the ID of each resource in 'bundle summary', by group and key. +summary_ids() { + $CLI bundle summary --output json | jq '.resources // {} | map_values(map_values(.id))' +} diff --git a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt index ae7b2f481dd..f7cf70383ca 100644 --- a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt +++ b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt @@ -18,7 +18,7 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged === The other volume has another volume_id, so the plan does not take it for the bundle's: it plans a create >>> [CLI] bundle plan -Warning: resources.volumes.volume1: id="main.test-schema-[UNIQUE_NAME].myvolume" holds another object: its volume_id is "[FOREIGN_VOLUME_ID]", not "[VOLUME_ID]" as recorded, so this bundle leaves it in place. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again. +Warning: resources.volumes.volume1: id="main.test-schema-[UNIQUE_NAME].myvolume" holds another object: its volume_id is "[FOREIGN_VOLUME_ID]", not "[VOLUME_ID]" as recorded, so this bundle leaves it in place. If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle, or manage it again with 'bundle deployment bind' while it is in the config ('bundle deployment unbind' first if the state still has it). create volumes.volume1 diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/output.txt b/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/output.txt index 784fd07007c..159a35ce0b9 100644 --- a/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/output.txt +++ b/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/output.txt @@ -18,7 +18,7 @@ Updated schemas.schema1 Files: 2 uploaded, 0 deleted Resources: 0 created, 1 changed, 0 deleted, 1 unchanged ->>> [CLI] bundle summary --output json +>>> summary_ids { "schemas": { "schema1": "main.test-schema-mixed-[UNIQUE_NAME]" diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/script b/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/script index b174ffe2ded..cfbd4067e9a 100644 --- a/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/script +++ b/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/script @@ -20,7 +20,7 @@ title "Rename the schema in the config to a mixed-case name. UC stores it in low trace update_file.py databricks.yml "name: test-schema-" "name: Test-Schema-Mixed-" trace $CLI bundle deploy --auto-approve print_requests.py //unity-catalog &> LOG.mixed_requests -trace $CLI bundle summary --output json | jq '.resources | map_values(map_values(.id))' +trace summary_ids title "Rename the schema out of band. UC moves the volume with it" trace $CLI schemas update "main.test-schema-mixed-${UNIQUE_NAME}" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields diff --git a/acceptance/bundle/resources/volumes/script.prepare b/acceptance/bundle/resources/volumes/script.prepare deleted file mode 100644 index 6f1a8f182ad..00000000000 --- a/acceptance/bundle/resources/volumes/script.prepare +++ /dev/null @@ -1,11 +0,0 @@ -# volume_fields is the allow-list of volume fields the acceptance tests assert on. Anything the -# backend returns outside this list is dropped, so a new field in the API response does not break -# the golden. -volume_fields() { - jq '{full_name, volume_id} | with_entries(select(.value != null))' -} - -# schema_fields is the same allow-list for the schemas these tests create and rename. -schema_fields() { - jq '{full_name, schema_id} | with_entries(select(.value != null))' -} diff --git a/acceptance/bundle/resources/volumes/select_renamed_schema/output.txt b/acceptance/bundle/resources/volumes/select_renamed_schema/output.txt index 7a3512c51fd..eef064bddb5 100644 --- a/acceptance/bundle/resources/volumes/select_renamed_schema/output.txt +++ b/acceptance/bundle/resources/volumes/select_renamed_schema/output.txt @@ -27,6 +27,16 @@ Resources: 1 created, 0 changed, 0 deleted, 1 unchanged "volume_id": "[UUID][1]" } +=== Selecting only the volume fails: only the schema's delete removes it, with everything in it + +>>> musterr [CLI] bundle deploy --select volumes.volume1 --auto-approve +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Error: cannot delete volumes.volume1 on its own: only deleting schemas.schema1, which was renamed outside the bundle, removes it; also select schemas.schema1 + +Files: 3 uploaded, 0 deleted + +>>> print_requests.py //unity-catalog + === Selecting only the schema still lists the volume, which its delete takes along >>> [CLI] bundle deploy --select schemas.schema1 --auto-approve @@ -42,7 +52,7 @@ is removed from the catalog, but the underlying files are not deleted: delete resources.volumes.volume1 Deleted schemas.schema1 Deleted volumes.volume1 -Files: 3 uploaded, 0 deleted +Files: 0 uploaded, 0 deleted Resources: 0 created, 0 changed, 2 deleted, 0 unchanged >>> print_requests.py //unity-catalog @@ -57,5 +67,5 @@ Resources: 0 created, 0 changed, 2 deleted, 0 unchanged >>> musterr [CLI] schemas get main.test-schema-renamed-[UNIQUE_NAME] Error: Schema 'main.test-schema-renamed-[UNIQUE_NAME]' does not exist. ->>> [CLI] bundle summary --output json +>>> summary_ids {} diff --git a/acceptance/bundle/resources/volumes/select_renamed_schema/script b/acceptance/bundle/resources/volumes/select_renamed_schema/script index 47cdfd6c320..e92ee072fd8 100644 --- a/acceptance/bundle/resources/volumes/select_renamed_schema/script +++ b/acceptance/bundle/resources/volumes/select_renamed_schema/script @@ -22,8 +22,12 @@ trace $CLI volumes update "$NEW_SCHEMA.myvolume" --new-name myvolume2 | volume_f envsubst < databricks-empty.yml.tmpl > databricks.yml print_requests.py //unity-catalog &> LOG.rename_requests +title "Selecting only the volume fails: only the schema's delete removes it, with everything in it" +trace musterr $CLI bundle deploy --select volumes.volume1 --auto-approve +trace print_requests.py //unity-catalog + title "Selecting only the schema still lists the volume, which its delete takes along" trace $CLI bundle deploy --select schemas.schema1 --auto-approve trace print_requests.py //unity-catalog trace musterr $CLI schemas get "$NEW_SCHEMA" -trace $CLI bundle summary --output json | jq '.resources // {}' +trace summary_ids diff --git a/acceptance/bundle/resources/volumes/with_parent_delete_fails/output.txt b/acceptance/bundle/resources/volumes/with_parent_delete_fails/output.txt index 4bb994bfd5e..ec92ba2caa7 100644 --- a/acceptance/bundle/resources/volumes/with_parent_delete_fails/output.txt +++ b/acceptance/bundle/resources/volumes/with_parent_delete_fails/output.txt @@ -38,12 +38,12 @@ is removed from the catalog, but the underlying files are not deleted: All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default -Error: cannot delete resources.schemas.schema1: deleting id=main.test-schema-renamed-[UNIQUE_NAME]: injected (400 INVALID_PARAMETER_VALUE) +Error: cannot delete resources.schemas.schema1: deleting id=main.test-schema-renamed-[UNIQUE_NAME]: Fault injected by test. (400 INVALID_PARAMETER_VALUE) Endpoint: DELETE [DATABRICKS_URL]/api/2.1/unity-catalog/schemas/main.test-schema-renamed-[UNIQUE_NAME]?force=true HTTP Status: 400 Bad Request API error_code: INVALID_PARAMETER_VALUE -API message: injected +API message: Fault injected by test. Error: cannot delete resources.volumes.volume1: dependency failed: resources.schemas.schema1 @@ -57,7 +57,7 @@ Error: cannot delete resources.volumes.volume1: dependency failed: resources.sch } } ->>> [CLI] bundle summary --output json +>>> summary_ids { "schemas": { "schema1": "main.test-schema-[UNIQUE_NAME]" diff --git a/acceptance/bundle/resources/volumes/with_parent_delete_fails/script b/acceptance/bundle/resources/volumes/with_parent_delete_fails/script index 7b97e2a90e7..efdeedf467d 100644 --- a/acceptance/bundle/resources/volumes/with_parent_delete_fails/script +++ b/acceptance/bundle/resources/volumes/with_parent_delete_fails/script @@ -10,6 +10,7 @@ trace $CLI volumes update "$NEW_SCHEMA.myvolume" --new-name myvolume2 | volume_f print_requests.py //unity-catalog &> LOG.deploy_requests title "The volume goes with the schema's delete, which fails: the volume stays in the state with the schema" +fault.py "DELETE /api/2.1/unity-catalog/schemas/*" 400 0 1 INVALID_PARAMETER_VALUE trace musterr $CLI bundle destroy --auto-approve trace print_requests.py //unity-catalog -trace $CLI bundle summary --output json | jq '.resources | map_values(map_values(.id))' +trace summary_ids diff --git a/acceptance/bundle/resources/volumes/with_parent_delete_fails/test.toml b/acceptance/bundle/resources/volumes/with_parent_delete_fails/test.toml index f135f75f4a0..4545cc36529 100644 --- a/acceptance/bundle/resources/volumes/with_parent_delete_fails/test.toml +++ b/acceptance/bundle/resources/volumes/with_parent_delete_fails/test.toml @@ -1,12 +1,8 @@ # Local only: the failure of the schema's delete is injected. +Cloud = false Ignore = [ ".databricks", ".gitignore", "databricks.yml", ] - -[[Server]] -Pattern = "DELETE /api/2.1/unity-catalog/schemas/{full_name}" -Response.StatusCode = 400 -Response.Body = '{"error_code": "INVALID_PARAMETER_VALUE", "message": "injected"}' diff --git a/bundle/deployplan/plan.go b/bundle/deployplan/plan.go index b85b2c4ce82..9b258007854 100644 --- a/bundle/deployplan/plan.go +++ b/bundle/deployplan/plan.go @@ -131,13 +131,13 @@ type PlanEntry struct { ID string `json:"id,omitempty"` DependsOn []DependsOnEntry `json:"depends_on,omitempty"` Action ActionType `json:"action,omitempty"` - // Gone is set on Delete entries when planning did not find the resource remotely: a read at its - // ID returned nothing (for a resource with an internal ID: found neither at its ID, where another - // object may be, nor by its internal ID, nor under a renamed parent; see findInMovedParents; a - // search the user may not run finds nothing too), or the state entry has an empty ID (left by a recreate in an older CLI - // whose create step failed after the delete). Applying such an entry only removes it from the - // state, without calling the delete API, and approval prompts do not list it as a deletion - // unless WithParent is set. + // Gone is set on Delete entries when planning did not find the resource remotely, or the state + // entry has an empty ID (left by a recreate in an older CLI whose create step failed after the + // delete). Applying such an entry only removes it from the state, without calling the delete + // API, and approval prompts do not list it as a deletion unless WithParent is set. + // A resource with an internal ID is not found when it is not at its recorded ID (another object + // may be there), not found by findByInternalID (a search the user may not run finds nothing), + // and not under a renamed parent (see findInMovedParents). Gone bool `json:"gone,omitempty"` // WithParent is set on a Gone Delete entry whose resource is inside a parent this plan deletes // at the new ID a rename out of band gave it (a volume in a renamed schema), but could not be @@ -394,6 +394,8 @@ func ResolveSelectors(resourceKeys iter.Seq[string], selectors []string) ([]stri // dependencies (forward), a deleted one its dependents (reverse) so they are removed // together. A delete selection errors if a live resource (not itself being deleted) // still references the resource to be deleted; removing it would break the live resource. +// It also errors for a WithParent delete whose parent is not selected, as the parent's +// delete is the only one that removes it. func (p *Plan) FilterToSelected(_ context.Context, selected []string) error { before := len(p.Plan) @@ -433,9 +435,12 @@ func (p *Plan) FilterToSelected(_ context.Context, selected []string) error { key := queue[0] queue = queue[1:] if p.Plan[key].Action == Delete { - if parent := p.Plan[key].WithParent; parent != "" { - // Its parent's delete is what removes it. - p.enqueueReachable(reachable, &queue, parent) + if parent := p.Plan[key].WithParent; parent != "" && !slices.Contains(resolved, parent) { + // Only its parent's delete removes it, and that takes everything in the parent along: + // the user has to ask for it. + deleted := strings.TrimPrefix(key, "resources.") + parentName := strings.TrimPrefix(parent, "resources.") + errs = append(errs, fmt.Sprintf("cannot delete %s on its own: only deleting %s, which was renamed outside the bundle, removes it; also select %s", deleted, parentName, parentName)) } for _, dependent := range dependents[key] { // Sub-nodes (grants/permissions, 4-part key) are not user-facing dependents diff --git a/bundle/deployplan/plan_filter_test.go b/bundle/deployplan/plan_filter_test.go index f19cc927c3c..b516d2fb312 100644 --- a/bundle/deployplan/plan_filter_test.go +++ b/bundle/deployplan/plan_filter_test.go @@ -1,6 +1,7 @@ package deployplan_test import ( + "maps" "slices" "testing" @@ -284,17 +285,24 @@ func TestFilterToSelected_DeletionLeavesDependencies(t *testing.T) { assert.NotContains(t, p.Plan, "resources.jobs.lib") } -func TestFilterToSelected_WithParentBringsItsParent(t *testing.T) { - p := deployplan.NewPlanDirect() - schemaDep := deployplan.DependsOnEntry{Node: "resources.schemas.s"} - p.Plan["resources.schemas.s"] = &deployplan.PlanEntry{Action: deployplan.Delete, ID: "main.moved"} - p.Plan["resources.volumes.v"] = &deployplan.PlanEntry{Action: deployplan.Delete, Gone: true, WithParent: "resources.schemas.s", DependsOn: []deployplan.DependsOnEntry{schemaDep}} - p.Plan["resources.volumes.other"] = &deployplan.PlanEntry{Action: deployplan.Delete} - - // Its parent's delete is what removes it, so selecting it brings the parent, and the parent brings - // it back. - require.NoError(t, p.FilterToSelected(t.Context(), []string{"volumes.v"})) - assert.Contains(t, p.Plan, "resources.schemas.s") - assert.Contains(t, p.Plan, "resources.volumes.v") - assert.NotContains(t, p.Plan, "resources.volumes.other") +func TestFilterToSelected_WithParentNeedsItsParentSelected(t *testing.T) { + newPlan := func() *deployplan.Plan { + p := deployplan.NewPlanDirect() + schemaDep := deployplan.DependsOnEntry{Node: "resources.schemas.s"} + p.Plan["resources.schemas.s"] = &deployplan.PlanEntry{Action: deployplan.Delete, ID: "main.moved"} + p.Plan["resources.volumes.v"] = &deployplan.PlanEntry{Action: deployplan.Delete, Gone: true, WithParent: "resources.schemas.s", DependsOn: []deployplan.DependsOnEntry{schemaDep}} + p.Plan["resources.volumes.w"] = &deployplan.PlanEntry{Action: deployplan.Delete, DependsOn: []deployplan.DependsOnEntry{schemaDep}} + p.Plan["resources.volumes.other"] = &deployplan.PlanEntry{Action: deployplan.Delete} + return p + } + + // Only the parent's delete removes it, and that one takes w along too: the user has to ask for it. + p := newPlan() + err := p.FilterToSelected(t.Context(), []string{"volumes.v"}) + require.EqualError(t, err, "cannot delete volumes.v on its own: only deleting schemas.s, which was renamed outside the bundle, removes it; also select schemas.s") + assert.Len(t, p.Plan, 4) + + p = newPlan() + require.NoError(t, p.FilterToSelected(t.Context(), []string{"volumes.v", "schemas.s"})) + assert.ElementsMatch(t, []string{"resources.schemas.s", "resources.volumes.v", "resources.volumes.w"}, slices.Collect(maps.Keys(p.Plan))) } diff --git a/bundle/direct/bundle_plan.go b/bundle/direct/bundle_plan.go index d531492a678..0ad01f9d744 100644 --- a/bundle/direct/bundle_plan.go +++ b/bundle/direct/bundle_plan.go @@ -389,7 +389,7 @@ func (b *DeploymentBundle) CalculatePlan(ctx context.Context, client *databricks } } if otherID != "" && remoteState == nil { - logdiag.LogDiag(ctx, diag.Warningf("%s: %s. If it is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and 'bundle deployment bind' to manage it again.", + logdiag.LogDiag(ctx, diag.Warningf("%s: %s. "+otherObjectAdvice, resourceKey, otherObjectMessage(adapter, dbentry.ID, savedState, otherID))[0]) } @@ -920,13 +920,13 @@ func isParentIDField(adapter *dresources.Adapter, path *structpath.PathNode) boo // findInMovedParents handles a Delete of a resource not found at its recorded ID (Gone) while its // parent was found at another ID in this plan: renamed out of band (see findMoved and -// findByInternalID). The rename moved the resource with the parent, so it is looked for under the -// parent's new ID, and deleted there as itself if it has its recorded internal ID. When the parent is -// deleted and the resource is there but cannot be verified (it has no internal ID: a model, a secret, -// a feature), or may be there (it has one, and a 404 cannot tell a renamed resource from a deleted -// one), it is marked WithParent: no delete is sent for it, but approval and lifecycle.prevent_destroy -// see that the parent's delete takes it along. A resource without an internal ID that is not there -// stays Gone. +// findByInternalID). Only a resource that lives in a UC parent (ParentIDFields, MovesWithParent) +// moves with it, so it is looked for under the parent's new ID, and deleted there as itself if it has +// its recorded internal ID. When the parent is deleted and the resource is there but cannot be +// verified (it has no internal ID: a model, a secret, a feature), or may be there (it has one, and a +// 404 cannot tell a renamed resource from a deleted one), it is marked WithParent: no delete is sent +// for it, but approval and lifecycle.prevent_destroy see that the parent's delete takes it along. A +// resource without an internal ID that is not there stays Gone. func (b *DeploymentBundle) findInMovedParents(ctx context.Context, plan *deployplan.Plan) error { parents, err := b.movedParents(plan) if err != nil || len(parents) == 0 { @@ -960,13 +960,12 @@ func (b *DeploymentBundle) findInMovedParents(ctx context.Context, plan *deployp // Verified at its recorded ID by its internal ID (see findDeleteTarget): it did not move. continue } - if !entry.Gone && !adapter.HasIDFromState() { - // Found at its recorded ID, and its ID is not known to be a UC full name (a secret scope may - // be named main.s.x): it is deleted there. Only a Gone one is looked for by prefix. + if config := adapter.ResourceConfig(); len(config.ParentIDFields) == 0 && !config.MovesWithParent { + // Not a resource that lives in a UC parent and moves with it: an ID that looks like a full + // name under the parent's (a secret scope named main.s.x) says nothing about where it is, + // found or Gone. continue } - // Any resource whose ID is a full name under the parent's can be in it, with or without - // IDFromState (a feature's ID is its full name too). parent, id := underMovedParent(parents, dbentry.ID) if parent == nil { continue @@ -1218,13 +1217,17 @@ func otherInternalID(adapter *dresources.Adapter, savedState, remote any) (strin // otherObjectMessage says that what is at the recorded ID id is not the bundle's: the plan leaves it // alone. It is told when the bundle's own is not found elsewhere either; a resource deleted and -// created again out of band looks the same, hence the advice the callers add. +// created again out of band looks the same, hence otherObjectAdvice. func otherObjectMessage(adapter *dresources.Adapter, id string, savedState any, otherID string) string { saved, _ := adapter.InternalID(savedState) return fmt.Sprintf("id=%q holds another object: its %s is %q, not %q as recorded, so this bundle leaves it in place", id, adapter.ResourceConfig().InternalIDField, otherID, saved) } +// otherObjectAdvice follows otherObjectMessage, for a resource still in the config (deploy) as well as +// one removed from it (a Delete): bind needs the resource in the config, and a free state entry. +const otherObjectAdvice = "If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle, or manage it again with 'bundle deployment bind' while it is in the config ('bundle deployment unbind' first if the state still has it)." + // warnOtherObjects warns about each Delete that leaves another object in place at its recorded ID // (see findDeleteTarget), after findInMovedParents had its chance to find the bundle's own. func (b *DeploymentBundle) warnOtherObjects(ctx context.Context, plan *deployplan.Plan) { @@ -1233,7 +1236,7 @@ func (b *DeploymentBundle) warnOtherObjects(ctx context.Context, plan *deploypla if !ok || plan.Plan[resourceKey].ID != "" { continue } - logdiag.LogDiag(ctx, diag.Warningf("%s: %s, and drops %s from the deployment state. If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle, or once the state entry is dropped, bind it again with 'bundle deployment bind' and destroy.", + logdiag.LogDiag(ctx, diag.Warningf("%s: %s, and drops %s from the deployment state. "+otherObjectAdvice, resourceKey, message, resourceKey)[0]) } } @@ -1293,7 +1296,8 @@ func (b *DeploymentBundle) findDeleteTarget(ctx context.Context, resourceKey str // band. A plan without config (destroy), or without this resource in it, has no new name to look at, // as findMoved does, so the resource searches for it (DoFindByInternalID). As in findMoved, what is // found is taken only if its internal ID matches. A search the user has no permission for finds -// nothing, and the delete only drops the state entry, as it did before; any other failure fails the +// nothing: the delete then goes to the recorded ID if the read there was forbidden too (see +// findDeleteTarget), else only drops the state entry, as it did before. Any other failure fails the // plan, so the state keeps the record of the resource and the command can be run again. func findByInternalID(ctx context.Context, adapter *dresources.Adapter, savedState any) (string, any, error) { if !adapter.HasFindByInternalID() { diff --git a/bundle/direct/dresources/README.md b/bundle/direct/dresources/README.md index 5bc04d79706..f82b7fce402 100644 --- a/bundle/direct/dresources/README.md +++ b/bundle/direct/dresources/README.md @@ -62,7 +62,7 @@ If the ID is built from the resource's own fields (a UC full name), implement `I If the backend also has its own ID that a rename keeps (a volume's `volume_id`), add it to the state type, copy it in `RemapState`, and name it in the config as `internal_id_field`. The framework records it on every save and skips its changes in the plan. When nothing is at the recorded ID, the plan reads at the ID the config gives the resource and takes what it finds only if the internal ID matches. This covers a rename done out of band, or by a deploy that stopped before re-keying the resource. -An object at the recorded ID with a different internal ID is someone else's (ours was renamed away and another created under its name): the plan treats ours as not there, so it never updates or deletes the other object. A resource to be deleted (by destroy, or removed from config) has no config to give its new name, so to delete one renamed out of band the resource needs `DoFindByInternalID`: it searches the parent (lists the schemas in the catalog, the volumes in the schema) for the recorded internal ID. Without it, the delete only drops the state entry of a resource not found at its recorded ID. A child whose parent the plan found at a new ID (a volume in a renamed schema, matched by full-name prefix) is looked for under the parent's new ID and deleted there as itself if its internal ID matches. If it cannot be verified there and the parent is deleted, no delete is sent for it, but the plan lists it as going with the parent (`with_parent`), so approval and its `prevent_destroy` still apply. A parent outside the bundle that was renamed is not followed. The search is a listing: one list of the schemas in the catalog (or the volumes in the schema) per resource not found at its recorded ID, so a destroy in a catalog with many schemas pages through all of them. +An object at the recorded ID with a different internal ID is someone else's (ours was renamed away and another created under its name): the plan treats ours as not there, so it never updates or deletes the other object. A resource to be deleted (by destroy, or removed from config) has no config to give its new name, so to delete one renamed out of band the resource needs `DoFindByInternalID`: it searches the parent (lists the schemas in the catalog, the volumes in the schema) for the recorded internal ID. Without it, the delete only drops the state entry of a resource not found at its recorded ID. A child whose parent the plan found at a new ID (a volume in a renamed schema, matched by full-name prefix; only a resource that lives in its UC parent: it has `parent_id_fields`, or `moves_with_parent` for a feature) is looked for under the parent's new ID and deleted there as itself if its internal ID matches. If it cannot be verified there and the parent is deleted, no delete is sent for it, but the plan lists it as going with the parent (`with_parent`), so approval and its `prevent_destroy` still apply, and `--select` of it alone fails: the parent has to be selected too. A parent outside the bundle that was renamed is not followed. The search is a listing: one list of the schemas in the catalog (or the volumes in the schema) per resource not found at its recorded ID, so a destroy in a catalog with many schemas pages through all of them. ## Empty states: IsEmptyState diff --git a/bundle/direct/dresources/config.go b/bundle/direct/dresources/config.go index 1e55636c57f..e1ff6370f03 100644 --- a/bundle/direct/dresources/config.go +++ b/bundle/direct/dresources/config.go @@ -145,6 +145,12 @@ type ResourceLifecycleConfig struct { // resource's own and never follows it. Requires IDFromState. ParentIDFields []FieldRule `yaml:"parent_id_fields,omitempty"` + // MovesWithParent: the resource lives in a UC schema and moves with it on a rename, but its + // parent is only part of its full-name ID, so it has no ParentIDFields (a feature). A Delete of + // it is looked for under a parent renamed out of band, as for a resource with ParentIDFields + // (see findInMovedParents). + MovesWithParent bool `yaml:"moves_with_parent,omitempty"` + // NormalizeSlash: string field patterns the UC API strips trailing slashes from. // A change is skipped when local and remote differ only by trailing slashes. NormalizeSlash []FieldRule `yaml:"normalize_slash,omitempty"` @@ -220,6 +226,7 @@ var empty = ResourceLifecycleConfig{ ProvidedIDFields: nil, UpdatableIDFields: nil, ParentIDFields: nil, + MovesWithParent: false, NormalizeSlash: nil, IgnoreRemoteAdditions: nil, BackendDefaults: nil, diff --git a/bundle/direct/dresources/configs/feature_specs.yml b/bundle/direct/dresources/configs/feature_specs.yml index 48edeb6e434..8f2af9e7679 100644 --- a/bundle/direct/dresources/configs/feature_specs.yml +++ b/bundle/direct/dresources/configs/feature_specs.yml @@ -2,3 +2,5 @@ provided_id_fields: # UC lowercases the name; remote returns "main.ml.myspec" for config "main.ml.MySpec". - field: name reason: id_field +# Inside its UC schema: a rename of the schema moves it, and its ID with it. +moves_with_parent: true diff --git a/bundle/direct/dresources/configs/features.yml b/bundle/direct/dresources/configs/features.yml index e8c72ce50d1..877d3cc883b 100644 --- a/bundle/direct/dresources/configs/features.yml +++ b/bundle/direct/dresources/configs/features.yml @@ -6,3 +6,5 @@ stable_output_fields: # name is the last component of full_name, which is immutable, so it cannot change on an # in-place update; see the stable_output_fields doc in config.go and ES-2202624. - field: name +# Inside its UC schema: a rename of the schema moves it, and its ID with it. +moves_with_parent: true diff --git a/bundle/direct/graph.go b/bundle/direct/graph.go index 9419dcc5e24..6f90be827b6 100644 --- a/bundle/direct/graph.go +++ b/bundle/direct/graph.go @@ -40,6 +40,12 @@ func makeGraph(plan *deployplan.Plan) (*dagrun.Graph, error) { if _, exists := plan.Plan[dep.Node]; exists { if isDelete { g.AddDirectedEdge(resourceKey, dep.Node, dep.Label) + // The delete that removes a WithParent dependency is its parent's, so that one waits + // too, and does not run if this one fails, as for a dependency deleted on its own. + if parent := plan.Plan[dep.Node].WithParent; parent != "" && parent != resourceKey && plan.Plan[parent] != nil && + !slices.ContainsFunc(entry.DependsOn, func(d deployplan.DependsOnEntry) bool { return d.Node == parent }) { + g.AddDirectedEdge(resourceKey, parent, dep.Label) + } } else { g.AddDirectedEdge(dep.Node, resourceKey, dep.Label) } diff --git a/bundle/direct/graph_test.go b/bundle/direct/graph_test.go index 73c304946d9..b8fa0786a2f 100644 --- a/bundle/direct/graph_test.go +++ b/bundle/direct/graph_test.go @@ -78,3 +78,43 @@ func TestMakeGraphWithParentReferencingASibling(t *testing.T) { }) assert.Equal(t, []string{volume1Key, schemaKey, volume2Key}, order) } + +func TestMakeGraphDependentOfWithParentWaitsForItsParent(t *testing.T) { + const ( + schemaKey = "resources.schemas.schema1" + volumeKey = "resources.volumes.volume1" + jobKey = "resources.jobs.job1" + ) + plan := deployplan.NewPlanDirect() + plan.Plan[schemaKey] = &deployplan.PlanEntry{Action: deployplan.Delete, ID: "main.moved"} + plan.Plan[volumeKey] = &deployplan.PlanEntry{Action: deployplan.Delete, Gone: true, WithParent: schemaKey, DependsOn: []deployplan.DependsOnEntry{{Node: schemaKey}}} + // The job names only the volume, whose removal is the schema's delete. + plan.Plan[jobKey] = &deployplan.PlanEntry{Action: deployplan.Delete, DependsOn: []deployplan.DependsOnEntry{{Node: volumeKey}}} + + for _, jobFails := range []bool{false, true} { + g, err := makeGraph(plan) + require.NoError(t, err) + require.NoError(t, g.DetectCycle()) + + var mu sync.Mutex + var order []string + failed := map[string]bool{} + g.Run(1, func(key string, failedDependency *string) bool { + mu.Lock() + defer mu.Unlock() + if failedDependency != nil { + failed[key] = true + return false + } + order = append(order, key) + return !jobFails || key != jobKey + }) + + if jobFails { + assert.Equal(t, []string{jobKey}, order) + assert.Equal(t, map[string]bool{schemaKey: true, volumeKey: true}, failed) + } else { + assert.Equal(t, []string{jobKey, schemaKey, volumeKey}, order) + } + } +} From d9feb5a6fab4b2460565da0693137f87e51ce73f Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Thu, 26 Nov 2026 22:57:53 +0000 Subject: [PATCH 14/22] direct: check a with_parent entry's parent against the whole selection; its parent's delete waits for its dependencies - FilterToSelected checks a with_parent entry's parent against the closure once the walk is done, so a parent pulled in as a dependent (a schema under a selected catalog) counts. - makeGraph moves a with_parent entry's edges to nodes outside its parent onto the parent's delete, unless that would close a cycle (a sibling deleted before the parent). - Tests: LOG.rename instead of /dev/null; a secret_scope_names helper. aws: TestAccept/bundle/resources/schemas/foreign_at_recorded_name TestAccept/bundle/resources/schemas/destroy_after_rename_out_of_band TestAccept/bundle/resources/schemas/recreated_at_recorded_name TestAccept/bundle/resources/schemas/destroy_renamed_with_model_and_secret TestAccept/bundle/resources/volumes/foreign_at_recorded_name TestAccept/bundle/resources/volumes/prevent_destroy_in_renamed_schema TestAccept/bundle/resources/volumes/removed_from_renamed_schema TestAccept/bundle/resources/volumes/prevent_destroy_schema_without_dependency TestAccept/bundle/resources/volumes/prevent_destroy_mixed_case_schema TestAccept/bundle/resources/volumes/select_renamed_schema Co-authored-by: Isaac --- .../destroy_forbidden_at_recorded_name/script | 2 +- .../script | 2 +- .../script | 2 +- acceptance/bundle/resources/script.prepare | 4 ++ bundle/deployplan/plan.go | 22 +++++---- bundle/deployplan/plan_filter_test.go | 13 +++++ bundle/direct/graph.go | 48 ++++++++++++++++++- bundle/direct/graph_test.go | 43 +++++++++++++++++ 8 files changed, 122 insertions(+), 14 deletions(-) diff --git a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/script b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/script index 43b1b4a3da8..9b51cea469b 100644 --- a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/script +++ b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/script @@ -5,7 +5,7 @@ NEW_SCHEMA="main.moved_schema" title "Deploy a schema with a volume in it and rename the schema out of band; the recorded names are not readable" trace $CLI bundle deploy -$CLI schemas update "$OLD_SCHEMA" --new-name moved_schema > /dev/null +$CLI schemas update "$OLD_SCHEMA" --new-name moved_schema &> LOG.rename print_requests.py //unity-catalog &> LOG.deploy_requests title "Destroy finds the renamed schema by its schema_id, and the volume in it by its volume_id, and deletes both there" diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/script b/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/script index a98d8afe765..b535c128fe5 100644 --- a/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/script +++ b/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/script @@ -1,6 +1,6 @@ title "Deploy a schema and a secret scope whose name starts with the schema's full name, and rename the schema out of band" trace $CLI bundle deploy -$CLI schemas update main.myschema --new-name moved_schema > /dev/null +$CLI schemas update main.myschema --new-name moved_schema &> LOG.rename print_requests.py //unity-catalog //secrets &> LOG.deploy_requests title "The scope is not in the schema: destroy deletes it where it is, and the schema at its new name" diff --git a/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/script b/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/script index e9d77095c08..1eddc39d2ac 100644 --- a/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/script +++ b/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/script @@ -14,4 +14,4 @@ title "The config follows the rename and drops the scope: the other scope is not cp databricks-renamed.yml databricks.yml trace $CLI bundle deploy --auto-approve trace print_requests.py --sort //unity-catalog //secrets -trace $CLI secrets list-scopes -o json | jq -c '[.[].name] | sort' +trace $CLI secrets list-scopes -o json | secret_scope_names diff --git a/acceptance/bundle/resources/script.prepare b/acceptance/bundle/resources/script.prepare index 9e4a02d228a..3bb0c380ada 100644 --- a/acceptance/bundle/resources/script.prepare +++ b/acceptance/bundle/resources/script.prepare @@ -10,6 +10,10 @@ volume_fields() { registered_model_fields() { jq '{full_name}' } +# secret_scope_names prints the sorted names from 'secrets list-scopes -o json' on one line. +secret_scope_names() { + jq -c '[.[].name] | sort' +} # summary_ids prints the ID of each resource in 'bundle summary', by group and key. summary_ids() { diff --git a/bundle/deployplan/plan.go b/bundle/deployplan/plan.go index 9b258007854..8e1983b9981 100644 --- a/bundle/deployplan/plan.go +++ b/bundle/deployplan/plan.go @@ -394,8 +394,8 @@ func ResolveSelectors(resourceKeys iter.Seq[string], selectors []string) ([]stri // dependencies (forward), a deleted one its dependents (reverse) so they are removed // together. A delete selection errors if a live resource (not itself being deleted) // still references the resource to be deleted; removing it would break the live resource. -// It also errors for a WithParent delete whose parent is not selected, as the parent's -// delete is the only one that removes it. +// It also errors for a WithParent delete whose parent is not in the closure, as the +// parent's delete is the only one that removes it. func (p *Plan) FilterToSelected(_ context.Context, selected []string) error { before := len(p.Plan) @@ -435,13 +435,6 @@ func (p *Plan) FilterToSelected(_ context.Context, selected []string) error { key := queue[0] queue = queue[1:] if p.Plan[key].Action == Delete { - if parent := p.Plan[key].WithParent; parent != "" && !slices.Contains(resolved, parent) { - // Only its parent's delete removes it, and that takes everything in the parent along: - // the user has to ask for it. - deleted := strings.TrimPrefix(key, "resources.") - parentName := strings.TrimPrefix(parent, "resources.") - errs = append(errs, fmt.Sprintf("cannot delete %s on its own: only deleting %s, which was renamed outside the bundle, removes it; also select %s", deleted, parentName, parentName)) - } for _, dependent := range dependents[key] { // Sub-nodes (grants/permissions, 4-part key) are not user-facing dependents // and never trigger the live-dependent error. If they are themselves being @@ -469,6 +462,17 @@ func (p *Plan) FilterToSelected(_ context.Context, selected []string) error { } } } + for key := range reachable { + // Only its parent's delete removes it, and that takes everything in the parent along: the + // parent has to be in the selection already (named, or pulled in as a dependent). + if parent := p.Plan[key].WithParent; p.Plan[key].Action == Delete && parent != "" { + if _, ok := reachable[parent]; !ok { + deleted := strings.TrimPrefix(key, "resources.") + parentName := strings.TrimPrefix(parent, "resources.") + errs = append(errs, fmt.Sprintf("cannot delete %s on its own: only deleting %s, which was renamed outside the bundle, removes it; also select %s", deleted, parentName, parentName)) + } + } + } if len(errs) > 0 { slices.Sort(errs) return errors.New(strings.Join(errs, "\n")) diff --git a/bundle/deployplan/plan_filter_test.go b/bundle/deployplan/plan_filter_test.go index b516d2fb312..603ff64117a 100644 --- a/bundle/deployplan/plan_filter_test.go +++ b/bundle/deployplan/plan_filter_test.go @@ -306,3 +306,16 @@ func TestFilterToSelected_WithParentNeedsItsParentSelected(t *testing.T) { require.NoError(t, p.FilterToSelected(t.Context(), []string{"volumes.v", "schemas.s"})) assert.ElementsMatch(t, []string{"resources.schemas.s", "resources.volumes.v", "resources.volumes.w"}, slices.Collect(maps.Keys(p.Plan))) } + +func TestFilterToSelected_WithParentWhoseParentIsPulledIn(t *testing.T) { + p := deployplan.NewPlanDirect() + p.Plan["resources.catalogs.c"] = &deployplan.PlanEntry{Action: deployplan.Delete} + p.Plan["resources.schemas.s"] = &deployplan.PlanEntry{Action: deployplan.Delete, ID: "c.moved", DependsOn: []deployplan.DependsOnEntry{{Node: "resources.catalogs.c"}}} + p.Plan["resources.volumes.v"] = &deployplan.PlanEntry{Action: deployplan.Delete, Gone: true, WithParent: "resources.schemas.s", DependsOn: []deployplan.DependsOnEntry{{Node: "resources.schemas.s"}}} + p.Plan["resources.volumes.other"] = &deployplan.PlanEntry{Action: deployplan.Delete} + + // The catalog pulls in the schema as its dependent, and the schema the volume: the schema is + // deleted in this run, so the volume can go with it. + require.NoError(t, p.FilterToSelected(t.Context(), []string{"catalogs.c"})) + assert.ElementsMatch(t, []string{"resources.catalogs.c", "resources.schemas.s", "resources.volumes.v"}, slices.Collect(maps.Keys(p.Plan))) +} diff --git a/bundle/direct/graph.go b/bundle/direct/graph.go index 6f90be827b6..d2c13d02344 100644 --- a/bundle/direct/graph.go +++ b/bundle/direct/graph.go @@ -1,6 +1,7 @@ package direct import ( + "cmp" "fmt" "maps" "slices" @@ -17,6 +18,9 @@ func makeGraph(plan *deployplan.Plan) (*dagrun.Graph, error) { g.AddNode(resourceKey) } + // The dependencies of a WithParent entry, which its parent's delete removes. + var moved []movedEdge + // Add edges based on depends_on field. // For deletions, reverse direction so children are deleted before parents. for resourceKey, entry := range plan.Plan { @@ -28,10 +32,15 @@ func makeGraph(plan *deployplan.Plan) (*dagrun.Graph, error) { if isDelete && entry.WithParent != "" { // No delete is sent for it, so nothing has to wait for it: its state is dropped only - // once its parent's delete, which removes it, has succeeded. Its other edges are left - // out, as one to a sibling in the same parent would close a cycle through the parent. + // once its parent's delete, which removes it, has succeeded. Its other dependencies must + // outlive that delete instead (see below). if parent := slices.IndexFunc(entry.DependsOn, func(d deployplan.DependsOnEntry) bool { return d.Node == entry.WithParent }); parent >= 0 && plan.Plan[entry.WithParent] != nil { g.AddDirectedEdge(entry.WithParent, resourceKey, entry.DependsOn[parent].Label) + for _, dep := range entry.DependsOn { + if dep.Node != entry.WithParent && plan.Plan[dep.Node] != nil { + moved = append(moved, movedEdge{from: entry.WithParent, to: dep.Node, label: dep.Label}) + } + } } continue } @@ -55,5 +64,40 @@ func makeGraph(plan *deployplan.Plan) (*dagrun.Graph, error) { } } + // The parent's delete waits for what its WithParent child depends on (an external location a + // volume's storage_location names), as the child's own delete would. A dependency that is + // itself deleted before the parent (a sibling in it) keeps its order: the edge would close a + // cycle through the parent. + slices.SortFunc(moved, func(x, y movedEdge) int { return cmp.Or(cmp.Compare(x.from, y.from), cmp.Compare(x.to, y.to)) }) + for _, e := range moved { + if !reaches(g, e.to, e.from) { + g.AddDirectedEdge(e.from, e.to, e.label) + } + } + return g, nil } + +type movedEdge struct { + from, to, label string +} + +// reaches reports whether there is a path from one node to another in g. +func reaches(g *dagrun.Graph, from, to string) bool { + seen := map[string]bool{from: true} + stack := []string{from} + for len(stack) > 0 { + n := stack[len(stack)-1] + stack = stack[:len(stack)-1] + if n == to { + return true + } + for _, e := range g.Adj[n] { + if !seen[e.To] { + seen[e.To] = true + stack = append(stack, e.To) + } + } + } + return false +} diff --git a/bundle/direct/graph_test.go b/bundle/direct/graph_test.go index b8fa0786a2f..500c22f2438 100644 --- a/bundle/direct/graph_test.go +++ b/bundle/direct/graph_test.go @@ -118,3 +118,46 @@ func TestMakeGraphDependentOfWithParentWaitsForItsParent(t *testing.T) { } } } + +func TestMakeGraphParentOfWithParentWaitsForItsDependencies(t *testing.T) { + const ( + locationKey = "resources.external_locations.loc" + schemaKey = "resources.schemas.schema1" + volumeKey = "resources.volumes.volume1" + ) + plan := deployplan.NewPlanDirect() + plan.Plan[locationKey] = &deployplan.PlanEntry{Action: deployplan.Delete} + plan.Plan[schemaKey] = &deployplan.PlanEntry{Action: deployplan.Delete, ID: "main.moved"} + // An external volume whose storage_location names the location: the schema's delete removes it. + plan.Plan[volumeKey] = &deployplan.PlanEntry{ + Action: deployplan.Delete, Gone: true, WithParent: schemaKey, + DependsOn: []deployplan.DependsOnEntry{{Node: schemaKey}, {Node: locationKey}}, + } + + for _, schemaFails := range []bool{false, true} { + g, err := makeGraph(plan) + require.NoError(t, err) + require.NoError(t, g.DetectCycle()) + + var mu sync.Mutex + var order []string + failed := map[string]bool{} + g.Run(1, func(key string, failedDependency *string) bool { + mu.Lock() + defer mu.Unlock() + if failedDependency != nil { + failed[key] = true + return false + } + order = append(order, key) + return !schemaFails || key != schemaKey + }) + + if schemaFails { + assert.Equal(t, []string{schemaKey}, order) + assert.Equal(t, map[string]bool{locationKey: true, volumeKey: true}, failed) + } else { + assert.Equal(t, []string{schemaKey, volumeKey, locationKey}, order) + } + } +} From 8c8cc5e1f7c2578943e0619a7ec65811626d9478 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Fri, 27 Nov 2026 02:09:19 +0000 Subject: [PATCH 15/22] direct: list every unverified child of a deleted moved parent with it - A child under a parent deleted at a moved ID that is not verified there gets with_parent, also on a 404 and without an internal ID: a 404 cannot tell a model renamed too from one deleted. - The search warning says what was searched; the Action.Gone comment matches PlanEntry.Gone. - Tests: registered_models/prevent_destroy_in_renamed_schema, registered_models/removed_from_renamed_schema; fault.py instead of [[Server]] stubs in destroy_forbidden_at_recorded_name. aws: TestAccept/bundle/resources/schemas/foreign_at_recorded_name TestAccept/bundle/resources/schemas/destroy_after_rename_out_of_band TestAccept/bundle/resources/schemas/recreated_at_recorded_name TestAccept/bundle/resources/schemas/destroy_renamed_with_model_and_secret TestAccept/bundle/resources/volumes/foreign_at_recorded_name TestAccept/bundle/resources/volumes/prevent_destroy_in_renamed_schema TestAccept/bundle/resources/volumes/removed_from_renamed_schema TestAccept/bundle/resources/volumes/prevent_destroy_schema_without_dependency TestAccept/bundle/resources/volumes/prevent_destroy_mixed_case_schema TestAccept/bundle/resources/volumes/select_renamed_schema TestAccept/bundle/resources/registered_models/prevent_destroy_in_renamed_schema TestAccept/bundle/resources/registered_models/removed_from_renamed_schema Co-authored-by: Isaac --- .../databricks.yml.tmpl | 15 +++++ .../out.test.toml | 2 + .../output.txt | 35 +++++++++++ .../prevent_destroy_in_renamed_schema/script | 27 ++++++++ .../test.toml | 7 +++ .../databricks-renamed-without-model.yml.tmpl | 8 +++ .../databricks.yml.tmpl | 13 ++++ .../removed_from_renamed_schema/out.test.toml | 2 + .../removed_from_renamed_schema/output.txt | 56 +++++++++++++++++ .../removed_from_renamed_schema/script | 27 ++++++++ .../removed_from_renamed_schema/test.toml | 7 +++ .../output.txt | 4 +- .../destroy_forbidden_at_recorded_name/script | 3 + .../test.toml | 11 +--- .../output.txt | 3 +- .../schemas/destroy_search_fails/output.txt | 2 +- bundle/deployplan/action.go | 6 +- bundle/direct/bundle_plan.go | 63 +++++++++++-------- 18 files changed, 250 insertions(+), 41 deletions(-) create mode 100644 acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/databricks.yml.tmpl create mode 100644 acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/out.test.toml create mode 100644 acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/output.txt create mode 100644 acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/script create mode 100644 acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/test.toml create mode 100644 acceptance/bundle/resources/registered_models/removed_from_renamed_schema/databricks-renamed-without-model.yml.tmpl create mode 100644 acceptance/bundle/resources/registered_models/removed_from_renamed_schema/databricks.yml.tmpl create mode 100644 acceptance/bundle/resources/registered_models/removed_from_renamed_schema/out.test.toml create mode 100644 acceptance/bundle/resources/registered_models/removed_from_renamed_schema/output.txt create mode 100644 acceptance/bundle/resources/registered_models/removed_from_renamed_schema/script create mode 100644 acceptance/bundle/resources/registered_models/removed_from_renamed_schema/test.toml diff --git a/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/databricks.yml.tmpl b/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/databricks.yml.tmpl new file mode 100644 index 00000000000..46f6f3d1ec4 --- /dev/null +++ b/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/databricks.yml.tmpl @@ -0,0 +1,15 @@ +bundle: + name: test-bundle-$UNIQUE_NAME + +resources: + schemas: + schema1: + catalog_name: main + name: test-schema-$UNIQUE_NAME + registered_models: + model1: + catalog_name: main + schema_name: ${resources.schemas.schema1.name} + name: mymodel + lifecycle: + prevent_destroy: true diff --git a/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/out.test.toml b/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/out.test.toml new file mode 100644 index 00000000000..0f167effef1 --- /dev/null +++ b/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/out.test.toml @@ -0,0 +1,2 @@ +Cloud = true +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/output.txt b/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/output.txt new file mode 100644 index 00000000000..7ce131f2422 --- /dev/null +++ b/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/output.txt @@ -0,0 +1,35 @@ + +=== Deploy a schema with a registered model in it that must not be destroyed + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Created registered_models.model1 +Created schemas.schema1 +Files: 2 uploaded, 0 deleted +Resources: 2 created, 0 changed, 0 deleted, 0 unchanged + +=== Rename the schema and the model out of band + +>>> [CLI] schemas update main.test-schema-[UNIQUE_NAME] --new-name test-schema-renamed-[UNIQUE_NAME] +{ + "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", + "schema_id": "[UUID]" +} + +>>> [CLI] registered-models update main.test-schema-renamed-[UNIQUE_NAME].mymodel --new-name mymodel2 +{ + "full_name": "main.test-schema-renamed-[UNIQUE_NAME].mymodel2" +} + +=== The model has no internal ID and is not at its new path, but the schema's delete would take it, so destroy lists it and prevent_destroy stops it + +>>> musterr [CLI] bundle destroy --auto-approve +Error: resources.registered_models.model1 has lifecycle.prevent_destroy set, but the plan calls for this resource to be recreated or destroyed. To avoid this error, disable lifecycle.prevent_destroy for resources.registered_models.model1 + + +>>> print_requests.py //unity-catalog + +>>> [CLI] registered-models get main.test-schema-renamed-[UNIQUE_NAME].mymodel2 +{ + "full_name": "main.test-schema-renamed-[UNIQUE_NAME].mymodel2" +} diff --git a/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/script b/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/script new file mode 100644 index 00000000000..75333bacf15 --- /dev/null +++ b/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/script @@ -0,0 +1,27 @@ +envsubst < databricks.yml.tmpl > databricks.yml + +OLD_SCHEMA="main.test-schema-${UNIQUE_NAME}" +NEW_SCHEMA="main.test-schema-renamed-${UNIQUE_NAME}" + +cleanup() { + errcode $CLI schemas delete "$NEW_SCHEMA" --force > LOG.cleanup 2>&1 + errcode $CLI schemas delete "$OLD_SCHEMA" --force >> LOG.cleanup 2>&1 + + # Consume the destroy's requests so none are left recorded. + print_requests.py //unity-catalog &> LOG.destroy_requests +} +trap cleanup EXIT + +title "Deploy a schema with a registered model in it that must not be destroyed" +trace $CLI bundle deploy +print_requests.py //unity-catalog &> LOG.deploy_requests + +title "Rename the schema and the model out of band" +trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields +trace $CLI registered-models update "$NEW_SCHEMA.mymodel" --new-name mymodel2 | registered_model_fields +print_requests.py //unity-catalog &> LOG.rename_requests + +title "The model has no internal ID and is not at its new path, but the schema's delete would take it, so destroy lists it and prevent_destroy stops it" +trace musterr $CLI bundle destroy --auto-approve +trace print_requests.py //unity-catalog +trace $CLI registered-models get "$NEW_SCHEMA.mymodel2" | registered_model_fields diff --git a/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/test.toml b/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/test.toml new file mode 100644 index 00000000000..4272a80cf61 --- /dev/null +++ b/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/test.toml @@ -0,0 +1,7 @@ +Cloud = true + +Ignore = [ + ".databricks", + ".gitignore", + "databricks.yml", +] diff --git a/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/databricks-renamed-without-model.yml.tmpl b/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/databricks-renamed-without-model.yml.tmpl new file mode 100644 index 00000000000..911125f013b --- /dev/null +++ b/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/databricks-renamed-without-model.yml.tmpl @@ -0,0 +1,8 @@ +bundle: + name: test-bundle-$UNIQUE_NAME + +resources: + schemas: + schema1: + catalog_name: main + name: test-schema-renamed-$UNIQUE_NAME diff --git a/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/databricks.yml.tmpl b/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/databricks.yml.tmpl new file mode 100644 index 00000000000..630cfc75a4a --- /dev/null +++ b/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/databricks.yml.tmpl @@ -0,0 +1,13 @@ +bundle: + name: test-bundle-$UNIQUE_NAME + +resources: + schemas: + schema1: + catalog_name: main + name: test-schema-$UNIQUE_NAME + registered_models: + model1: + catalog_name: main + schema_name: ${resources.schemas.schema1.name} + name: mymodel diff --git a/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/out.test.toml b/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/out.test.toml new file mode 100644 index 00000000000..0f167effef1 --- /dev/null +++ b/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/out.test.toml @@ -0,0 +1,2 @@ +Cloud = true +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/output.txt b/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/output.txt new file mode 100644 index 00000000000..1596484a9c2 --- /dev/null +++ b/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/output.txt @@ -0,0 +1,56 @@ + +=== Deploy a schema with a registered model in it, and rename the schema out of band + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Created registered_models.model1 +Created schemas.schema1 +Files: 3 uploaded, 0 deleted +Resources: 2 created, 0 changed, 0 deleted, 0 unchanged + +>>> [CLI] schemas update main.test-schema-[UNIQUE_NAME] --new-name test-schema-renamed-[UNIQUE_NAME] +{ + "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", + "schema_id": "[UUID]" +} + +=== Follow the rename in the config and remove the model from it: the model has no internal ID, but is found at its new path inside the bundle's schema + +>>> [CLI] bundle plan +delete registered_models.model1 + +Plan: 0 to add, 0 to change, 1 to delete, 1 unchanged + +=== Deploy deletes the model at its new path, and keeps the schema + +>>> [CLI] bundle deploy --auto-approve +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Deleted registered_models.model1 +Files: 2 uploaded, 0 deleted +Resources: 0 created, 0 changed, 1 deleted, 1 unchanged + +>>> print_requests.py //unity-catalog +{ + "method": "DELETE", + "path": "/api/2.1/unity-catalog/models/main.test-schema-renamed-[UNIQUE_NAME].mymodel" +} + +>>> musterr [CLI] registered-models get main.test-schema-renamed-[UNIQUE_NAME].mymodel +Error: Routine or Model 'main.test-schema-renamed-[UNIQUE_NAME].mymodel' does not exist. + +>>> [CLI] schemas get main.test-schema-renamed-[UNIQUE_NAME] +{ + "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", + "schema_id": "[UUID]" +} + +>>> [CLI] bundle destroy --auto-approve +The following resources will be deleted: + delete resources.schemas.schema1 + +This action will result in the deletion of the following UC schemas. Any underlying data may be lost: + delete resources.schemas.schema1 + +All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default + +Destroy: 1 deleted diff --git a/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/script b/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/script new file mode 100644 index 00000000000..3ec1600a0df --- /dev/null +++ b/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/script @@ -0,0 +1,27 @@ +envsubst < databricks.yml.tmpl > databricks.yml + +OLD_SCHEMA="main.test-schema-${UNIQUE_NAME}" +NEW_SCHEMA="main.test-schema-renamed-${UNIQUE_NAME}" + +cleanup() { + trace $CLI bundle destroy --auto-approve + + # Consume the destroy's requests so none are left recorded. + print_requests.py //unity-catalog &> LOG.destroy_requests +} +trap cleanup EXIT + +title "Deploy a schema with a registered model in it, and rename the schema out of band" +trace $CLI bundle deploy +trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields +print_requests.py //unity-catalog &> LOG.deploy_requests + +title "Follow the rename in the config and remove the model from it: the model has no internal ID, but is found at its new path inside the bundle's schema" +envsubst < databricks-renamed-without-model.yml.tmpl > databricks.yml +trace $CLI bundle plan + +title "Deploy deletes the model at its new path, and keeps the schema" +trace $CLI bundle deploy --auto-approve +trace print_requests.py //unity-catalog +trace musterr $CLI registered-models get "$NEW_SCHEMA.mymodel" +trace $CLI schemas get "$NEW_SCHEMA" | schema_fields diff --git a/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/test.toml b/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/test.toml new file mode 100644 index 00000000000..4272a80cf61 --- /dev/null +++ b/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/test.toml @@ -0,0 +1,7 @@ +Cloud = true + +Ignore = [ + ".databricks", + ".gitignore", + "databricks.yml", +] diff --git a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/output.txt b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/output.txt index 9062926de43..3870129a336 100644 --- a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/output.txt +++ b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/output.txt @@ -11,8 +11,8 @@ Resources: 2 created, 0 changed, 0 deleted, 0 unchanged === Destroy finds the renamed schema by its schema_id, and the volume in it by its volume_id, and deletes both there >>> [CLI] bundle destroy --auto-approve -Warn: planning resources.volumes.volume1: reading resources.volumes.volume1 id="main.myschema.myvolume": User does not have USE SCHEMA on the schema -Warn: planning resources.schemas.schema1: reading resources.schemas.schema1 id="main.myschema": User does not have USE SCHEMA on the schema +Warn: planning resources.volumes.volume1: reading resources.volumes.volume1 id="main.myschema.myvolume": Fault injected by test. +Warn: planning resources.schemas.schema1: reading resources.schemas.schema1 id="main.myschema": Fault injected by test. The following resources will be deleted: delete resources.schemas.schema1 delete resources.volumes.volume1 diff --git a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/script b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/script index 9b51cea469b..ec86594ceb9 100644 --- a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/script +++ b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/script @@ -8,6 +8,9 @@ trace $CLI bundle deploy $CLI schemas update "$OLD_SCHEMA" --new-name moved_schema &> LOG.rename print_requests.py //unity-catalog &> LOG.deploy_requests +fault.py "GET /api/2.1/unity-catalog/schemas/main.myschema" 403 0 100 PERMISSION_DENIED +fault.py "GET /api/2.1/unity-catalog/volumes/main.myschema.myvolume" 403 0 100 PERMISSION_DENIED + title "Destroy finds the renamed schema by its schema_id, and the volume in it by its volume_id, and deletes both there" trace $CLI bundle destroy --auto-approve trace print_requests.py //unity-catalog diff --git a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/test.toml b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/test.toml index 626afeea1eb..28b4988ce94 100644 --- a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/test.toml +++ b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/test.toml @@ -1,18 +1,9 @@ # Local only: the 403 at the recorded name is injected, as for another user's schema the # deployer cannot read. +Cloud = false Ignore = [ ".databricks", ".gitignore", "databricks.yml", ] - -[[Server]] -Pattern = "GET /api/2.1/unity-catalog/schemas/main.myschema" -Response.StatusCode = 403 -Response.Body = '{"error_code": "PERMISSION_DENIED", "message": "User does not have USE SCHEMA on the schema"}' - -[[Server]] -Pattern = "GET /api/2.1/unity-catalog/volumes/main.myschema.myvolume" -Response.StatusCode = 403 -Response.Body = '{"error_code": "PERMISSION_DENIED", "message": "User does not have USE SCHEMA on the schema"}' diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/output.txt b/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/output.txt index 62292cf7679..86da7ec8f2a 100644 --- a/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/output.txt +++ b/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/output.txt @@ -36,13 +36,14 @@ Resources: 3 created, 0 changed, 0 deleted, 0 unchanged The following resources will be deleted: delete resources.registered_models.model1 delete resources.schemas.schema1 + delete resources.secrets.secret1 This action will result in the deletion of the following UC schemas. Any underlying data may be lost: delete resources.schemas.schema1 All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default -Destroy: 2 deleted +Destroy: 3 deleted >>> print_requests.py //unity-catalog { diff --git a/acceptance/bundle/resources/schemas/destroy_search_fails/output.txt b/acceptance/bundle/resources/schemas/destroy_search_fails/output.txt index 8cd20a40ff8..2c385761a9e 100644 --- a/acceptance/bundle/resources/schemas/destroy_search_fails/output.txt +++ b/acceptance/bundle/resources/schemas/destroy_search_fails/output.txt @@ -17,7 +17,7 @@ Resources: 2 created, 0 changed, 0 deleted, 0 unchanged === The search for the renamed schema fails, so destroy fails without deleting anything or dropping the state >>> musterr [CLI] bundle destroy --auto-approve -Error: cannot plan resources.schemas.schema1: looking for schema_id "[UUID]" after a rename outside the bundle: Fault injected by test. (500 INTERNAL_ERROR) +Error: cannot plan resources.schemas.schema1: searching "main" for schema_id "[UUID]", as a read at its recorded ID did not return it: Fault injected by test. (500 INTERNAL_ERROR) Endpoint: GET [DATABRICKS_URL]/api/2.1/unity-catalog/schemas?catalog_name=main&max_results=0 HTTP Status: 500 Internal Server Error diff --git a/bundle/deployplan/action.go b/bundle/deployplan/action.go index 27cda9b80e7..0cbcdcde685 100644 --- a/bundle/deployplan/action.go +++ b/bundle/deployplan/action.go @@ -9,8 +9,10 @@ type Action struct { // Full resource key, e.g. "resources.jobs.foo" or "resources.jobs.foo.permissions" ResourceKey string ActionType ActionType - // Gone mirrors PlanEntry.Gone: the delete is a state-only cleanup because the - // resource no longer exists remotely (confirmed by a read, or an empty-ID state entry). + // Gone mirrors PlanEntry.Gone: applying the delete sends no delete call and only removes the + // state entry, as planning did not find the resource remotely (or the state entry has an empty + // ID). With WithParent, the resource is most likely still there until its parent's delete + // removes it, so the action is shown and approved as a deletion. Gone bool // StateOnly mirrors PlanEntry.StateOnly: applying the action only changes the state // (a delete of a resource without DoDelete, or a re-key). diff --git a/bundle/direct/bundle_plan.go b/bundle/direct/bundle_plan.go index 0ad01f9d744..adc488506b8 100644 --- a/bundle/direct/bundle_plan.go +++ b/bundle/direct/bundle_plan.go @@ -921,12 +921,12 @@ func isParentIDField(adapter *dresources.Adapter, path *structpath.PathNode) boo // findInMovedParents handles a Delete of a resource not found at its recorded ID (Gone) while its // parent was found at another ID in this plan: renamed out of band (see findMoved and // findByInternalID). Only a resource that lives in a UC parent (ParentIDFields, MovesWithParent) -// moves with it, so it is looked for under the parent's new ID, and deleted there as itself if it has -// its recorded internal ID. When the parent is deleted and the resource is there but cannot be -// verified (it has no internal ID: a model, a secret, a feature), or may be there (it has one, and a -// 404 cannot tell a renamed resource from a deleted one), it is marked WithParent: no delete is sent -// for it, but approval and lifecycle.prevent_destroy see that the parent's delete takes it along. A -// resource without an internal ID that is not there stays Gone. +// moves with it, so it is looked for under the parent's new ID. It is deleted there as itself if it +// has its recorded internal ID, or, having none to check (a model, a secret, a feature), if it is +// found there inside the bundle's parent, which stays. Otherwise, under a parent that is deleted, it +// is marked WithParent, found or not (a 404 cannot tell a resource renamed too from one deleted): no +// delete is sent for it, but approval and lifecycle.prevent_destroy see that the parent's delete +// takes it along. Under a parent that stays, it stays Gone. func (b *DeploymentBundle) findInMovedParents(ctx context.Context, plan *deployplan.Plan) error { parents, err := b.movedParents(plan) if err != nil || len(parents) == 0 { @@ -970,7 +970,7 @@ func (b *DeploymentBundle) findInMovedParents(ctx context.Context, plan *deployp if parent == nil { continue } - remote, verified, maybeThere, err := b.readUnderMovedParent(ctx, adapter, dbentry, id) + remote, verified, err := b.readUnderMovedParent(ctx, adapter, dbentry, id) if err != nil { return fmt.Errorf("%s: %w", resourceKey, err) } @@ -987,7 +987,10 @@ func (b *DeploymentBundle) findInMovedParents(ctx context.Context, plan *deployp if verified && adapter.HasIDFromState() { parents = append(parents, &movedParent{key: resourceKey, recordedID: dbentry.ID, id: id, deleted: true}) } - case parent.deleted && maybeThere: + case parent.deleted: + // Not verified, or not found: a 404 cannot tell a resource renamed too (or moved by the + // parent's rename and then renamed) from one deleted, with or without an internal ID. The + // parent's delete removes it if it is there, so it is listed with it. entry.WithParent = parent.key default: continue @@ -1050,20 +1053,19 @@ func underMovedParent(parents []*movedParent, id string) (*movedParent, string) } // readUnderMovedParent reads the resource recorded in dbentry at id, under its parent's new ID. It -// returns what it read there (nil if nothing), and whether that is verified: it has the recorded -// internal ID. Else maybeThere reports whether the resource can be there: found, or not readable, or, -// for a resource with an internal ID, not found (a 404 cannot tell a resource renamed too from one -// deleted). A read that fails other than with a missing resource or a permission error fails the +// returns what it read there (nil if nothing, or if the read is forbidden), and whether that is +// verified: it has the recorded internal ID. Something else there (another internal ID) is not +// returned. A read that fails other than with a missing resource or a permission error fails the // plan. -func (b *DeploymentBundle) readUnderMovedParent(ctx context.Context, adapter *dresources.Adapter, dbentry dstate.ResourceEntry, id string) (remote any, verified, maybeThere bool, err error) { +func (b *DeploymentBundle) readUnderMovedParent(ctx context.Context, adapter *dresources.Adapter, dbentry dstate.ResourceEntry, id string) (remote any, verified bool, err error) { savedInternalID := "" if hasRecordedInternalID(adapter, dbentry) { state, err := parseState(adapter.StateType(), dbentry.State) if err != nil { - return nil, false, false, fmt.Errorf("interpreting state: %w", err) + return nil, false, fmt.Errorf("interpreting state: %w", err) } if savedInternalID, err = adapter.InternalID(state); err != nil { - return nil, false, false, err + return nil, false, err } } remote, err = retry.With(ctx, retry.IsTransientRead, func() (any, error) { @@ -1071,21 +1073,18 @@ func (b *DeploymentBundle) readUnderMovedParent(ctx context.Context, adapter *dr }) if err != nil { if apierr.IsMissing(err) { - return nil, false, savedInternalID != "", nil + return nil, false, nil } - return nil, false, true, ignoreMissingOrForbidden(ctx, err, fmt.Sprintf("reading id=%q under its renamed parent", id)) + return nil, false, ignoreMissingOrForbidden(ctx, err, fmt.Sprintf("reading id=%q under its renamed parent", id)) } if savedInternalID == "" { - return remote, false, true, nil + return remote, false, nil } ok, err := hasInternalIDOf(adapter, remote, savedInternalID) - if err != nil { - return nil, false, false, err - } - if !ok { - return nil, false, true, nil + if err != nil || !ok { + return nil, false, err } - return remote, true, true, nil + return remote, true, nil } // hasRecordedInternalID reports whether the state in dbentry has the resource's internal ID. @@ -1311,7 +1310,8 @@ func findByInternalID(ctx context.Context, adapter *dresources.Adapter, savedSta return adapter.DoFindByInternalID(ctx, savedState) }) if err != nil || id == "" { - return "", nil, ignoreMissingOrForbidden(ctx, err, fmt.Sprintf("looking for %s %q after a rename outside the bundle", adapter.ResourceConfig().InternalIDField, savedInternalID)) + return "", nil, ignoreMissingOrForbidden(ctx, err, fmt.Sprintf("searching %sfor %s %q, as a read at its recorded ID did not return it", + searchedParent(adapter, savedState), adapter.ResourceConfig().InternalIDField, savedInternalID)) } remote, err := retry.With(ctx, retry.IsTransientRead, func() (any, error) { return adapter.DoRead(ctx, id) @@ -1325,6 +1325,19 @@ func findByInternalID(ctx context.Context, adapter *dresources.Adapter, savedSta return id, remote, nil } +// searchedParent names, followed by a space, the parent DoFindByInternalID lists: the recorded full +// name without its last part (the catalog of a schema, the schema of a volume). Empty if unknown. +func searchedParent(adapter *dresources.Adapter, savedState any) string { + if !adapter.HasIDFromState() { + return "" + } + recorded, err := adapter.IDFromState(savedState) + if i := strings.LastIndex(recorded, "."); err == nil && i > 0 { + return fmt.Sprintf("%q ", recorded[:i]) + } + return "" +} + // ignoreMissingOrForbidden returns err unless it is nil, a missing resource or a permission error // (logged as a warning). func ignoreMissingOrForbidden(ctx context.Context, err error, what string) error { From b7a7d52b691e04d737061b19fbd74ce2898f7c7c Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Fri, 27 Nov 2026 03:07:23 +0000 Subject: [PATCH 16/22] direct: narrow to the identity check; the destroy lookup and moved parents move to a follow-up Keep: a schema or volume at the recorded name with another schema_id/volume_id is never updated or deleted; deploy falls through to findMoved, a delete drops the state entry and warns. Move to the follow-up: DoFindByInternalID and the list routes, findInMovedParents, with_parent, the makeGraph and --select handling, Destroy at a moved ID, and their tests. destroy_after_rename_out_of_band is back to main, Badness note included. aws: TestAccept/bundle/resources/schemas/foreign_at_recorded_name TestAccept/bundle/resources/schemas/destroy_after_rename_out_of_band TestAccept/bundle/resources/schemas/recreated_at_recorded_name TestAccept/bundle/resources/volumes/foreign_at_recorded_name Co-authored-by: Isaac --- .../bundles/schema-volume-identity.md | 2 +- .../databricks.yml.tmpl | 15 - .../out.test.toml | 2 - .../output.txt | 35 -- .../prevent_destroy_in_renamed_schema/script | 27 -- .../test.toml | 7 - .../databricks-renamed-without-model.yml.tmpl | 8 - .../databricks.yml.tmpl | 13 - .../removed_from_renamed_schema/out.test.toml | 2 - .../removed_from_renamed_schema/output.txt | 56 --- .../removed_from_renamed_schema/script | 27 -- .../removed_from_renamed_schema/test.toml | 7 - .../output.txt | 37 +- .../destroy_after_rename_out_of_band/script | 8 +- .../test.toml | 1 + .../databricks.yml.tmpl | 13 - .../out.test.toml | 2 - .../output.txt | 47 -- .../destroy_forbidden_at_recorded_name/script | 17 - .../test.toml | 9 - .../databricks.yml.tmpl | 23 - .../out.test.toml | 2 - .../output.txt | 63 --- .../script | 32 -- .../test.toml | 7 - .../databricks.yml | 11 - .../out.test.toml | 2 - .../output.txt | 43 -- .../script | 9 - .../test.toml | 5 - .../destroy_search_fails/databricks.yml.tmpl | 13 - .../destroy_search_fails/out.test.toml | 2 - .../schemas/destroy_search_fails/output.txt | 46 -- .../schemas/destroy_search_fails/script | 16 - .../schemas/destroy_search_fails/test.toml | 8 - .../foreign_at_recorded_name/output.txt | 22 +- .../schemas/foreign_at_recorded_name/script | 4 +- .../foreign_at_recorded_name/test.toml | 1 + .../databricks-initial.yml | 11 - .../databricks-renamed.yml | 8 - .../out.test.toml | 2 - .../output.txt | 34 -- .../script | 17 - .../test.toml | 6 - acceptance/bundle/resources/script.prepare | 12 - .../foreign_at_recorded_name/output.txt | 22 +- .../volumes/foreign_at_recorded_name/script | 4 +- .../foreign_at_recorded_name/test.toml | 1 + .../databricks.yml.tmpl | 15 - .../out.test.toml | 2 - .../output.txt | 51 -- .../prevent_destroy_in_renamed_schema/script | 33 -- .../test.toml | 7 - .../databricks.yml.tmpl | 15 - .../out.test.toml | 2 - .../output.txt | 51 -- .../prevent_destroy_mixed_case_schema/script | 32 -- .../test.toml | 7 - .../databricks-schema-only.yml.tmpl | 8 - .../databricks.yml.tmpl | 15 - .../out.test.toml | 2 - .../output.txt | 36 -- .../script | 28 -- .../test.toml | 7 - ...databricks-renamed-without-volume.yml.tmpl | 8 - .../databricks.yml.tmpl | 13 - .../removed_from_renamed_schema/out.test.toml | 2 - .../removed_from_renamed_schema/output.txt | 56 --- .../removed_from_renamed_schema/script | 28 -- .../removed_from_renamed_schema/test.toml | 7 - .../databricks-empty.yml.tmpl | 2 - .../databricks-schema-only.yml.tmpl | 8 - .../select_renamed_schema/databricks.yml.tmpl | 13 - .../select_renamed_schema/out.test.toml | 2 - .../volumes/select_renamed_schema/output.txt | 71 --- .../volumes/select_renamed_schema/script | 33 -- .../volumes/select_renamed_schema/test.toml | 7 - .../databricks.yml.tmpl | 13 - .../with_parent_delete_fails/out.test.toml | 2 - .../with_parent_delete_fails/output.txt | 68 --- .../volumes/with_parent_delete_fails/script | 16 - .../with_parent_delete_fails/test.toml | 8 - bundle/deployplan/action.go | 12 +- bundle/deployplan/plan.go | 49 +- bundle/deployplan/plan_filter_test.go | 36 -- bundle/deployplan/plan_test.go | 18 - bundle/direct/apply.go | 7 +- bundle/direct/bundle_apply.go | 11 +- bundle/direct/bundle_plan.go | 436 +++--------------- bundle/direct/bundle_plan_test.go | 71 +-- bundle/direct/dresources/README.md | 2 +- bundle/direct/dresources/adapter.go | 41 +- bundle/direct/dresources/config.go | 7 - .../dresources/configs/feature_specs.yml | 2 - bundle/direct/dresources/configs/features.yml | 2 - bundle/direct/dresources/schema.go | 21 - bundle/direct/dresources/volume.go | 24 - bundle/direct/graph.go | 60 --- bundle/direct/graph_test.go | 163 ------- bundle/direct/pkg.go | 4 - libs/testserver/handlers.go | 8 - libs/testserver/schemas.go | 16 - libs/testserver/volumes.go | 25 - 103 files changed, 136 insertions(+), 2308 deletions(-) delete mode 100644 acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/databricks.yml.tmpl delete mode 100644 acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/out.test.toml delete mode 100644 acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/output.txt delete mode 100644 acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/script delete mode 100644 acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/test.toml delete mode 100644 acceptance/bundle/resources/registered_models/removed_from_renamed_schema/databricks-renamed-without-model.yml.tmpl delete mode 100644 acceptance/bundle/resources/registered_models/removed_from_renamed_schema/databricks.yml.tmpl delete mode 100644 acceptance/bundle/resources/registered_models/removed_from_renamed_schema/out.test.toml delete mode 100644 acceptance/bundle/resources/registered_models/removed_from_renamed_schema/output.txt delete mode 100644 acceptance/bundle/resources/registered_models/removed_from_renamed_schema/script delete mode 100644 acceptance/bundle/resources/registered_models/removed_from_renamed_schema/test.toml delete mode 100644 acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/databricks.yml.tmpl delete mode 100644 acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/out.test.toml delete mode 100644 acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/output.txt delete mode 100644 acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/script delete mode 100644 acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/test.toml delete mode 100644 acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/databricks.yml.tmpl delete mode 100644 acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/out.test.toml delete mode 100644 acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/output.txt delete mode 100644 acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/script delete mode 100644 acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/test.toml delete mode 100644 acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/databricks.yml delete mode 100644 acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/out.test.toml delete mode 100644 acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/output.txt delete mode 100644 acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/script delete mode 100644 acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/test.toml delete mode 100644 acceptance/bundle/resources/schemas/destroy_search_fails/databricks.yml.tmpl delete mode 100644 acceptance/bundle/resources/schemas/destroy_search_fails/out.test.toml delete mode 100644 acceptance/bundle/resources/schemas/destroy_search_fails/output.txt delete mode 100644 acceptance/bundle/resources/schemas/destroy_search_fails/script delete mode 100644 acceptance/bundle/resources/schemas/destroy_search_fails/test.toml delete mode 100644 acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/databricks-initial.yml delete mode 100644 acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/databricks-renamed.yml delete mode 100644 acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/out.test.toml delete mode 100644 acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/output.txt delete mode 100644 acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/script delete mode 100644 acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/test.toml delete mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/databricks.yml.tmpl delete mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/out.test.toml delete mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/output.txt delete mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/script delete mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/test.toml delete mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/databricks.yml.tmpl delete mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/out.test.toml delete mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/output.txt delete mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/script delete mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/test.toml delete mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/databricks-schema-only.yml.tmpl delete mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/databricks.yml.tmpl delete mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/out.test.toml delete mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/output.txt delete mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/script delete mode 100644 acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/test.toml delete mode 100644 acceptance/bundle/resources/volumes/removed_from_renamed_schema/databricks-renamed-without-volume.yml.tmpl delete mode 100644 acceptance/bundle/resources/volumes/removed_from_renamed_schema/databricks.yml.tmpl delete mode 100644 acceptance/bundle/resources/volumes/removed_from_renamed_schema/out.test.toml delete mode 100644 acceptance/bundle/resources/volumes/removed_from_renamed_schema/output.txt delete mode 100644 acceptance/bundle/resources/volumes/removed_from_renamed_schema/script delete mode 100644 acceptance/bundle/resources/volumes/removed_from_renamed_schema/test.toml delete mode 100644 acceptance/bundle/resources/volumes/select_renamed_schema/databricks-empty.yml.tmpl delete mode 100644 acceptance/bundle/resources/volumes/select_renamed_schema/databricks-schema-only.yml.tmpl delete mode 100644 acceptance/bundle/resources/volumes/select_renamed_schema/databricks.yml.tmpl delete mode 100644 acceptance/bundle/resources/volumes/select_renamed_schema/out.test.toml delete mode 100644 acceptance/bundle/resources/volumes/select_renamed_schema/output.txt delete mode 100644 acceptance/bundle/resources/volumes/select_renamed_schema/script delete mode 100644 acceptance/bundle/resources/volumes/select_renamed_schema/test.toml delete mode 100644 acceptance/bundle/resources/volumes/with_parent_delete_fails/databricks.yml.tmpl delete mode 100644 acceptance/bundle/resources/volumes/with_parent_delete_fails/out.test.toml delete mode 100644 acceptance/bundle/resources/volumes/with_parent_delete_fails/output.txt delete mode 100644 acceptance/bundle/resources/volumes/with_parent_delete_fails/script delete mode 100644 acceptance/bundle/resources/volumes/with_parent_delete_fails/test.toml delete mode 100644 bundle/direct/graph_test.go diff --git a/.nextchanges/bundles/schema-volume-identity.md b/.nextchanges/bundles/schema-volume-identity.md index 7f3ccbef27e..8c7917b49eb 100644 --- a/.nextchanges/bundles/schema-volume-identity.md +++ b/.nextchanges/bundles/schema-volume-identity.md @@ -1 +1 @@ -* Fixed `bundle destroy` deleting a schema or volume that someone else created under the name of the bundle's own after it was renamed out of band, in the direct deployment engine. Plan and deploy no longer take such an object for the bundle's, and destroy deletes the bundle's renamed schema or volume instead of leaving it behind. A deploy that removes such a renamed schema or volume from the config now deletes it at its new name too (a schema with its contents); before, it only dropped it from the state. A schema or volume deleted and created again outside the bundle is no longer the bundle's either: deploy fails on its name and destroy leaves it, until it is bound again with `bundle deployment unbind` and `bundle deployment bind`. ([#8056](https://github.com/databricks/cli/pull/8056)) +* Fixed the direct deployment engine taking a schema or volume that someone else created under the name of the bundle's own, after it was renamed out of band, for the bundle's: `bundle deploy` no longer updates it and `bundle destroy` no longer deletes it; destroy drops the bundle's entry from the state and warns. A schema or volume deleted and created again outside the bundle is no longer the bundle's either: deploy fails on its name and destroy leaves it, until it is bound again with `bundle deployment unbind` and `bundle deployment bind`. ([#8056](https://github.com/databricks/cli/pull/8056)) diff --git a/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/databricks.yml.tmpl b/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/databricks.yml.tmpl deleted file mode 100644 index 46f6f3d1ec4..00000000000 --- a/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/databricks.yml.tmpl +++ /dev/null @@ -1,15 +0,0 @@ -bundle: - name: test-bundle-$UNIQUE_NAME - -resources: - schemas: - schema1: - catalog_name: main - name: test-schema-$UNIQUE_NAME - registered_models: - model1: - catalog_name: main - schema_name: ${resources.schemas.schema1.name} - name: mymodel - lifecycle: - prevent_destroy: true diff --git a/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/out.test.toml b/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/out.test.toml deleted file mode 100644 index 0f167effef1..00000000000 --- a/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/out.test.toml +++ /dev/null @@ -1,2 +0,0 @@ -Cloud = true -EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/output.txt b/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/output.txt deleted file mode 100644 index 7ce131f2422..00000000000 --- a/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/output.txt +++ /dev/null @@ -1,35 +0,0 @@ - -=== Deploy a schema with a registered model in it that must not be destroyed - ->>> [CLI] bundle deploy -Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... -Created registered_models.model1 -Created schemas.schema1 -Files: 2 uploaded, 0 deleted -Resources: 2 created, 0 changed, 0 deleted, 0 unchanged - -=== Rename the schema and the model out of band - ->>> [CLI] schemas update main.test-schema-[UNIQUE_NAME] --new-name test-schema-renamed-[UNIQUE_NAME] -{ - "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", - "schema_id": "[UUID]" -} - ->>> [CLI] registered-models update main.test-schema-renamed-[UNIQUE_NAME].mymodel --new-name mymodel2 -{ - "full_name": "main.test-schema-renamed-[UNIQUE_NAME].mymodel2" -} - -=== The model has no internal ID and is not at its new path, but the schema's delete would take it, so destroy lists it and prevent_destroy stops it - ->>> musterr [CLI] bundle destroy --auto-approve -Error: resources.registered_models.model1 has lifecycle.prevent_destroy set, but the plan calls for this resource to be recreated or destroyed. To avoid this error, disable lifecycle.prevent_destroy for resources.registered_models.model1 - - ->>> print_requests.py //unity-catalog - ->>> [CLI] registered-models get main.test-schema-renamed-[UNIQUE_NAME].mymodel2 -{ - "full_name": "main.test-schema-renamed-[UNIQUE_NAME].mymodel2" -} diff --git a/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/script b/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/script deleted file mode 100644 index 75333bacf15..00000000000 --- a/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/script +++ /dev/null @@ -1,27 +0,0 @@ -envsubst < databricks.yml.tmpl > databricks.yml - -OLD_SCHEMA="main.test-schema-${UNIQUE_NAME}" -NEW_SCHEMA="main.test-schema-renamed-${UNIQUE_NAME}" - -cleanup() { - errcode $CLI schemas delete "$NEW_SCHEMA" --force > LOG.cleanup 2>&1 - errcode $CLI schemas delete "$OLD_SCHEMA" --force >> LOG.cleanup 2>&1 - - # Consume the destroy's requests so none are left recorded. - print_requests.py //unity-catalog &> LOG.destroy_requests -} -trap cleanup EXIT - -title "Deploy a schema with a registered model in it that must not be destroyed" -trace $CLI bundle deploy -print_requests.py //unity-catalog &> LOG.deploy_requests - -title "Rename the schema and the model out of band" -trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields -trace $CLI registered-models update "$NEW_SCHEMA.mymodel" --new-name mymodel2 | registered_model_fields -print_requests.py //unity-catalog &> LOG.rename_requests - -title "The model has no internal ID and is not at its new path, but the schema's delete would take it, so destroy lists it and prevent_destroy stops it" -trace musterr $CLI bundle destroy --auto-approve -trace print_requests.py //unity-catalog -trace $CLI registered-models get "$NEW_SCHEMA.mymodel2" | registered_model_fields diff --git a/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/test.toml b/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/test.toml deleted file mode 100644 index 4272a80cf61..00000000000 --- a/acceptance/bundle/resources/registered_models/prevent_destroy_in_renamed_schema/test.toml +++ /dev/null @@ -1,7 +0,0 @@ -Cloud = true - -Ignore = [ - ".databricks", - ".gitignore", - "databricks.yml", -] diff --git a/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/databricks-renamed-without-model.yml.tmpl b/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/databricks-renamed-without-model.yml.tmpl deleted file mode 100644 index 911125f013b..00000000000 --- a/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/databricks-renamed-without-model.yml.tmpl +++ /dev/null @@ -1,8 +0,0 @@ -bundle: - name: test-bundle-$UNIQUE_NAME - -resources: - schemas: - schema1: - catalog_name: main - name: test-schema-renamed-$UNIQUE_NAME diff --git a/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/databricks.yml.tmpl b/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/databricks.yml.tmpl deleted file mode 100644 index 630cfc75a4a..00000000000 --- a/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/databricks.yml.tmpl +++ /dev/null @@ -1,13 +0,0 @@ -bundle: - name: test-bundle-$UNIQUE_NAME - -resources: - schemas: - schema1: - catalog_name: main - name: test-schema-$UNIQUE_NAME - registered_models: - model1: - catalog_name: main - schema_name: ${resources.schemas.schema1.name} - name: mymodel diff --git a/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/out.test.toml b/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/out.test.toml deleted file mode 100644 index 0f167effef1..00000000000 --- a/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/out.test.toml +++ /dev/null @@ -1,2 +0,0 @@ -Cloud = true -EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/output.txt b/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/output.txt deleted file mode 100644 index 1596484a9c2..00000000000 --- a/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/output.txt +++ /dev/null @@ -1,56 +0,0 @@ - -=== Deploy a schema with a registered model in it, and rename the schema out of band - ->>> [CLI] bundle deploy -Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... -Created registered_models.model1 -Created schemas.schema1 -Files: 3 uploaded, 0 deleted -Resources: 2 created, 0 changed, 0 deleted, 0 unchanged - ->>> [CLI] schemas update main.test-schema-[UNIQUE_NAME] --new-name test-schema-renamed-[UNIQUE_NAME] -{ - "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", - "schema_id": "[UUID]" -} - -=== Follow the rename in the config and remove the model from it: the model has no internal ID, but is found at its new path inside the bundle's schema - ->>> [CLI] bundle plan -delete registered_models.model1 - -Plan: 0 to add, 0 to change, 1 to delete, 1 unchanged - -=== Deploy deletes the model at its new path, and keeps the schema - ->>> [CLI] bundle deploy --auto-approve -Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... -Deleted registered_models.model1 -Files: 2 uploaded, 0 deleted -Resources: 0 created, 0 changed, 1 deleted, 1 unchanged - ->>> print_requests.py //unity-catalog -{ - "method": "DELETE", - "path": "/api/2.1/unity-catalog/models/main.test-schema-renamed-[UNIQUE_NAME].mymodel" -} - ->>> musterr [CLI] registered-models get main.test-schema-renamed-[UNIQUE_NAME].mymodel -Error: Routine or Model 'main.test-schema-renamed-[UNIQUE_NAME].mymodel' does not exist. - ->>> [CLI] schemas get main.test-schema-renamed-[UNIQUE_NAME] -{ - "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", - "schema_id": "[UUID]" -} - ->>> [CLI] bundle destroy --auto-approve -The following resources will be deleted: - delete resources.schemas.schema1 - -This action will result in the deletion of the following UC schemas. Any underlying data may be lost: - delete resources.schemas.schema1 - -All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default - -Destroy: 1 deleted diff --git a/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/script b/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/script deleted file mode 100644 index 3ec1600a0df..00000000000 --- a/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/script +++ /dev/null @@ -1,27 +0,0 @@ -envsubst < databricks.yml.tmpl > databricks.yml - -OLD_SCHEMA="main.test-schema-${UNIQUE_NAME}" -NEW_SCHEMA="main.test-schema-renamed-${UNIQUE_NAME}" - -cleanup() { - trace $CLI bundle destroy --auto-approve - - # Consume the destroy's requests so none are left recorded. - print_requests.py //unity-catalog &> LOG.destroy_requests -} -trap cleanup EXIT - -title "Deploy a schema with a registered model in it, and rename the schema out of band" -trace $CLI bundle deploy -trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields -print_requests.py //unity-catalog &> LOG.deploy_requests - -title "Follow the rename in the config and remove the model from it: the model has no internal ID, but is found at its new path inside the bundle's schema" -envsubst < databricks-renamed-without-model.yml.tmpl > databricks.yml -trace $CLI bundle plan - -title "Deploy deletes the model at its new path, and keeps the schema" -trace $CLI bundle deploy --auto-approve -trace print_requests.py //unity-catalog -trace musterr $CLI registered-models get "$NEW_SCHEMA.mymodel" -trace $CLI schemas get "$NEW_SCHEMA" | schema_fields diff --git a/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/test.toml b/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/test.toml deleted file mode 100644 index 4272a80cf61..00000000000 --- a/acceptance/bundle/resources/registered_models/removed_from_renamed_schema/test.toml +++ /dev/null @@ -1,7 +0,0 @@ -Cloud = true - -Ignore = [ - ".databricks", - ".gitignore", - "databricks.yml", -] diff --git a/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/output.txt b/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/output.txt index df352651a1e..0997557db9b 100644 --- a/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/output.txt +++ b/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/output.txt @@ -18,40 +18,19 @@ Resources: 2 created, 0 changed, 0 deleted, 0 unchanged >>> update_file.py databricks.yml test-schema- test-schema-renamed- -=== Destroy finds the schema at its new name by its schema_id and deletes it, with its volume +=== Destroy >>> [CLI] bundle destroy --auto-approve -The following resources will be deleted: - delete resources.schemas.schema1 - delete resources.volumes.volume1 - -This action will result in the deletion of the following UC schemas. Any underlying data may be lost: - delete resources.schemas.schema1 - -This action will result in the deletion of the following volumes. -For managed volumes, the files stored in the volume are also deleted from your -cloud tenant within 30 days. For external volumes, the metadata about the volume -is removed from the catalog, but the underlying files are not deleted: - delete resources.volumes.volume1 - All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default -Destroy: 2 deleted +Destroy: 0 deleted >>> print_requests.py //unity-catalog -{ - "method": "DELETE", - "path": "/api/2.1/unity-catalog/volumes/main.test-schema-renamed-[UNIQUE_NAME].myvolume" -} -{ - "method": "DELETE", - "path": "/api/2.1/unity-catalog/schemas/main.test-schema-renamed-[UNIQUE_NAME]", - "q": { - "force": "true" - } -} -=== The renamed schema is gone, and the volume with it +=== The renamed schema is still there ->>> musterr [CLI] schemas get main.test-schema-renamed-[UNIQUE_NAME] -Error: Schema 'main.test-schema-renamed-[UNIQUE_NAME]' does not exist. +>>> [CLI] schemas get main.test-schema-renamed-[UNIQUE_NAME] +{ + "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", + "schema_id": "[SCHEMA_ID]" +} diff --git a/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/script b/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/script index 6cd24237b09..5917b662e47 100644 --- a/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/script +++ b/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/script @@ -18,13 +18,13 @@ print_requests.py //unity-catalog &> LOG.deploy_requests add_repl "$($CLI schemas get "$OLD_SCHEMA" | jq -r .schema_id)" SCHEMA_ID title "Rename the schema out of band, and the config to match" -trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields +trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | jq '{full_name, schema_id}' print_requests.py //unity-catalog &> LOG.rename_requests trace update_file.py databricks.yml test-schema- test-schema-renamed- -title "Destroy finds the schema at its new name by its schema_id and deletes it, with its volume" +title "Destroy" trace $CLI bundle destroy --auto-approve trace print_requests.py //unity-catalog -title "The renamed schema is gone, and the volume with it" -trace musterr $CLI schemas get "$NEW_SCHEMA" +title "The renamed schema is still there" +trace $CLI schemas get "$NEW_SCHEMA" | jq '{full_name, schema_id}' diff --git a/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/test.toml b/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/test.toml index 4272a80cf61..c982f8a9fc2 100644 --- a/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/test.toml +++ b/acceptance/bundle/resources/schemas/destroy_after_rename_out_of_band/test.toml @@ -1,3 +1,4 @@ +Badness = "Destroy does not look for a schema renamed outside the bundle: it reads the recorded name, finds nothing and drops the state entry ('Destroy: 0 deleted'), so the renamed schema and its volume are left behind. Destroy should find the schema by its recorded schema_id, as deploy's plan does, and delete it." Cloud = true Ignore = [ diff --git a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/databricks.yml.tmpl b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/databricks.yml.tmpl deleted file mode 100644 index 192f6d3b8b3..00000000000 --- a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/databricks.yml.tmpl +++ /dev/null @@ -1,13 +0,0 @@ -bundle: - name: test-bundle-$UNIQUE_NAME - -resources: - schemas: - schema1: - catalog_name: main - name: myschema - volumes: - volume1: - catalog_name: main - schema_name: ${resources.schemas.schema1.name} - name: myvolume diff --git a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/out.test.toml b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/out.test.toml deleted file mode 100644 index a927a5fbc06..00000000000 --- a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/out.test.toml +++ /dev/null @@ -1,2 +0,0 @@ -Cloud = false -EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/output.txt b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/output.txt deleted file mode 100644 index 3870129a336..00000000000 --- a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/output.txt +++ /dev/null @@ -1,47 +0,0 @@ - -=== Deploy a schema with a volume in it and rename the schema out of band; the recorded names are not readable - ->>> [CLI] bundle deploy -Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... -Created schemas.schema1 -Created volumes.volume1 -Files: 2 uploaded, 0 deleted -Resources: 2 created, 0 changed, 0 deleted, 0 unchanged - -=== Destroy finds the renamed schema by its schema_id, and the volume in it by its volume_id, and deletes both there - ->>> [CLI] bundle destroy --auto-approve -Warn: planning resources.volumes.volume1: reading resources.volumes.volume1 id="main.myschema.myvolume": Fault injected by test. -Warn: planning resources.schemas.schema1: reading resources.schemas.schema1 id="main.myschema": Fault injected by test. -The following resources will be deleted: - delete resources.schemas.schema1 - delete resources.volumes.volume1 - -This action will result in the deletion of the following UC schemas. Any underlying data may be lost: - delete resources.schemas.schema1 - -This action will result in the deletion of the following volumes. -For managed volumes, the files stored in the volume are also deleted from your -cloud tenant within 30 days. For external volumes, the metadata about the volume -is removed from the catalog, but the underlying files are not deleted: - delete resources.volumes.volume1 - -All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default - -Destroy: 2 deleted - ->>> print_requests.py //unity-catalog -{ - "method": "DELETE", - "path": "/api/2.1/unity-catalog/volumes/main.moved_schema.myvolume" -} -{ - "method": "DELETE", - "path": "/api/2.1/unity-catalog/schemas/main.moved_schema", - "q": { - "force": "true" - } -} - ->>> musterr [CLI] schemas get main.moved_schema -Error: Schema 'main.moved_schema' does not exist. diff --git a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/script b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/script deleted file mode 100644 index ec86594ceb9..00000000000 --- a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/script +++ /dev/null @@ -1,17 +0,0 @@ -envsubst < databricks.yml.tmpl > databricks.yml - -OLD_SCHEMA="main.myschema" -NEW_SCHEMA="main.moved_schema" - -title "Deploy a schema with a volume in it and rename the schema out of band; the recorded names are not readable" -trace $CLI bundle deploy -$CLI schemas update "$OLD_SCHEMA" --new-name moved_schema &> LOG.rename -print_requests.py //unity-catalog &> LOG.deploy_requests - -fault.py "GET /api/2.1/unity-catalog/schemas/main.myschema" 403 0 100 PERMISSION_DENIED -fault.py "GET /api/2.1/unity-catalog/volumes/main.myschema.myvolume" 403 0 100 PERMISSION_DENIED - -title "Destroy finds the renamed schema by its schema_id, and the volume in it by its volume_id, and deletes both there" -trace $CLI bundle destroy --auto-approve -trace print_requests.py //unity-catalog -trace musterr $CLI schemas get "$NEW_SCHEMA" diff --git a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/test.toml b/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/test.toml deleted file mode 100644 index 28b4988ce94..00000000000 --- a/acceptance/bundle/resources/schemas/destroy_forbidden_at_recorded_name/test.toml +++ /dev/null @@ -1,9 +0,0 @@ -# Local only: the 403 at the recorded name is injected, as for another user's schema the -# deployer cannot read. -Cloud = false - -Ignore = [ - ".databricks", - ".gitignore", - "databricks.yml", -] diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/databricks.yml.tmpl b/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/databricks.yml.tmpl deleted file mode 100644 index b8ea3af2c01..00000000000 --- a/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/databricks.yml.tmpl +++ /dev/null @@ -1,23 +0,0 @@ -bundle: - name: test-bundle-$UNIQUE_NAME - -variables: - secret_value: - description: The secret's value - -resources: - schemas: - schema1: - catalog_name: main - name: test-schema-$UNIQUE_NAME - registered_models: - model1: - catalog_name: main - schema_name: ${resources.schemas.schema1.name} - name: mymodel - secrets: - secret1: - catalog_name: main - schema_name: ${resources.schemas.schema1.name} - name: mysecret - value: ${var.secret_value} diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/out.test.toml b/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/out.test.toml deleted file mode 100644 index 0f167effef1..00000000000 --- a/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/out.test.toml +++ /dev/null @@ -1,2 +0,0 @@ -Cloud = true -EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/output.txt b/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/output.txt deleted file mode 100644 index 86da7ec8f2a..00000000000 --- a/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/output.txt +++ /dev/null @@ -1,63 +0,0 @@ - -=== Deploy a schema with a registered model and a secret in it, and rename the schema out of band - ->>> [CLI] bundle deploy -Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... -Created registered_models.model1 -Created schemas.schema1 -Created secrets.secret1 -Files: 2 uploaded, 0 deleted -Resources: 3 created, 0 changed, 0 deleted, 0 unchanged - ->>> [CLI] schemas update main.test-schema-[UNIQUE_NAME] --new-name test-schema-renamed-[UNIQUE_NAME] -{ - "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", - "schema_id": "[SCHEMA_ID]" -} - -=== Out of band, delete the secret, and create another schema under the old name with a model of the same name - ->>> [CLI] secrets-uc delete-secret main.test-schema-renamed-[UNIQUE_NAME].mysecret - ->>> [CLI] schemas create test-schema-[UNIQUE_NAME] main -{ - "full_name": "main.test-schema-[UNIQUE_NAME]", - "schema_id": "[UUID]" -} - ->>> [CLI] registered-models create --catalog-name main --schema-name test-schema-[UNIQUE_NAME] --name mymodel -{ - "full_name": "main.test-schema-[UNIQUE_NAME].mymodel" -} - -=== The model has no internal ID to find it by, so destroy lists it as going with the schema it found, and sends a delete only for that schema. The secret is gone, and nothing at the old name is touched - ->>> [CLI] bundle destroy --auto-approve -The following resources will be deleted: - delete resources.registered_models.model1 - delete resources.schemas.schema1 - delete resources.secrets.secret1 - -This action will result in the deletion of the following UC schemas. Any underlying data may be lost: - delete resources.schemas.schema1 - -All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default - -Destroy: 3 deleted - ->>> print_requests.py //unity-catalog -{ - "method": "DELETE", - "path": "/api/2.1/unity-catalog/schemas/main.test-schema-renamed-[UNIQUE_NAME]", - "q": { - "force": "true" - } -} - ->>> musterr [CLI] schemas get main.test-schema-renamed-[UNIQUE_NAME] -Error: Schema 'main.test-schema-renamed-[UNIQUE_NAME]' does not exist. - ->>> [CLI] registered-models get main.test-schema-[UNIQUE_NAME].mymodel -{ - "full_name": "main.test-schema-[UNIQUE_NAME].mymodel" -} diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/script b/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/script deleted file mode 100644 index 8f032853d06..00000000000 --- a/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/script +++ /dev/null @@ -1,32 +0,0 @@ -envsubst < databricks.yml.tmpl > databricks.yml -export BUNDLE_VAR_secret_value=secret-value - -OLD_SCHEMA="main.test-schema-${UNIQUE_NAME}" -NEW_SCHEMA="main.test-schema-renamed-${UNIQUE_NAME}" - -cleanup() { - errcode $CLI schemas delete "$NEW_SCHEMA" --force > LOG.cleanup 2>&1 - errcode $CLI schemas delete "$OLD_SCHEMA" --force >> LOG.cleanup 2>&1 - - # Consume the destroy's requests so none are left recorded. - print_requests.py //unity-catalog &> LOG.destroy_requests -} -trap cleanup EXIT - -title "Deploy a schema with a registered model and a secret in it, and rename the schema out of band" -trace $CLI bundle deploy -trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields -print_requests.py //unity-catalog &> LOG.deploy_requests -add_repl "$($CLI schemas get "$NEW_SCHEMA" | jq -r .schema_id)" SCHEMA_ID - -title "Out of band, delete the secret, and create another schema under the old name with a model of the same name" -trace $CLI secrets-uc delete-secret "$NEW_SCHEMA.mysecret" -trace $CLI schemas create "test-schema-${UNIQUE_NAME}" main | schema_fields -trace $CLI registered-models create --catalog-name main --schema-name "test-schema-${UNIQUE_NAME}" --name mymodel | registered_model_fields -print_requests.py //unity-catalog &> LOG.oob_requests - -title "The model has no internal ID to find it by, so destroy lists it as going with the schema it found, and sends a delete only for that schema. The secret is gone, and nothing at the old name is touched" -trace $CLI bundle destroy --auto-approve -trace print_requests.py //unity-catalog -trace musterr $CLI schemas get "$NEW_SCHEMA" -trace $CLI registered-models get "$OLD_SCHEMA.mymodel" | registered_model_fields diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/test.toml b/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/test.toml deleted file mode 100644 index 4272a80cf61..00000000000 --- a/acceptance/bundle/resources/schemas/destroy_renamed_with_model_and_secret/test.toml +++ /dev/null @@ -1,7 +0,0 @@ -Cloud = true - -Ignore = [ - ".databricks", - ".gitignore", - "databricks.yml", -] diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/databricks.yml b/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/databricks.yml deleted file mode 100644 index bd70982d43e..00000000000 --- a/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/databricks.yml +++ /dev/null @@ -1,11 +0,0 @@ -bundle: - name: test-bundle - -resources: - schemas: - schema1: - catalog_name: main - name: myschema - secret_scopes: - scope1: - name: main.myschema.scope diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/out.test.toml b/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/out.test.toml deleted file mode 100644 index a927a5fbc06..00000000000 --- a/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/out.test.toml +++ /dev/null @@ -1,2 +0,0 @@ -Cloud = false -EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/output.txt b/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/output.txt deleted file mode 100644 index c090430d5b7..00000000000 --- a/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/output.txt +++ /dev/null @@ -1,43 +0,0 @@ - -=== Deploy a schema and a secret scope whose name starts with the schema's full name, and rename the schema out of band - ->>> [CLI] bundle deploy -Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files... -Created schemas.schema1 -Created secret_scopes.scope1 -Created secret_scopes.scope1.permissions -Files: 1 uploaded, 0 deleted -Resources: 3 created, 0 changed, 0 deleted, 0 unchanged - -=== The scope is not in the schema: destroy deletes it where it is, and the schema at its new name - ->>> [CLI] bundle destroy --auto-approve -The following resources will be deleted: - delete resources.schemas.schema1 - delete resources.secret_scopes.scope1 - -This action will result in the deletion of the following UC schemas. Any underlying data may be lost: - delete resources.schemas.schema1 - -All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle/default - -Destroy: 2 deleted - ->>> print_requests.py --sort //unity-catalog //secrets -{ - "method": "DELETE", - "path": "/api/2.1/unity-catalog/schemas/main.moved_schema", - "q": { - "force": "true" - } -} -{ - "method": "POST", - "path": "/api/2.0/secrets/scopes/delete", - "body": { - "scope": "main.myschema.scope" - } -} - ->>> musterr [CLI] secrets list-secrets main.myschema.scope -Error: Scope main.myschema.scope does not exist diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/script b/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/script deleted file mode 100644 index b535c128fe5..00000000000 --- a/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/script +++ /dev/null @@ -1,9 +0,0 @@ -title "Deploy a schema and a secret scope whose name starts with the schema's full name, and rename the schema out of band" -trace $CLI bundle deploy -$CLI schemas update main.myschema --new-name moved_schema &> LOG.rename -print_requests.py //unity-catalog //secrets &> LOG.deploy_requests - -title "The scope is not in the schema: destroy deletes it where it is, and the schema at its new name" -trace $CLI bundle destroy --auto-approve -trace print_requests.py --sort //unity-catalog //secrets -trace musterr $CLI secrets list-secrets main.myschema.scope diff --git a/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/test.toml b/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/test.toml deleted file mode 100644 index 00fdd96b767..00000000000 --- a/acceptance/bundle/resources/schemas/destroy_renamed_with_scope_named_like_it/test.toml +++ /dev/null @@ -1,5 +0,0 @@ -# Local only: names are fixed so that the scope's name starts with the schema's full name. - -Ignore = [ - ".databricks", -] diff --git a/acceptance/bundle/resources/schemas/destroy_search_fails/databricks.yml.tmpl b/acceptance/bundle/resources/schemas/destroy_search_fails/databricks.yml.tmpl deleted file mode 100644 index 2234fdf2e39..00000000000 --- a/acceptance/bundle/resources/schemas/destroy_search_fails/databricks.yml.tmpl +++ /dev/null @@ -1,13 +0,0 @@ -bundle: - name: test-bundle-$UNIQUE_NAME - -resources: - schemas: - schema1: - catalog_name: main - name: test-schema-$UNIQUE_NAME - volumes: - volume1: - catalog_name: main - schema_name: ${resources.schemas.schema1.name} - name: myvolume diff --git a/acceptance/bundle/resources/schemas/destroy_search_fails/out.test.toml b/acceptance/bundle/resources/schemas/destroy_search_fails/out.test.toml deleted file mode 100644 index a927a5fbc06..00000000000 --- a/acceptance/bundle/resources/schemas/destroy_search_fails/out.test.toml +++ /dev/null @@ -1,2 +0,0 @@ -Cloud = false -EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/schemas/destroy_search_fails/output.txt b/acceptance/bundle/resources/schemas/destroy_search_fails/output.txt deleted file mode 100644 index 2c385761a9e..00000000000 --- a/acceptance/bundle/resources/schemas/destroy_search_fails/output.txt +++ /dev/null @@ -1,46 +0,0 @@ - -=== Deploy a schema with a volume in it, and rename the schema out of band - ->>> [CLI] bundle deploy -Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... -Created schemas.schema1 -Created volumes.volume1 -Files: 2 uploaded, 0 deleted -Resources: 2 created, 0 changed, 0 deleted, 0 unchanged - ->>> [CLI] schemas update main.test-schema-[UNIQUE_NAME] --new-name test-schema-renamed-[UNIQUE_NAME] -{ - "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", - "schema_id": "[UUID]" -} - -=== The search for the renamed schema fails, so destroy fails without deleting anything or dropping the state - ->>> musterr [CLI] bundle destroy --auto-approve -Error: cannot plan resources.schemas.schema1: searching "main" for schema_id "[UUID]", as a read at its recorded ID did not return it: Fault injected by test. (500 INTERNAL_ERROR) - -Endpoint: GET [DATABRICKS_URL]/api/2.1/unity-catalog/schemas?catalog_name=main&max_results=0 -HTTP Status: 500 Internal Server Error -API error_code: INTERNAL_ERROR -API message: Fault injected by test. - -Error: planning failed - - ->>> print_requests.py //unity-catalog - ->>> summary_ids -{ - "schemas": { - "schema1": "main.test-schema-[UNIQUE_NAME]" - }, - "volumes": { - "volume1": "main.test-schema-[UNIQUE_NAME].myvolume" - } -} - ->>> [CLI] schemas get main.test-schema-renamed-[UNIQUE_NAME] -{ - "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", - "schema_id": "[UUID]" -} diff --git a/acceptance/bundle/resources/schemas/destroy_search_fails/script b/acceptance/bundle/resources/schemas/destroy_search_fails/script deleted file mode 100644 index 780e6027756..00000000000 --- a/acceptance/bundle/resources/schemas/destroy_search_fails/script +++ /dev/null @@ -1,16 +0,0 @@ -envsubst < databricks.yml.tmpl > databricks.yml - -OLD_SCHEMA="main.test-schema-${UNIQUE_NAME}" -NEW_SCHEMA="main.test-schema-renamed-${UNIQUE_NAME}" - -title "Deploy a schema with a volume in it, and rename the schema out of band" -trace $CLI bundle deploy -trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields -print_requests.py //unity-catalog &> LOG.deploy_requests - -title "The search for the renamed schema fails, so destroy fails without deleting anything or dropping the state" -fault.py "GET /api/2.1/unity-catalog/schemas" 500 0 100 INTERNAL_ERROR -trace musterr $CLI bundle destroy --auto-approve -trace print_requests.py //unity-catalog -trace summary_ids -trace $CLI schemas get "$NEW_SCHEMA" | schema_fields diff --git a/acceptance/bundle/resources/schemas/destroy_search_fails/test.toml b/acceptance/bundle/resources/schemas/destroy_search_fails/test.toml deleted file mode 100644 index fb72b6dda1b..00000000000 --- a/acceptance/bundle/resources/schemas/destroy_search_fails/test.toml +++ /dev/null @@ -1,8 +0,0 @@ -# Local only: the failure of the search for the renamed schema is injected. -Cloud = false - -Ignore = [ - ".databricks", - ".gitignore", - "databricks.yml", -] diff --git a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt index 488fca97772..e1cf6f7f397 100644 --- a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt +++ b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt @@ -55,31 +55,23 @@ API message: Schema 'test-schema-[UNIQUE_NAME]' already exists Files: 2 uploaded, 0 deleted -=== Destroy finds the bundle's schema by its schema_id and deletes it, not the other one +=== Destroy does not delete the other schema: it only drops the bundle's from the state >>> [CLI] bundle destroy --auto-approve -The following resources will be deleted: - delete resources.schemas.schema1 - -This action will result in the deletion of the following UC schemas. Any underlying data may be lost: - delete resources.schemas.schema1 +Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it in place, and drops resources.schemas.schema1 from the deployment state. If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle, or manage it again with 'bundle deployment bind' while it is in the config ('bundle deployment unbind' first if the state still has it). All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default -Destroy: 1 deleted +Destroy: 0 deleted >>> print_requests.py //unity-catalog + +>>> [CLI] schemas get main.test-schema-moved-[UNIQUE_NAME] { - "method": "DELETE", - "path": "/api/2.1/unity-catalog/schemas/main.test-schema-moved-[UNIQUE_NAME]", - "q": { - "force": "true" - } + "full_name": "main.test-schema-moved-[UNIQUE_NAME]", + "schema_id": "[SCHEMA_ID]" } ->>> musterr [CLI] schemas get main.test-schema-moved-[UNIQUE_NAME] -Error: Schema 'main.test-schema-moved-[UNIQUE_NAME]' does not exist. - === The other schema is left alone >>> [CLI] schemas get main.test-schema-[UNIQUE_NAME] diff --git a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script index ac68043644e..3dd19c5f2e4 100644 --- a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script +++ b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script @@ -35,10 +35,10 @@ title "Deploy leaves the other schema alone: the create fails on its name" trace musterr $CLI bundle deploy print_requests.py //unity-catalog &> LOG.failed_deploy_requests -title "Destroy finds the bundle's schema by its schema_id and deletes it, not the other one" +title "Destroy does not delete the other schema: it only drops the bundle's from the state" trace $CLI bundle destroy --auto-approve trace print_requests.py //unity-catalog -trace musterr $CLI schemas get "$MOVED_SCHEMA" +trace $CLI schemas get "$MOVED_SCHEMA" | schema_fields title "The other schema is left alone" trace $CLI schemas get "$SCHEMA" | schema_fields diff --git a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/test.toml b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/test.toml index 4272a80cf61..652d759a483 100644 --- a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/test.toml +++ b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/test.toml @@ -1,3 +1,4 @@ +Badness = "Destroy does not look for the bundle's schema after a rename out of band: it drops the state entry and leaves the renamed schema behind. It should find it by its schema_id." Cloud = true Ignore = [ diff --git a/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/databricks-initial.yml b/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/databricks-initial.yml deleted file mode 100644 index bd70982d43e..00000000000 --- a/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/databricks-initial.yml +++ /dev/null @@ -1,11 +0,0 @@ -bundle: - name: test-bundle - -resources: - schemas: - schema1: - catalog_name: main - name: myschema - secret_scopes: - scope1: - name: main.myschema.scope diff --git a/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/databricks-renamed.yml b/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/databricks-renamed.yml deleted file mode 100644 index 9db36f92729..00000000000 --- a/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/databricks-renamed.yml +++ /dev/null @@ -1,8 +0,0 @@ -bundle: - name: test-bundle - -resources: - schemas: - schema1: - catalog_name: main - name: moved_schema diff --git a/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/out.test.toml b/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/out.test.toml deleted file mode 100644 index a927a5fbc06..00000000000 --- a/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/out.test.toml +++ /dev/null @@ -1,2 +0,0 @@ -Cloud = false -EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/output.txt b/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/output.txt deleted file mode 100644 index 572b2eed059..00000000000 --- a/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/output.txt +++ /dev/null @@ -1,34 +0,0 @@ - -=== Deploy a schema and a secret scope whose name starts with the schema's full name - ->>> [CLI] bundle deploy -Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files... -Created schemas.schema1 -Created secret_scopes.scope1 -Created secret_scopes.scope1.permissions -Files: 3 uploaded, 0 deleted -Resources: 3 created, 0 changed, 0 deleted, 0 unchanged - -=== Out of band: rename the schema, delete the scope, and create another scope named like the renamed schema - ->>> [CLI] schemas update main.myschema --new-name moved_schema -{ - "full_name": "main.moved_schema", - "schema_id": "[UUID]" -} - ->>> [CLI] secrets delete-scope main.myschema.scope - ->>> [CLI] secrets create-scope main.moved_schema.scope - -=== The config follows the rename and drops the scope: the other scope is not the bundle's, and is left alone - ->>> [CLI] bundle deploy --auto-approve -Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files... -Files: 3 uploaded, 0 deleted -Resources: 0 created, 0 changed, 0 deleted, 1 unchanged - ->>> print_requests.py --sort //unity-catalog //secrets - ->>> [CLI] secrets list-scopes -o json -["main.moved_schema.scope"] diff --git a/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/script b/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/script deleted file mode 100644 index 1eddc39d2ac..00000000000 --- a/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/script +++ /dev/null @@ -1,17 +0,0 @@ -cp databricks-initial.yml databricks.yml - -title "Deploy a schema and a secret scope whose name starts with the schema's full name" -trace $CLI bundle deploy -print_requests.py //unity-catalog //secrets &> LOG.deploy_requests - -title "Out of band: rename the schema, delete the scope, and create another scope named like the renamed schema" -trace $CLI schemas update main.myschema --new-name moved_schema | schema_fields -trace $CLI secrets delete-scope main.myschema.scope -trace $CLI secrets create-scope main.moved_schema.scope -print_requests.py //unity-catalog //secrets &> LOG.out_of_band_requests - -title "The config follows the rename and drops the scope: the other scope is not the bundle's, and is left alone" -cp databricks-renamed.yml databricks.yml -trace $CLI bundle deploy --auto-approve -trace print_requests.py --sort //unity-catalog //secrets -trace $CLI secrets list-scopes -o json | secret_scope_names diff --git a/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/test.toml b/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/test.toml deleted file mode 100644 index 9af916a3791..00000000000 --- a/acceptance/bundle/resources/schemas/rename_with_gone_scope_named_like_it/test.toml +++ /dev/null @@ -1,6 +0,0 @@ -# Local only: names are fixed so that the scope's name starts with the schema's full name. - -Ignore = [ - ".databricks", - "databricks.yml", -] diff --git a/acceptance/bundle/resources/script.prepare b/acceptance/bundle/resources/script.prepare index 3bb0c380ada..c657433796c 100644 --- a/acceptance/bundle/resources/script.prepare +++ b/acceptance/bundle/resources/script.prepare @@ -7,15 +7,3 @@ schema_fields() { volume_fields() { jq '{full_name, volume_id} | with_entries(select(.value != null))' } -registered_model_fields() { - jq '{full_name}' -} -# secret_scope_names prints the sorted names from 'secrets list-scopes -o json' on one line. -secret_scope_names() { - jq -c '[.[].name] | sort' -} - -# summary_ids prints the ID of each resource in 'bundle summary', by group and key. -summary_ids() { - $CLI bundle summary --output json | jq '.resources // {} | map_values(map_values(.id))' -} diff --git a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt index f7cf70383ca..3364b361f1a 100644 --- a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt +++ b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt @@ -24,31 +24,23 @@ create volumes.volume1 Plan: 1 to add, 0 to change, 0 to delete, 0 unchanged -=== Destroy finds the bundle's volume by its volume_id and deletes it, not the other one +=== Destroy does not delete the other volume: it only drops the bundle's from the state >>> [CLI] bundle destroy --auto-approve -The following resources will be deleted: - delete resources.volumes.volume1 - -This action will result in the deletion of the following volumes. -For managed volumes, the files stored in the volume are also deleted from your -cloud tenant within 30 days. For external volumes, the metadata about the volume -is removed from the catalog, but the underlying files are not deleted: - delete resources.volumes.volume1 +Warning: resources.volumes.volume1: id="main.test-schema-[UNIQUE_NAME].myvolume" holds another object: its volume_id is "[FOREIGN_VOLUME_ID]", not "[VOLUME_ID]" as recorded, so this bundle leaves it in place, and drops resources.volumes.volume1 from the deployment state. If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle, or manage it again with 'bundle deployment bind' while it is in the config ('bundle deployment unbind' first if the state still has it). All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default -Destroy: 1 deleted +Destroy: 0 deleted >>> print_requests.py //unity-catalog + +>>> [CLI] volumes read main.test-schema-[UNIQUE_NAME].myvolume-moved { - "method": "DELETE", - "path": "/api/2.1/unity-catalog/volumes/main.test-schema-[UNIQUE_NAME].myvolume-moved" + "full_name": "main.test-schema-[UNIQUE_NAME].myvolume-moved", + "volume_id": "[VOLUME_ID]" } ->>> musterr [CLI] volumes read main.test-schema-[UNIQUE_NAME].myvolume-moved -Error: Volume 'main.test-schema-[UNIQUE_NAME].myvolume-moved' does not exist. - === The other volume is left alone >>> [CLI] volumes read main.test-schema-[UNIQUE_NAME].myvolume diff --git a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script index 880a8cf30fe..9f151855a86 100644 --- a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script +++ b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script @@ -26,10 +26,10 @@ print_requests.py //unity-catalog &> LOG.oob_requests title "The other volume has another volume_id, so the plan does not take it for the bundle's: it plans a create" trace $CLI bundle plan -title "Destroy finds the bundle's volume by its volume_id and deletes it, not the other one" +title "Destroy does not delete the other volume: it only drops the bundle's from the state" trace $CLI bundle destroy --auto-approve trace print_requests.py //unity-catalog -trace musterr $CLI volumes read "$SCHEMA.myvolume-moved" +trace $CLI volumes read "$SCHEMA.myvolume-moved" | volume_fields title "The other volume is left alone" trace $CLI volumes read "$SCHEMA.myvolume" | volume_fields diff --git a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/test.toml b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/test.toml index 4272a80cf61..dd82528e899 100644 --- a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/test.toml +++ b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/test.toml @@ -1,3 +1,4 @@ +Badness = "Destroy does not look for the bundle's volume after a rename out of band: it drops the state entry and leaves the renamed volume behind. It should find it by its volume_id." Cloud = true Ignore = [ diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/databricks.yml.tmpl b/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/databricks.yml.tmpl deleted file mode 100644 index f2a772f1291..00000000000 --- a/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/databricks.yml.tmpl +++ /dev/null @@ -1,15 +0,0 @@ -bundle: - name: test-bundle-$UNIQUE_NAME - -resources: - schemas: - schema1: - catalog_name: main - name: test-schema-$UNIQUE_NAME - volumes: - volume1: - catalog_name: main - schema_name: ${resources.schemas.schema1.name} - name: myvolume - lifecycle: - prevent_destroy: true diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/out.test.toml b/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/out.test.toml deleted file mode 100644 index 0f167effef1..00000000000 --- a/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/out.test.toml +++ /dev/null @@ -1,2 +0,0 @@ -Cloud = true -EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/output.txt b/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/output.txt deleted file mode 100644 index d82c830d346..00000000000 --- a/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/output.txt +++ /dev/null @@ -1,51 +0,0 @@ - -=== Deploy a schema with a volume in it that must not be destroyed - ->>> [CLI] bundle deploy -Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... -Created schemas.schema1 -Created volumes.volume1 -Files: 2 uploaded, 0 deleted -Resources: 2 created, 0 changed, 0 deleted, 0 unchanged - -=== Rename the schema out of band. UC moves the volume with it - ->>> [CLI] schemas update main.test-schema-[UNIQUE_NAME] --new-name test-schema-renamed-[UNIQUE_NAME] -{ - "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", - "schema_id": "[UUID][0]" -} - -=== Destroy finds the volume in the renamed schema, so prevent_destroy still stops it - ->>> musterr [CLI] bundle destroy --auto-approve -Error: resources.volumes.volume1 has lifecycle.prevent_destroy set, but the plan calls for this resource to be recreated or destroyed. To avoid this error, disable lifecycle.prevent_destroy for resources.volumes.volume1 - - ->>> print_requests.py //unity-catalog - ->>> [CLI] volumes read main.test-schema-renamed-[UNIQUE_NAME].myvolume -{ - "full_name": "main.test-schema-renamed-[UNIQUE_NAME].myvolume", - "volume_id": "[UUID][1]" -} - -=== With the volume renamed out of band too, it is not found, but destroy still lists it, so prevent_destroy still stops it - ->>> [CLI] volumes update main.test-schema-renamed-[UNIQUE_NAME].myvolume --new-name myvolume2 -{ - "full_name": "main.test-schema-renamed-[UNIQUE_NAME].myvolume2", - "volume_id": "[UUID][1]" -} - ->>> musterr [CLI] bundle destroy --auto-approve -Error: resources.volumes.volume1 has lifecycle.prevent_destroy set, but the plan calls for this resource to be recreated or destroyed. To avoid this error, disable lifecycle.prevent_destroy for resources.volumes.volume1 - - ->>> print_requests.py //unity-catalog - ->>> [CLI] volumes read main.test-schema-renamed-[UNIQUE_NAME].myvolume2 -{ - "full_name": "main.test-schema-renamed-[UNIQUE_NAME].myvolume2", - "volume_id": "[UUID][1]" -} diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/script b/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/script deleted file mode 100644 index ee03e585849..00000000000 --- a/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/script +++ /dev/null @@ -1,33 +0,0 @@ -envsubst < databricks.yml.tmpl > databricks.yml - -OLD_SCHEMA="main.test-schema-${UNIQUE_NAME}" -NEW_SCHEMA="main.test-schema-renamed-${UNIQUE_NAME}" - -cleanup() { - errcode $CLI schemas delete "$NEW_SCHEMA" --force > LOG.cleanup 2>&1 - errcode $CLI schemas delete "$OLD_SCHEMA" --force >> LOG.cleanup 2>&1 - - # Consume the destroy's requests so none are left recorded. - print_requests.py //unity-catalog &> LOG.destroy_requests -} -trap cleanup EXIT - -title "Deploy a schema with a volume in it that must not be destroyed" -trace $CLI bundle deploy -print_requests.py //unity-catalog &> LOG.deploy_requests - -title "Rename the schema out of band. UC moves the volume with it" -trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields -print_requests.py //unity-catalog &> LOG.rename_requests - -title "Destroy finds the volume in the renamed schema, so prevent_destroy still stops it" -trace musterr $CLI bundle destroy --auto-approve -trace print_requests.py //unity-catalog -trace $CLI volumes read "$NEW_SCHEMA.myvolume" | volume_fields - -title "With the volume renamed out of band too, it is not found, but destroy still lists it, so prevent_destroy still stops it" -trace $CLI volumes update "$NEW_SCHEMA.myvolume" --new-name myvolume2 | volume_fields -print_requests.py //unity-catalog &> LOG.rename2_requests -trace musterr $CLI bundle destroy --auto-approve -trace print_requests.py //unity-catalog -trace $CLI volumes read "$NEW_SCHEMA.myvolume2" | volume_fields diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/test.toml b/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/test.toml deleted file mode 100644 index 4272a80cf61..00000000000 --- a/acceptance/bundle/resources/volumes/prevent_destroy_in_renamed_schema/test.toml +++ /dev/null @@ -1,7 +0,0 @@ -Cloud = true - -Ignore = [ - ".databricks", - ".gitignore", - "databricks.yml", -] diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/databricks.yml.tmpl b/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/databricks.yml.tmpl deleted file mode 100644 index f2a772f1291..00000000000 --- a/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/databricks.yml.tmpl +++ /dev/null @@ -1,15 +0,0 @@ -bundle: - name: test-bundle-$UNIQUE_NAME - -resources: - schemas: - schema1: - catalog_name: main - name: test-schema-$UNIQUE_NAME - volumes: - volume1: - catalog_name: main - schema_name: ${resources.schemas.schema1.name} - name: myvolume - lifecycle: - prevent_destroy: true diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/out.test.toml b/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/out.test.toml deleted file mode 100644 index 0f167effef1..00000000000 --- a/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/out.test.toml +++ /dev/null @@ -1,2 +0,0 @@ -Cloud = true -EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/output.txt b/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/output.txt deleted file mode 100644 index 159a35ce0b9..00000000000 --- a/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/output.txt +++ /dev/null @@ -1,51 +0,0 @@ - -=== Deploy a schema with a volume in it that must not be destroyed - ->>> [CLI] bundle deploy -Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... -Created schemas.schema1 -Created volumes.volume1 -Files: 2 uploaded, 0 deleted -Resources: 2 created, 0 changed, 0 deleted, 0 unchanged - -=== Rename the schema in the config to a mixed-case name. UC stores it in lower case; the state records the volume as the config names it - ->>> update_file.py databricks.yml name: test-schema- name: Test-Schema-Mixed- - ->>> [CLI] bundle deploy --auto-approve -Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... -Updated schemas.schema1 -Files: 2 uploaded, 0 deleted -Resources: 0 created, 1 changed, 0 deleted, 1 unchanged - ->>> summary_ids -{ - "schemas": { - "schema1": "main.test-schema-mixed-[UNIQUE_NAME]" - }, - "volumes": { - "volume1": "main.Test-Schema-Mixed-[UNIQUE_NAME].myvolume" - } -} - -=== Rename the schema out of band. UC moves the volume with it - ->>> [CLI] schemas update main.test-schema-mixed-[UNIQUE_NAME] --new-name test-schema-renamed-[UNIQUE_NAME] -{ - "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", - "schema_id": "[UUID][0]" -} - -=== Destroy finds the volume under the schema's new name, so prevent_destroy still stops it - ->>> musterr [CLI] bundle destroy --auto-approve -Error: resources.volumes.volume1 has lifecycle.prevent_destroy set, but the plan calls for this resource to be recreated or destroyed. To avoid this error, disable lifecycle.prevent_destroy for resources.volumes.volume1 - - ->>> print_requests.py //unity-catalog - ->>> [CLI] volumes read main.test-schema-renamed-[UNIQUE_NAME].myvolume -{ - "full_name": "main.test-schema-renamed-[UNIQUE_NAME].myvolume", - "volume_id": "[UUID][1]" -} diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/script b/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/script deleted file mode 100644 index cfbd4067e9a..00000000000 --- a/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/script +++ /dev/null @@ -1,32 +0,0 @@ -envsubst < databricks.yml.tmpl > databricks.yml - -OLD_SCHEMA="main.test-schema-${UNIQUE_NAME}" -NEW_SCHEMA="main.test-schema-renamed-${UNIQUE_NAME}" - -cleanup() { - errcode $CLI schemas delete "$NEW_SCHEMA" --force > LOG.cleanup 2>&1 - errcode $CLI schemas delete "main.test-schema-mixed-${UNIQUE_NAME}" --force >> LOG.cleanup 2>&1 - - # Consume the destroy's requests so none are left recorded. - print_requests.py //unity-catalog &> LOG.destroy_requests -} -trap cleanup EXIT - -title "Deploy a schema with a volume in it that must not be destroyed" -trace $CLI bundle deploy -print_requests.py //unity-catalog &> LOG.deploy_requests - -title "Rename the schema in the config to a mixed-case name. UC stores it in lower case; the state records the volume as the config names it" -trace update_file.py databricks.yml "name: test-schema-" "name: Test-Schema-Mixed-" -trace $CLI bundle deploy --auto-approve -print_requests.py //unity-catalog &> LOG.mixed_requests -trace summary_ids - -title "Rename the schema out of band. UC moves the volume with it" -trace $CLI schemas update "main.test-schema-mixed-${UNIQUE_NAME}" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields -print_requests.py //unity-catalog &> LOG.rename_requests - -title "Destroy finds the volume under the schema's new name, so prevent_destroy still stops it" -trace musterr $CLI bundle destroy --auto-approve -trace print_requests.py //unity-catalog -trace $CLI volumes read "$NEW_SCHEMA.myvolume" | volume_fields diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/test.toml b/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/test.toml deleted file mode 100644 index 4272a80cf61..00000000000 --- a/acceptance/bundle/resources/volumes/prevent_destroy_mixed_case_schema/test.toml +++ /dev/null @@ -1,7 +0,0 @@ -Cloud = true - -Ignore = [ - ".databricks", - ".gitignore", - "databricks.yml", -] diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/databricks-schema-only.yml.tmpl b/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/databricks-schema-only.yml.tmpl deleted file mode 100644 index 6f97649e503..00000000000 --- a/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/databricks-schema-only.yml.tmpl +++ /dev/null @@ -1,8 +0,0 @@ -bundle: - name: test-bundle-$UNIQUE_NAME - -resources: - schemas: - schema1: - catalog_name: main - name: Test-Schema-$UNIQUE_NAME diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/databricks.yml.tmpl b/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/databricks.yml.tmpl deleted file mode 100644 index 084b982c3de..00000000000 --- a/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/databricks.yml.tmpl +++ /dev/null @@ -1,15 +0,0 @@ -bundle: - name: test-bundle-$UNIQUE_NAME - -resources: - schemas: - schema1: - catalog_name: main - name: Test-Schema-$UNIQUE_NAME - volumes: - volume1: - catalog_name: main - schema_name: test-schema-$UNIQUE_NAME - name: myvolume - lifecycle: - prevent_destroy: true diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/out.test.toml b/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/out.test.toml deleted file mode 100644 index 0f167effef1..00000000000 --- a/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/out.test.toml +++ /dev/null @@ -1,2 +0,0 @@ -Cloud = true -EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/output.txt b/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/output.txt deleted file mode 100644 index e413387fd5b..00000000000 --- a/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/output.txt +++ /dev/null @@ -1,36 +0,0 @@ - -=== Deploy a schema, then a volume that names it in lower case, as UC stores it: the volume has no dependency on the schema - ->>> [CLI] bundle deploy -Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... -Created schemas.schema1 -Files: 3 uploaded, 0 deleted -Resources: 1 created, 0 changed, 0 deleted, 0 unchanged - ->>> [CLI] bundle deploy -Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... -Created volumes.volume1 -Files: 1 uploaded, 0 deleted -Resources: 1 created, 0 changed, 0 deleted, 1 unchanged - -=== Rename the schema out of band. UC moves the volume with it - ->>> [CLI] schemas update main.test-schema-[UNIQUE_NAME] --new-name test-schema-renamed-[UNIQUE_NAME] -{ - "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", - "schema_id": "[UUID][0]" -} - -=== Destroy finds the volume under the schema's new name, so prevent_destroy still stops it - ->>> musterr [CLI] bundle destroy --auto-approve -Error: resources.volumes.volume1 has lifecycle.prevent_destroy set, but the plan calls for this resource to be recreated or destroyed. To avoid this error, disable lifecycle.prevent_destroy for resources.volumes.volume1 - - ->>> print_requests.py //unity-catalog - ->>> [CLI] volumes read main.test-schema-renamed-[UNIQUE_NAME].myvolume -{ - "full_name": "main.test-schema-renamed-[UNIQUE_NAME].myvolume", - "volume_id": "[UUID][1]" -} diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/script b/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/script deleted file mode 100644 index 8aa7d09d46c..00000000000 --- a/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/script +++ /dev/null @@ -1,28 +0,0 @@ - -OLD_SCHEMA="main.test-schema-${UNIQUE_NAME}" -NEW_SCHEMA="main.test-schema-renamed-${UNIQUE_NAME}" - -cleanup() { - errcode $CLI schemas delete "$NEW_SCHEMA" --force > LOG.cleanup 2>&1 - errcode $CLI schemas delete "$OLD_SCHEMA" --force >> LOG.cleanup 2>&1 - - # Consume the destroy's requests so none are left recorded. - print_requests.py //unity-catalog &> LOG.destroy_requests -} -trap cleanup EXIT - -title "Deploy a schema, then a volume that names it in lower case, as UC stores it: the volume has no dependency on the schema" -envsubst < databricks-schema-only.yml.tmpl > databricks.yml -trace $CLI bundle deploy -envsubst < databricks.yml.tmpl > databricks.yml -trace $CLI bundle deploy -print_requests.py //unity-catalog &> LOG.deploy_requests - -title "Rename the schema out of band. UC moves the volume with it" -trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields -print_requests.py //unity-catalog &> LOG.rename_requests - -title "Destroy finds the volume under the schema's new name, so prevent_destroy still stops it" -trace musterr $CLI bundle destroy --auto-approve -trace print_requests.py //unity-catalog -trace $CLI volumes read "$NEW_SCHEMA.myvolume" | volume_fields diff --git a/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/test.toml b/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/test.toml deleted file mode 100644 index 4272a80cf61..00000000000 --- a/acceptance/bundle/resources/volumes/prevent_destroy_schema_without_dependency/test.toml +++ /dev/null @@ -1,7 +0,0 @@ -Cloud = true - -Ignore = [ - ".databricks", - ".gitignore", - "databricks.yml", -] diff --git a/acceptance/bundle/resources/volumes/removed_from_renamed_schema/databricks-renamed-without-volume.yml.tmpl b/acceptance/bundle/resources/volumes/removed_from_renamed_schema/databricks-renamed-without-volume.yml.tmpl deleted file mode 100644 index 911125f013b..00000000000 --- a/acceptance/bundle/resources/volumes/removed_from_renamed_schema/databricks-renamed-without-volume.yml.tmpl +++ /dev/null @@ -1,8 +0,0 @@ -bundle: - name: test-bundle-$UNIQUE_NAME - -resources: - schemas: - schema1: - catalog_name: main - name: test-schema-renamed-$UNIQUE_NAME diff --git a/acceptance/bundle/resources/volumes/removed_from_renamed_schema/databricks.yml.tmpl b/acceptance/bundle/resources/volumes/removed_from_renamed_schema/databricks.yml.tmpl deleted file mode 100644 index 2234fdf2e39..00000000000 --- a/acceptance/bundle/resources/volumes/removed_from_renamed_schema/databricks.yml.tmpl +++ /dev/null @@ -1,13 +0,0 @@ -bundle: - name: test-bundle-$UNIQUE_NAME - -resources: - schemas: - schema1: - catalog_name: main - name: test-schema-$UNIQUE_NAME - volumes: - volume1: - catalog_name: main - schema_name: ${resources.schemas.schema1.name} - name: myvolume diff --git a/acceptance/bundle/resources/volumes/removed_from_renamed_schema/out.test.toml b/acceptance/bundle/resources/volumes/removed_from_renamed_schema/out.test.toml deleted file mode 100644 index 0f167effef1..00000000000 --- a/acceptance/bundle/resources/volumes/removed_from_renamed_schema/out.test.toml +++ /dev/null @@ -1,2 +0,0 @@ -Cloud = true -EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/volumes/removed_from_renamed_schema/output.txt b/acceptance/bundle/resources/volumes/removed_from_renamed_schema/output.txt deleted file mode 100644 index d67a9191ac7..00000000000 --- a/acceptance/bundle/resources/volumes/removed_from_renamed_schema/output.txt +++ /dev/null @@ -1,56 +0,0 @@ - -=== Deploy a schema with a volume in it, and rename the schema out of band - ->>> [CLI] bundle deploy -Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... -Created schemas.schema1 -Created volumes.volume1 -Files: 3 uploaded, 0 deleted -Resources: 2 created, 0 changed, 0 deleted, 0 unchanged - ->>> [CLI] schemas update main.test-schema-[UNIQUE_NAME] --new-name test-schema-renamed-[UNIQUE_NAME] -{ - "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", - "schema_id": "[SCHEMA_ID]" -} - -=== Follow the rename in the config and remove the volume from it: the plan keeps the schema and finds the volume under its new name - ->>> [CLI] bundle plan -delete volumes.volume1 - -Plan: 0 to add, 0 to change, 1 to delete, 1 unchanged - -=== Deploy deletes the volume at its new name - ->>> [CLI] bundle deploy --auto-approve -Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... - -This action will result in the deletion or recreation of the following volumes. -For managed volumes, the files stored in the volume are also deleted from your -cloud tenant within 30 days. For external volumes, the metadata about the volume -is removed from the catalog, but the underlying files are not deleted: - delete resources.volumes.volume1 -Deleted volumes.volume1 -Files: 2 uploaded, 0 deleted -Resources: 0 created, 0 changed, 1 deleted, 1 unchanged - ->>> print_requests.py //unity-catalog -{ - "method": "DELETE", - "path": "/api/2.1/unity-catalog/volumes/main.test-schema-renamed-[UNIQUE_NAME].myvolume" -} - ->>> musterr [CLI] volumes read main.test-schema-renamed-[UNIQUE_NAME].myvolume -Error: Volume 'main.test-schema-renamed-[UNIQUE_NAME].myvolume' does not exist. - ->>> [CLI] bundle destroy --auto-approve -The following resources will be deleted: - delete resources.schemas.schema1 - -This action will result in the deletion of the following UC schemas. Any underlying data may be lost: - delete resources.schemas.schema1 - -All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default - -Destroy: 1 deleted diff --git a/acceptance/bundle/resources/volumes/removed_from_renamed_schema/script b/acceptance/bundle/resources/volumes/removed_from_renamed_schema/script deleted file mode 100644 index 6e768e643cb..00000000000 --- a/acceptance/bundle/resources/volumes/removed_from_renamed_schema/script +++ /dev/null @@ -1,28 +0,0 @@ -envsubst < databricks.yml.tmpl > databricks.yml - -OLD_SCHEMA="main.test-schema-${UNIQUE_NAME}" -NEW_SCHEMA="main.test-schema-renamed-${UNIQUE_NAME}" - -cleanup() { - trace $CLI bundle destroy --auto-approve - - # Consume the destroy's requests so none are left recorded. - print_requests.py //unity-catalog &> LOG.destroy_requests -} -trap cleanup EXIT - -title "Deploy a schema with a volume in it, and rename the schema out of band" -trace $CLI bundle deploy -trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields -print_requests.py //unity-catalog &> LOG.deploy_requests -add_repl "$($CLI schemas get "$NEW_SCHEMA" | jq -r .schema_id)" SCHEMA_ID -add_repl "$($CLI volumes read "$NEW_SCHEMA.myvolume" | jq -r .volume_id)" VOLUME_ID - -title "Follow the rename in the config and remove the volume from it: the plan keeps the schema and finds the volume under its new name" -envsubst < databricks-renamed-without-volume.yml.tmpl > databricks.yml -trace $CLI bundle plan - -title "Deploy deletes the volume at its new name" -trace $CLI bundle deploy --auto-approve -trace print_requests.py //unity-catalog -trace musterr $CLI volumes read "$NEW_SCHEMA.myvolume" diff --git a/acceptance/bundle/resources/volumes/removed_from_renamed_schema/test.toml b/acceptance/bundle/resources/volumes/removed_from_renamed_schema/test.toml deleted file mode 100644 index 4272a80cf61..00000000000 --- a/acceptance/bundle/resources/volumes/removed_from_renamed_schema/test.toml +++ /dev/null @@ -1,7 +0,0 @@ -Cloud = true - -Ignore = [ - ".databricks", - ".gitignore", - "databricks.yml", -] diff --git a/acceptance/bundle/resources/volumes/select_renamed_schema/databricks-empty.yml.tmpl b/acceptance/bundle/resources/volumes/select_renamed_schema/databricks-empty.yml.tmpl deleted file mode 100644 index 244a11badf3..00000000000 --- a/acceptance/bundle/resources/volumes/select_renamed_schema/databricks-empty.yml.tmpl +++ /dev/null @@ -1,2 +0,0 @@ -bundle: - name: test-bundle-$UNIQUE_NAME diff --git a/acceptance/bundle/resources/volumes/select_renamed_schema/databricks-schema-only.yml.tmpl b/acceptance/bundle/resources/volumes/select_renamed_schema/databricks-schema-only.yml.tmpl deleted file mode 100644 index 6f97649e503..00000000000 --- a/acceptance/bundle/resources/volumes/select_renamed_schema/databricks-schema-only.yml.tmpl +++ /dev/null @@ -1,8 +0,0 @@ -bundle: - name: test-bundle-$UNIQUE_NAME - -resources: - schemas: - schema1: - catalog_name: main - name: Test-Schema-$UNIQUE_NAME diff --git a/acceptance/bundle/resources/volumes/select_renamed_schema/databricks.yml.tmpl b/acceptance/bundle/resources/volumes/select_renamed_schema/databricks.yml.tmpl deleted file mode 100644 index 278bc421df8..00000000000 --- a/acceptance/bundle/resources/volumes/select_renamed_schema/databricks.yml.tmpl +++ /dev/null @@ -1,13 +0,0 @@ -bundle: - name: test-bundle-$UNIQUE_NAME - -resources: - schemas: - schema1: - catalog_name: main - name: Test-Schema-$UNIQUE_NAME - volumes: - volume1: - catalog_name: main - schema_name: test-schema-$UNIQUE_NAME - name: myvolume diff --git a/acceptance/bundle/resources/volumes/select_renamed_schema/out.test.toml b/acceptance/bundle/resources/volumes/select_renamed_schema/out.test.toml deleted file mode 100644 index 0f167effef1..00000000000 --- a/acceptance/bundle/resources/volumes/select_renamed_schema/out.test.toml +++ /dev/null @@ -1,2 +0,0 @@ -Cloud = true -EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/volumes/select_renamed_schema/output.txt b/acceptance/bundle/resources/volumes/select_renamed_schema/output.txt deleted file mode 100644 index eef064bddb5..00000000000 --- a/acceptance/bundle/resources/volumes/select_renamed_schema/output.txt +++ /dev/null @@ -1,71 +0,0 @@ - -=== Deploy a schema, then a volume that names it in lower case: the volume has no dependency on the schema - ->>> [CLI] bundle deploy -Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... -Created schemas.schema1 -Files: 4 uploaded, 0 deleted -Resources: 1 created, 0 changed, 0 deleted, 0 unchanged - ->>> [CLI] bundle deploy -Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... -Created volumes.volume1 -Files: 1 uploaded, 0 deleted -Resources: 1 created, 0 changed, 0 deleted, 1 unchanged - -=== Rename the schema and the volume out of band, and remove both from the config - ->>> [CLI] schemas update main.test-schema-[UNIQUE_NAME] --new-name test-schema-renamed-[UNIQUE_NAME] -{ - "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", - "schema_id": "[UUID][0]" -} - ->>> [CLI] volumes update main.test-schema-renamed-[UNIQUE_NAME].myvolume --new-name myvolume2 -{ - "full_name": "main.test-schema-renamed-[UNIQUE_NAME].myvolume2", - "volume_id": "[UUID][1]" -} - -=== Selecting only the volume fails: only the schema's delete removes it, with everything in it - ->>> musterr [CLI] bundle deploy --select volumes.volume1 --auto-approve -Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... -Error: cannot delete volumes.volume1 on its own: only deleting schemas.schema1, which was renamed outside the bundle, removes it; also select schemas.schema1 - -Files: 3 uploaded, 0 deleted - ->>> print_requests.py //unity-catalog - -=== Selecting only the schema still lists the volume, which its delete takes along - ->>> [CLI] bundle deploy --select schemas.schema1 --auto-approve -Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... - -This action will result in the deletion or recreation of the following UC schemas. Any underlying data may be lost: - delete resources.schemas.schema1 - -This action will result in the deletion or recreation of the following volumes. -For managed volumes, the files stored in the volume are also deleted from your -cloud tenant within 30 days. For external volumes, the metadata about the volume -is removed from the catalog, but the underlying files are not deleted: - delete resources.volumes.volume1 -Deleted schemas.schema1 -Deleted volumes.volume1 -Files: 0 uploaded, 0 deleted -Resources: 0 created, 0 changed, 2 deleted, 0 unchanged - ->>> print_requests.py //unity-catalog -{ - "method": "DELETE", - "path": "/api/2.1/unity-catalog/schemas/main.test-schema-renamed-[UNIQUE_NAME]", - "q": { - "force": "true" - } -} - ->>> musterr [CLI] schemas get main.test-schema-renamed-[UNIQUE_NAME] -Error: Schema 'main.test-schema-renamed-[UNIQUE_NAME]' does not exist. - ->>> summary_ids -{} diff --git a/acceptance/bundle/resources/volumes/select_renamed_schema/script b/acceptance/bundle/resources/volumes/select_renamed_schema/script deleted file mode 100644 index e92ee072fd8..00000000000 --- a/acceptance/bundle/resources/volumes/select_renamed_schema/script +++ /dev/null @@ -1,33 +0,0 @@ -OLD_SCHEMA="main.test-schema-${UNIQUE_NAME}" -NEW_SCHEMA="main.test-schema-renamed-${UNIQUE_NAME}" - -cleanup() { - errcode $CLI schemas delete "$NEW_SCHEMA" --force > LOG.cleanup 2>&1 - - # Consume the destroy's requests so none are left recorded. - print_requests.py //unity-catalog &> LOG.destroy_requests -} -trap cleanup EXIT - -title "Deploy a schema, then a volume that names it in lower case: the volume has no dependency on the schema" -envsubst < databricks-schema-only.yml.tmpl > databricks.yml -trace $CLI bundle deploy -envsubst < databricks.yml.tmpl > databricks.yml -trace $CLI bundle deploy -print_requests.py //unity-catalog &> LOG.deploy_requests - -title "Rename the schema and the volume out of band, and remove both from the config" -trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields -trace $CLI volumes update "$NEW_SCHEMA.myvolume" --new-name myvolume2 | volume_fields -envsubst < databricks-empty.yml.tmpl > databricks.yml -print_requests.py //unity-catalog &> LOG.rename_requests - -title "Selecting only the volume fails: only the schema's delete removes it, with everything in it" -trace musterr $CLI bundle deploy --select volumes.volume1 --auto-approve -trace print_requests.py //unity-catalog - -title "Selecting only the schema still lists the volume, which its delete takes along" -trace $CLI bundle deploy --select schemas.schema1 --auto-approve -trace print_requests.py //unity-catalog -trace musterr $CLI schemas get "$NEW_SCHEMA" -trace summary_ids diff --git a/acceptance/bundle/resources/volumes/select_renamed_schema/test.toml b/acceptance/bundle/resources/volumes/select_renamed_schema/test.toml deleted file mode 100644 index 4272a80cf61..00000000000 --- a/acceptance/bundle/resources/volumes/select_renamed_schema/test.toml +++ /dev/null @@ -1,7 +0,0 @@ -Cloud = true - -Ignore = [ - ".databricks", - ".gitignore", - "databricks.yml", -] diff --git a/acceptance/bundle/resources/volumes/with_parent_delete_fails/databricks.yml.tmpl b/acceptance/bundle/resources/volumes/with_parent_delete_fails/databricks.yml.tmpl deleted file mode 100644 index 2234fdf2e39..00000000000 --- a/acceptance/bundle/resources/volumes/with_parent_delete_fails/databricks.yml.tmpl +++ /dev/null @@ -1,13 +0,0 @@ -bundle: - name: test-bundle-$UNIQUE_NAME - -resources: - schemas: - schema1: - catalog_name: main - name: test-schema-$UNIQUE_NAME - volumes: - volume1: - catalog_name: main - schema_name: ${resources.schemas.schema1.name} - name: myvolume diff --git a/acceptance/bundle/resources/volumes/with_parent_delete_fails/out.test.toml b/acceptance/bundle/resources/volumes/with_parent_delete_fails/out.test.toml deleted file mode 100644 index a927a5fbc06..00000000000 --- a/acceptance/bundle/resources/volumes/with_parent_delete_fails/out.test.toml +++ /dev/null @@ -1,2 +0,0 @@ -Cloud = false -EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/volumes/with_parent_delete_fails/output.txt b/acceptance/bundle/resources/volumes/with_parent_delete_fails/output.txt deleted file mode 100644 index ec92ba2caa7..00000000000 --- a/acceptance/bundle/resources/volumes/with_parent_delete_fails/output.txt +++ /dev/null @@ -1,68 +0,0 @@ - -=== Deploy a schema with a volume in it, and rename both out of band - ->>> [CLI] bundle deploy -Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... -Created schemas.schema1 -Created volumes.volume1 -Files: 2 uploaded, 0 deleted -Resources: 2 created, 0 changed, 0 deleted, 0 unchanged - ->>> [CLI] schemas update main.test-schema-[UNIQUE_NAME] --new-name test-schema-renamed-[UNIQUE_NAME] -{ - "full_name": "main.test-schema-renamed-[UNIQUE_NAME]", - "schema_id": "[UUID][0]" -} - ->>> [CLI] volumes update main.test-schema-renamed-[UNIQUE_NAME].myvolume --new-name myvolume2 -{ - "full_name": "main.test-schema-renamed-[UNIQUE_NAME].myvolume2", - "volume_id": "[UUID][1]" -} - -=== The volume goes with the schema's delete, which fails: the volume stays in the state with the schema - ->>> musterr [CLI] bundle destroy --auto-approve -The following resources will be deleted: - delete resources.schemas.schema1 - delete resources.volumes.volume1 - -This action will result in the deletion of the following UC schemas. Any underlying data may be lost: - delete resources.schemas.schema1 - -This action will result in the deletion of the following volumes. -For managed volumes, the files stored in the volume are also deleted from your -cloud tenant within 30 days. For external volumes, the metadata about the volume -is removed from the catalog, but the underlying files are not deleted: - delete resources.volumes.volume1 - -All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default - -Error: cannot delete resources.schemas.schema1: deleting id=main.test-schema-renamed-[UNIQUE_NAME]: Fault injected by test. (400 INVALID_PARAMETER_VALUE) - -Endpoint: DELETE [DATABRICKS_URL]/api/2.1/unity-catalog/schemas/main.test-schema-renamed-[UNIQUE_NAME]?force=true -HTTP Status: 400 Bad Request -API error_code: INVALID_PARAMETER_VALUE -API message: Fault injected by test. - -Error: cannot delete resources.volumes.volume1: dependency failed: resources.schemas.schema1 - - ->>> print_requests.py //unity-catalog -{ - "method": "DELETE", - "path": "/api/2.1/unity-catalog/schemas/main.test-schema-renamed-[UNIQUE_NAME]", - "q": { - "force": "true" - } -} - ->>> summary_ids -{ - "schemas": { - "schema1": "main.test-schema-[UNIQUE_NAME]" - }, - "volumes": { - "volume1": "main.test-schema-[UNIQUE_NAME].myvolume" - } -} diff --git a/acceptance/bundle/resources/volumes/with_parent_delete_fails/script b/acceptance/bundle/resources/volumes/with_parent_delete_fails/script deleted file mode 100644 index efdeedf467d..00000000000 --- a/acceptance/bundle/resources/volumes/with_parent_delete_fails/script +++ /dev/null @@ -1,16 +0,0 @@ -envsubst < databricks.yml.tmpl > databricks.yml - -OLD_SCHEMA="main.test-schema-${UNIQUE_NAME}" -NEW_SCHEMA="main.test-schema-renamed-${UNIQUE_NAME}" - -title "Deploy a schema with a volume in it, and rename both out of band" -trace $CLI bundle deploy -trace $CLI schemas update "$OLD_SCHEMA" --new-name "test-schema-renamed-${UNIQUE_NAME}" | schema_fields -trace $CLI volumes update "$NEW_SCHEMA.myvolume" --new-name myvolume2 | volume_fields -print_requests.py //unity-catalog &> LOG.deploy_requests - -title "The volume goes with the schema's delete, which fails: the volume stays in the state with the schema" -fault.py "DELETE /api/2.1/unity-catalog/schemas/*" 400 0 1 INVALID_PARAMETER_VALUE -trace musterr $CLI bundle destroy --auto-approve -trace print_requests.py //unity-catalog -trace summary_ids diff --git a/acceptance/bundle/resources/volumes/with_parent_delete_fails/test.toml b/acceptance/bundle/resources/volumes/with_parent_delete_fails/test.toml deleted file mode 100644 index 4545cc36529..00000000000 --- a/acceptance/bundle/resources/volumes/with_parent_delete_fails/test.toml +++ /dev/null @@ -1,8 +0,0 @@ -# Local only: the failure of the schema's delete is injected. -Cloud = false - -Ignore = [ - ".databricks", - ".gitignore", - "databricks.yml", -] diff --git a/bundle/deployplan/action.go b/bundle/deployplan/action.go index 0cbcdcde685..698c11e56b1 100644 --- a/bundle/deployplan/action.go +++ b/bundle/deployplan/action.go @@ -9,16 +9,12 @@ type Action struct { // Full resource key, e.g. "resources.jobs.foo" or "resources.jobs.foo.permissions" ResourceKey string ActionType ActionType - // Gone mirrors PlanEntry.Gone: applying the delete sends no delete call and only removes the - // state entry, as planning did not find the resource remotely (or the state entry has an empty - // ID). With WithParent, the resource is most likely still there until its parent's delete - // removes it, so the action is shown and approved as a deletion. + // Gone mirrors PlanEntry.Gone: the delete is a state-only cleanup because the + // resource no longer exists remotely (confirmed by a read, or an empty-ID state entry). Gone bool // StateOnly mirrors PlanEntry.StateOnly: applying the action only changes the state // (a delete of a resource without DoDelete, or a re-key). StateOnly bool - // WithParent mirrors PlanEntry.WithParent: the resource goes with its parent's delete. - WithParent bool } func (a Action) String() string { @@ -27,9 +23,9 @@ func (a Action) String() string { // IsStateOnly reports whether applying this action makes no backend call and only changes the // state (see PlanEntry.IsStateOnly). Such actions are omitted from human output and need no -// approval. An action with WithParent is not: its parent's delete removes the resource. +// approval. func (a Action) IsStateOnly() bool { - return (a.Gone || a.StateOnly) && !a.WithParent + return a.Gone || a.StateOnly } func (a Action) IsChildResource() bool { diff --git a/bundle/deployplan/plan.go b/bundle/deployplan/plan.go index 8e1983b9981..6fcaa333e08 100644 --- a/bundle/deployplan/plan.go +++ b/bundle/deployplan/plan.go @@ -125,27 +125,17 @@ func LoadPlanFromFile(path string) (*Plan, error) { type PlanEntry struct { // ID is set when the resource is at another ID than the one in state: a rename moved it there - // (see findMoved, and findByInternalID and findInMovedParents for a Delete), or a parent renamed - // in this plan moves it before it is recreated (see addParentMoves). Applying the entry - // addresses the resource at ID. + // (see findMoved), or a parent renamed in this plan moves it before it is recreated (see + // addParentMoves). Applying the entry addresses the resource at ID. ID string `json:"id,omitempty"` DependsOn []DependsOnEntry `json:"depends_on,omitempty"` Action ActionType `json:"action,omitempty"` - // Gone is set on Delete entries when planning did not find the resource remotely, or the state - // entry has an empty ID (left by a recreate in an older CLI whose create step failed after the - // delete). Applying such an entry only removes it from the state, without calling the delete - // API, and approval prompts do not list it as a deletion unless WithParent is set. - // A resource with an internal ID is not found when it is not at its recorded ID (another object - // may be there), not found by findByInternalID (a search the user may not run finds nothing), - // and not under a renamed parent (see findInMovedParents). + // Gone is set on Delete entries when the resource no longer exists remotely: planning + // confirmed it with a read, or the state entry has an empty ID (left by a recreate in + // an older CLI whose create step failed after the delete). Applying such an entry only + // removes it from the state, without calling the delete API, and approval prompts do + // not list it as a deletion. Gone bool `json:"gone,omitempty"` - // WithParent is set on a Gone Delete entry whose resource is inside a parent this plan deletes - // at the new ID a rename out of band gave it (a volume in a renamed schema), but could not be - // verified there by its internal ID. Applying it sends no delete for it, which could hit another - // object under the guessed name, and only removes it from the state; the parent's delete - // removes it with everything else in the parent. So approval prompts, lifecycle.prevent_destroy - // and the counts treat it as a deletion. It holds the parent's resource key. - WithParent string `json:"with_parent,omitempty"` // StateOnly marks an entry whose apply has no backend effect and only changes the state: // - a Delete of a resource that implements no DoDelete. Like Gone, applying it only // removes it from the state and it is excluded from destructive-action prompts, @@ -164,17 +154,10 @@ type PlanEntry struct { Changes Changes `json:"changes,omitempty"` } -// IsStateOnly reports whether applying this entry has no backend effect and only changes the +// IsStateOnly reports whether applying this entry makes no backend call and only changes the // state: a delete of a resource already gone remotely (Gone) or without a delete operation, or a -// re-key (StateOnly). Such entries are omitted from human output and need no approval. A Gone -// entry with WithParent is not: its parent's delete removes it. +// re-key (StateOnly). Such entries are omitted from human output and need no approval. func (e *PlanEntry) IsStateOnly() bool { - return (e.Gone || e.StateOnly) && e.WithParent == "" -} - -// SendsNoDelete reports whether applying this Delete entry only removes it from the state, without -// calling the delete API: it is state-only, or its parent's delete removes it (WithParent). -func (e *PlanEntry) SendsNoDelete() bool { return e.Gone || e.StateOnly } @@ -290,7 +273,6 @@ func (p *Plan) GetActions() []Action { ActionType: entry.Action, Gone: entry.Gone, StateOnly: entry.StateOnly, - WithParent: entry.WithParent != "", }) } @@ -394,8 +376,6 @@ func ResolveSelectors(resourceKeys iter.Seq[string], selectors []string) ([]stri // dependencies (forward), a deleted one its dependents (reverse) so they are removed // together. A delete selection errors if a live resource (not itself being deleted) // still references the resource to be deleted; removing it would break the live resource. -// It also errors for a WithParent delete whose parent is not in the closure, as the -// parent's delete is the only one that removes it. func (p *Plan) FilterToSelected(_ context.Context, selected []string) error { before := len(p.Plan) @@ -462,17 +442,6 @@ func (p *Plan) FilterToSelected(_ context.Context, selected []string) error { } } } - for key := range reachable { - // Only its parent's delete removes it, and that takes everything in the parent along: the - // parent has to be in the selection already (named, or pulled in as a dependent). - if parent := p.Plan[key].WithParent; p.Plan[key].Action == Delete && parent != "" { - if _, ok := reachable[parent]; !ok { - deleted := strings.TrimPrefix(key, "resources.") - parentName := strings.TrimPrefix(parent, "resources.") - errs = append(errs, fmt.Sprintf("cannot delete %s on its own: only deleting %s, which was renamed outside the bundle, removes it; also select %s", deleted, parentName, parentName)) - } - } - } if len(errs) > 0 { slices.Sort(errs) return errors.New(strings.Join(errs, "\n")) diff --git a/bundle/deployplan/plan_filter_test.go b/bundle/deployplan/plan_filter_test.go index 603ff64117a..8c6dd30240e 100644 --- a/bundle/deployplan/plan_filter_test.go +++ b/bundle/deployplan/plan_filter_test.go @@ -1,7 +1,6 @@ package deployplan_test import ( - "maps" "slices" "testing" @@ -284,38 +283,3 @@ func TestFilterToSelected_DeletionLeavesDependencies(t *testing.T) { assert.Contains(t, p.Plan, "resources.jobs.gone") assert.NotContains(t, p.Plan, "resources.jobs.lib") } - -func TestFilterToSelected_WithParentNeedsItsParentSelected(t *testing.T) { - newPlan := func() *deployplan.Plan { - p := deployplan.NewPlanDirect() - schemaDep := deployplan.DependsOnEntry{Node: "resources.schemas.s"} - p.Plan["resources.schemas.s"] = &deployplan.PlanEntry{Action: deployplan.Delete, ID: "main.moved"} - p.Plan["resources.volumes.v"] = &deployplan.PlanEntry{Action: deployplan.Delete, Gone: true, WithParent: "resources.schemas.s", DependsOn: []deployplan.DependsOnEntry{schemaDep}} - p.Plan["resources.volumes.w"] = &deployplan.PlanEntry{Action: deployplan.Delete, DependsOn: []deployplan.DependsOnEntry{schemaDep}} - p.Plan["resources.volumes.other"] = &deployplan.PlanEntry{Action: deployplan.Delete} - return p - } - - // Only the parent's delete removes it, and that one takes w along too: the user has to ask for it. - p := newPlan() - err := p.FilterToSelected(t.Context(), []string{"volumes.v"}) - require.EqualError(t, err, "cannot delete volumes.v on its own: only deleting schemas.s, which was renamed outside the bundle, removes it; also select schemas.s") - assert.Len(t, p.Plan, 4) - - p = newPlan() - require.NoError(t, p.FilterToSelected(t.Context(), []string{"volumes.v", "schemas.s"})) - assert.ElementsMatch(t, []string{"resources.schemas.s", "resources.volumes.v", "resources.volumes.w"}, slices.Collect(maps.Keys(p.Plan))) -} - -func TestFilterToSelected_WithParentWhoseParentIsPulledIn(t *testing.T) { - p := deployplan.NewPlanDirect() - p.Plan["resources.catalogs.c"] = &deployplan.PlanEntry{Action: deployplan.Delete} - p.Plan["resources.schemas.s"] = &deployplan.PlanEntry{Action: deployplan.Delete, ID: "c.moved", DependsOn: []deployplan.DependsOnEntry{{Node: "resources.catalogs.c"}}} - p.Plan["resources.volumes.v"] = &deployplan.PlanEntry{Action: deployplan.Delete, Gone: true, WithParent: "resources.schemas.s", DependsOn: []deployplan.DependsOnEntry{{Node: "resources.schemas.s"}}} - p.Plan["resources.volumes.other"] = &deployplan.PlanEntry{Action: deployplan.Delete} - - // The catalog pulls in the schema as its dependent, and the schema the volume: the schema is - // deleted in this run, so the volume can go with it. - require.NoError(t, p.FilterToSelected(t.Context(), []string{"catalogs.c"})) - assert.ElementsMatch(t, []string{"resources.catalogs.c", "resources.schemas.s", "resources.volumes.v"}, slices.Collect(maps.Keys(p.Plan))) -} diff --git a/bundle/deployplan/plan_test.go b/bundle/deployplan/plan_test.go index 2d1bfc9561a..1d93583514a 100644 --- a/bundle/deployplan/plan_test.go +++ b/bundle/deployplan/plan_test.go @@ -113,21 +113,3 @@ func TestHasOnlySingleReason(t *testing.T) { }) } } - -func TestWithParentIsADeleteThatSendsNoCall(t *testing.T) { - gone := &deployplan.PlanEntry{Action: deployplan.Delete, Gone: true} - withParent := &deployplan.PlanEntry{Action: deployplan.Delete, Gone: true, WithParent: "resources.schemas.s"} - - // A resource already gone is hidden and needs no call. - assert.True(t, gone.IsStateOnly()) - assert.True(t, gone.SendsNoDelete()) - - // One that goes with its parent's delete is shown and approved as a deletion, but no delete is - // sent for it. - assert.False(t, withParent.IsStateOnly()) - assert.True(t, withParent.SendsNoDelete()) - - plan := &deployplan.Plan{Plan: map[string]*deployplan.PlanEntry{"resources.volumes.v": withParent}} - actions := plan.GetActions() - assert.False(t, actions[0].IsStateOnly()) -} diff --git a/bundle/direct/apply.go b/bundle/direct/apply.go index 8d0ba0cefac..d886b478886 100644 --- a/bundle/direct/apply.go +++ b/bundle/direct/apply.go @@ -1,7 +1,6 @@ package direct import ( - "cmp" "context" "encoding/json" "errors" @@ -24,9 +23,7 @@ func (d *DeploymentUnit) withResourceKey(ctx context.Context) context.Context { return dresources.WithResourceKey(ctx, strings.TrimPrefix(d.ResourceKey, "resources.")) } -// Destroy deletes the resource at its recorded ID, or at movedID when the plan found it there (see -// findByInternalID and findInMovedParents). -func (d *DeploymentUnit) Destroy(ctx context.Context, db *dstate.DeploymentState, movedID string) error { +func (d *DeploymentUnit) Destroy(ctx context.Context, db *dstate.DeploymentState) error { ctx = log.WithPrefix(ctx, "destroying "+d.ResourceKey) ctx = d.withResourceKey(ctx) id := db.GetResourceID(d.ResourceKey) @@ -35,7 +32,7 @@ func (d *DeploymentUnit) Destroy(ctx context.Context, db *dstate.DeploymentState return nil } - return d.Delete(ctx, db, cmp.Or(movedID, id)) + return d.Delete(ctx, db, id) } func (d *DeploymentUnit) Deploy(ctx context.Context, db *dstate.DeploymentState, newState any, actionType deployplan.ActionType, planEntry *deployplan.PlanEntry) error { diff --git a/bundle/direct/bundle_apply.go b/bundle/direct/bundle_apply.go index dad1bf6fabb..60e1b66bb47 100644 --- a/bundle/direct/bundle_apply.go +++ b/bundle/direct/bundle_apply.go @@ -115,14 +115,13 @@ func (b *DeploymentBundle) Apply(ctx context.Context, client *databricks.Workspa } if action == deployplan.Delete { - if entry.SendsNoDelete() { - // The resource is already deleted remotely (Gone), has no delete - // operation (StateOnly), or goes with its parent's delete (WithParent); - // in each case only remove it from the state, without calling the - // delete API. + if entry.IsStateOnly() { + // The resource is already deleted remotely (Gone) or has no delete + // operation (StateOnly); either way only remove it from the state, + // without calling the delete API. err = b.StateDB.DeleteState(ctx, resourceKey, false) } else { - err = d.Destroy(ctx, &b.StateDB, entry.ID) + err = d.Destroy(ctx, &b.StateDB) } if err != nil { logdiag.LogError(ctx, fmt.Errorf("%s: %w", errorPrefix, err)) diff --git a/bundle/direct/bundle_plan.go b/bundle/direct/bundle_plan.go index adc488506b8..9b53d0ede01 100644 --- a/bundle/direct/bundle_plan.go +++ b/bundle/direct/bundle_plan.go @@ -246,42 +246,35 @@ func (b *DeploymentBundle) CalculatePlan(ctx context.Context, client *databricks remoteState, err := retry.With(ctx, retry.IsTransientRead, func() (any, error) { return adapter.DoRead(ctx, id) }) - missing := false if err != nil { if apierr.IsMissing(err) { - missing = true - remoteState = nil - } else if !adapter.HasInternalID() { + // The resource is already deleted remotely. Keep the Delete entry so + // that applying it removes the stale state entry, but mark it Gone so + // apply skips the delete call and prompts don't list it as a deletion. + entry.Gone = true + } else { log.Warnf(ctx, "reading %s id=%q: %s", resourceKey, id, err) // This is not an error during deletion, so don't return false here } - // For a resource with an internal ID, findDeleteTarget decides: the read is how - // the plan tells the bundle's resource from another one under its name. } else if adapter.IsGone(remoteState) { // The resource is in a transient terminal-teardown state (e.g. an app in // DELETING) that a GET still returns but a second delete would reject. // Treat it as gone: apply cleans up state without re-issuing the delete. entry.Gone = true - } - - if adapter.HasInternalID() && !entry.Gone { - var readErr error - if !missing { - readErr = err - } - entry.ID, remoteState, err = b.findDeleteTarget(ctx, resourceKey, adapter, id, remoteState, readErr) + } else { + // Someone else's resource under the name ours had is never deleted: it is left in + // place, and only the state entry is dropped. + message, err := b.otherObjectAtRecordedID(resourceKey, adapter, id, remoteState) if err != nil { logdiag.LogError(ctx, fmt.Errorf("%s: %w", errorPrefix, err)) return false } - // After a failed read (a permission error), the delete goes to the recorded ID, as before. - missing = remoteState == nil && readErr == nil - } - if missing { - // The resource is already deleted remotely. Keep the Delete entry so - // that applying it removes the stale state entry, but mark it Gone so - // apply skips the delete call and prompts don't list it as a deletion. - entry.Gone = true + if message != "" { + logdiag.LogDiag(ctx, diag.Warningf("%s: %s, and drops %s from the deployment state. %s", + resourceKey, message, resourceKey, otherObjectAdvice)[0]) + entry.Gone = true + remoteState = nil + } } entry.RemoteState = remoteState @@ -389,8 +382,8 @@ func (b *DeploymentBundle) CalculatePlan(ctx context.Context, client *databricks } } if otherID != "" && remoteState == nil { - logdiag.LogDiag(ctx, diag.Warningf("%s: %s. "+otherObjectAdvice, - resourceKey, otherObjectMessage(adapter, dbentry.ID, savedState, otherID))[0]) + logdiag.LogDiag(ctx, diag.Warningf("%s: %s. %s", + resourceKey, otherObjectMessage(adapter, dbentry.ID, savedState, otherID), otherObjectAdvice)[0]) } // We have a choice whether to include remoteState or remoteStateComparable from below. @@ -488,18 +481,6 @@ func (b *DeploymentBundle) CalculatePlan(ctx context.Context, client *databricks return nil, err } - if err := b.findInMovedParents(ctx, plan); err != nil { - return nil, err - } - b.warnOtherObjects(ctx, plan) - - // findInMovedParents can add dependencies on a moved parent. - if g, err := makeGraph(plan); err != nil { - return nil, err - } else if err := g.DetectCycle(); err != nil { - return nil, err - } - for _, entry := range plan.Plan { if entry.Action == deployplan.Skip { entry.NewState = nil @@ -918,186 +899,62 @@ func isParentIDField(adapter *dresources.Adapter, path *structpath.PathNode) boo return ok } -// findInMovedParents handles a Delete of a resource not found at its recorded ID (Gone) while its -// parent was found at another ID in this plan: renamed out of band (see findMoved and -// findByInternalID). Only a resource that lives in a UC parent (ParentIDFields, MovesWithParent) -// moves with it, so it is looked for under the parent's new ID. It is deleted there as itself if it -// has its recorded internal ID, or, having none to check (a model, a secret, a feature), if it is -// found there inside the bundle's parent, which stays. Otherwise, under a parent that is deleted, it -// is marked WithParent, found or not (a 404 cannot tell a resource renamed too from one deleted): no -// delete is sent for it, but approval and lifecycle.prevent_destroy see that the parent's delete -// takes it along. Under a parent that stays, it stays Gone. -func (b *DeploymentBundle) findInMovedParents(ctx context.Context, plan *deployplan.Plan) error { - parents, err := b.movedParents(plan) - if err != nil || len(parents) == 0 { - return err +// otherInternalID returns the internal ID of remote, read at the recorded ID, if it is another +// object than the one savedState records: ours was renamed away and another one created under its +// old name. It is "" when the resource is ours, or when either side has no internal ID to compare. +func otherInternalID(adapter *dresources.Adapter, savedState, remote any) (string, error) { + if !adapter.HasInternalID() || remote == nil { + return "", nil } - - // A verified child is a parent for deeper ones (a resource in a volume, should one exist), so the - // shallower resources go first. - keys := slices.Collect(maps.Keys(plan.Plan)) - slices.SortFunc(keys, func(x, y string) int { - return cmp.Or( - cmp.Compare(strings.Count(b.StateDB.GetResourceID(x), "."), strings.Count(b.StateDB.GetResourceID(y), ".")), - cmp.Compare(x, y), - ) - }) - - for _, resourceKey := range keys { - entry := plan.Plan[resourceKey] - if entry.Action != deployplan.Delete || entry.ID != "" || (entry.StateOnly && !entry.Gone) { - continue - } - adapter, err := b.getAdapterForKey(resourceKey) - if err != nil { - return err - } - dbentry, ok := b.StateDB.GetResourceEntry(resourceKey) - if !ok { - continue - } - if !entry.Gone && entry.RemoteState != nil && hasRecordedInternalID(adapter, dbentry) { - // Verified at its recorded ID by its internal ID (see findDeleteTarget): it did not move. - continue - } - if config := adapter.ResourceConfig(); len(config.ParentIDFields) == 0 && !config.MovesWithParent { - // Not a resource that lives in a UC parent and moves with it: an ID that looks like a full - // name under the parent's (a secret scope named main.s.x) says nothing about where it is, - // found or Gone. - continue - } - parent, id := underMovedParent(parents, dbentry.ID) - if parent == nil { - continue - } - remote, verified, err := b.readUnderMovedParent(ctx, adapter, dbentry, id) - if err != nil { - return fmt.Errorf("%s: %w", resourceKey, err) - } - // Whatever is at the recorded ID now is not in the bundle's parent, so it is never the target. - entry.Gone = true - entry.RemoteState = nil - switch { - case verified || (remote != nil && !parent.deleted): - // Verified by its internal ID; or, without one to check, found at the name the rename gave it - // inside the bundle's own parent, which stays: deleted there as itself. - entry.ID = id - entry.RemoteState = remote - entry.Gone = false - if verified && adapter.HasIDFromState() { - parents = append(parents, &movedParent{key: resourceKey, recordedID: dbentry.ID, id: id, deleted: true}) - } - case parent.deleted: - // Not verified, or not found: a 404 cannot tell a resource renamed too (or moved by the - // parent's rename and then renamed) from one deleted, with or without an internal ID. The - // parent's delete removes it if it is there, so it is listed with it. - entry.WithParent = parent.key - default: - continue - } - if parent.deleted && !slices.ContainsFunc(entry.DependsOn, func(d deployplan.DependsOnEntry) bool { return d.Node == parent.key }) { - // Ordered with its parent (see makeGraph), and selected with it (see FilterToSelected), - // also when the config named the parent literally. - entry.DependsOn = append(entry.DependsOn, deployplan.DependsOnEntry{Node: parent.key}) - } + saved, err := adapter.InternalID(savedState) + if err != nil || saved == "" { + return "", err } - return nil + remapped, err := adapter.RemapState(remote) + if err != nil { + return "", err + } + found, err := adapter.InternalID(remapped) + if err != nil || found == saved { + return "", err + } + return found, nil } -// movedParent is a UC resource this plan found at another ID than its recorded one. -type movedParent struct { - key string - recordedID string - id string - // deleted reports whether the plan deletes it there, taking everything in it along. - deleted bool +// otherObjectMessage says that what is at the recorded ID id is not the bundle's: the plan leaves it +// alone. It is told when the bundle's own is not found elsewhere either; a resource deleted and +// created again out of band looks the same, hence otherObjectAdvice. +func otherObjectMessage(adapter *dresources.Adapter, id string, savedState any, otherID string) string { + saved, _ := adapter.InternalID(savedState) + return fmt.Sprintf("id=%q holds another object: its %s is %q, not %q as recorded, so this bundle leaves it in place", + id, adapter.ResourceConfig().InternalIDField, otherID, saved) } -// movedParents returns the UC resources (their ID is a full name, see IDFromState) the plan found at -// another ID than their recorded one: deleted there (findByInternalID), or re-keyed (findMoved). -func (b *DeploymentBundle) movedParents(plan *deployplan.Plan) ([]*movedParent, error) { - var parents []*movedParent - for _, key := range slices.Sorted(maps.Keys(plan.Plan)) { - entry := plan.Plan[key] - if entry.ID == "" || entry.Action == deployplan.Recreate || (entry.Action == deployplan.Delete && entry.Gone) { - continue - } - adapter, err := b.getAdapterForKey(key) - if err != nil { - return nil, err - } - recordedID := b.StateDB.GetResourceID(key) - if !adapter.HasIDFromState() || recordedID == "" || strings.EqualFold(recordedID, entry.ID) { - continue - } - parents = append(parents, &movedParent{key: key, recordedID: recordedID, id: entry.ID, deleted: entry.Action == deployplan.Delete}) - } - return parents, nil -} +// otherObjectAdvice follows otherObjectMessage, for a resource still in the config (deploy) as well as +// one removed from it (a Delete): bind needs the resource in the config, and a free state entry. +const otherObjectAdvice = "If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle, or manage it again with 'bundle deployment bind' while it is in the config ('bundle deployment unbind' first if the state still has it)." -// underMovedParent returns the parent whose recorded ID is the longest prefix of id (main.s for -// main.s.v), and id under the parent's new ID (main.t.v); nil if there is none. UC full names are -// case-insensitive, and the bundle can record a child in config case and its parent in UC's lower case. -func underMovedParent(parents []*movedParent, id string) (*movedParent, string) { - var best *movedParent - for _, parent := range parents { - prefix := parent.recordedID + "." - if len(id) > len(prefix) && strings.EqualFold(id[:len(prefix)], prefix) && (best == nil || len(parent.recordedID) > len(best.recordedID)) { - best = parent - } - } - if best == nil { - return nil, "" +// otherObjectAtRecordedID returns, for a resource to be deleted, what otherObjectMessage says when +// remote, read at its recorded ID id, is another object than the one the state records; "" when it +// is ours, or when there is no internal ID to tell (a state entry with only the ID). The bundle's +// own is not looked for: a renamed one is left behind, as before. +func (b *DeploymentBundle) otherObjectAtRecordedID(resourceKey string, adapter *dresources.Adapter, id string, remote any) (string, error) { + if !adapter.HasInternalID() { + return "", nil } - return best, best.id + "." + id[len(best.recordedID)+1:] -} - -// readUnderMovedParent reads the resource recorded in dbentry at id, under its parent's new ID. It -// returns what it read there (nil if nothing, or if the read is forbidden), and whether that is -// verified: it has the recorded internal ID. Something else there (another internal ID) is not -// returned. A read that fails other than with a missing resource or a permission error fails the -// plan. -func (b *DeploymentBundle) readUnderMovedParent(ctx context.Context, adapter *dresources.Adapter, dbentry dstate.ResourceEntry, id string) (remote any, verified bool, err error) { - savedInternalID := "" - if hasRecordedInternalID(adapter, dbentry) { - state, err := parseState(adapter.StateType(), dbentry.State) - if err != nil { - return nil, false, fmt.Errorf("interpreting state: %w", err) - } - if savedInternalID, err = adapter.InternalID(state); err != nil { - return nil, false, err - } + dbentry, _ := b.StateDB.GetResourceEntry(resourceKey) + if len(dbentry.State) == 0 { + return "", nil } - remote, err = retry.With(ctx, retry.IsTransientRead, func() (any, error) { - return adapter.DoRead(ctx, id) - }) + savedState, err := parseState(adapter.StateType(), dbentry.State) if err != nil { - if apierr.IsMissing(err) { - return nil, false, nil - } - return nil, false, ignoreMissingOrForbidden(ctx, err, fmt.Sprintf("reading id=%q under its renamed parent", id)) - } - if savedInternalID == "" { - return remote, false, nil + return "", fmt.Errorf("interpreting state: %w", err) } - ok, err := hasInternalIDOf(adapter, remote, savedInternalID) - if err != nil || !ok { - return nil, false, err - } - return remote, true, nil -} - -// hasRecordedInternalID reports whether the state in dbentry has the resource's internal ID. -func hasRecordedInternalID(adapter *dresources.Adapter, dbentry dstate.ResourceEntry) bool { - if !adapter.HasInternalID() || len(dbentry.State) == 0 { - return false - } - state, err := parseState(adapter.StateType(), dbentry.State) - if err != nil { - return false + otherID, err := otherInternalID(adapter, savedState, remote) + if err != nil || otherID == "" { + return "", err } - id, err := adapter.InternalID(state) - return err == nil && id != "" + return otherObjectMessage(adapter, id, savedState, otherID), nil } // recordRefTypes records, for each pure-reference field of resourceKey, the type of the resource it @@ -1175,182 +1032,17 @@ func findMoved(ctx context.Context, adapter *dresources.Adapter, sv *structvar.S log.Warnf(ctx, "looking for a moved resource at id=%q: %s", id, err) return "", nil, nil } - if ok, err := hasInternalIDOf(adapter, remote, savedInternalID); err != nil || !ok { - return "", nil, err - } - return id, remote, nil -} - -// hasInternalIDOf reports whether remote has the internal ID internalID: it is the resource the -// state records, wherever it is now. -func hasInternalIDOf(adapter *dresources.Adapter, remote any, internalID string) (bool, error) { - remapped, err := adapter.RemapState(remote) - if err != nil { - return false, err - } - found, err := adapter.InternalID(remapped) - return found == internalID, err -} - -// otherInternalID returns the internal ID of remote, read at the recorded ID, if it is another -// object than the one savedState records: ours was renamed away and another one created under its -// old name. It is "" when the resource is ours, or when either side has no internal ID to compare. -func otherInternalID(adapter *dresources.Adapter, savedState, remote any) (string, error) { - if !adapter.HasInternalID() || remote == nil { - return "", nil - } - saved, err := adapter.InternalID(savedState) - if err != nil || saved == "" { - return "", err - } - remapped, err := adapter.RemapState(remote) - if err != nil { - return "", err - } - found, err := adapter.InternalID(remapped) - if err != nil || found == saved { - return "", err - } - return found, nil -} - -// otherObjectMessage says that what is at the recorded ID id is not the bundle's: the plan leaves it -// alone. It is told when the bundle's own is not found elsewhere either; a resource deleted and -// created again out of band looks the same, hence otherObjectAdvice. -func otherObjectMessage(adapter *dresources.Adapter, id string, savedState any, otherID string) string { - saved, _ := adapter.InternalID(savedState) - return fmt.Sprintf("id=%q holds another object: its %s is %q, not %q as recorded, so this bundle leaves it in place", - id, adapter.ResourceConfig().InternalIDField, otherID, saved) -} - -// otherObjectAdvice follows otherObjectMessage, for a resource still in the config (deploy) as well as -// one removed from it (a Delete): bind needs the resource in the config, and a free state entry. -const otherObjectAdvice = "If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle, or manage it again with 'bundle deployment bind' while it is in the config ('bundle deployment unbind' first if the state still has it)." - -// warnOtherObjects warns about each Delete that leaves another object in place at its recorded ID -// (see findDeleteTarget), after findInMovedParents had its chance to find the bundle's own. -func (b *DeploymentBundle) warnOtherObjects(ctx context.Context, plan *deployplan.Plan) { - for _, resourceKey := range slices.Sorted(maps.Keys(plan.Plan)) { - message, ok := b.otherObjects.Load(resourceKey) - if !ok || plan.Plan[resourceKey].ID != "" { - continue - } - logdiag.LogDiag(ctx, diag.Warningf("%s: %s, and drops %s from the deployment state. "+otherObjectAdvice, - resourceKey, message, resourceKey)[0]) - } -} - -// findDeleteTarget returns where to delete a resource with an internal ID, given what a read at its -// recorded ID returned: there if it is ours, else where findByInternalID finds it. An empty ID means -// the recorded one. A nil remote means it is nowhere, except after a read that failed with a -// permission error (readErr), when the caller leaves the delete to the recorded ID, as before. -// Someone else's resource under the name ours had is never the target, and neither is a resource -// whose identity could not be checked: a read that failed for any other reason than a missing -// resource or a permission error fails the plan. -func (b *DeploymentBundle) findDeleteTarget(ctx context.Context, resourceKey string, adapter *dresources.Adapter, id string, remote any, readErr error) (string, any, error) { - dbentry, _ := b.StateDB.GetResourceEntry(resourceKey) - if len(dbentry.State) == 0 { - // Only the ID is recorded: there is no internal ID to check. - if readErr != nil { - log.Warnf(ctx, "reading %s id=%q: %s", resourceKey, id, readErr) - } - return "", remote, nil - } - savedState, err := parseState(adapter.StateType(), dbentry.State) - if err != nil { - return "", nil, fmt.Errorf("interpreting state: %w", err) - } - if saved, err := adapter.InternalID(savedState); err != nil || saved == "" { - if readErr != nil { - log.Warnf(ctx, "reading %s id=%q: %s", resourceKey, id, readErr) - } - return "", remote, err - } - if readErr != nil { - if errors.Is(readErr, apierr.ErrPermissionDenied) { - // What the user cannot read may be another object under the name ours had (whose owner - // did not grant access): look for ours first. Found nowhere, it is left to the delete - // call at the recorded ID, as before. - log.Warnf(ctx, "reading %s id=%q: %s", resourceKey, id, readErr) - return findByInternalID(ctx, adapter, savedState) - } - return "", nil, fmt.Errorf("cannot check that id=%q is still this bundle's: %w", id, readErr) - } - otherID, err := otherInternalID(adapter, savedState, remote) + found, err := adapter.RemapState(remote) if err != nil { return "", nil, err } - if remote != nil && otherID == "" { - return "", remote, nil - } - movedID, moved, err := findByInternalID(ctx, adapter, savedState) - if err == nil && moved == nil && otherID != "" { - // Told after findInMovedParents, which may still find it (see warnOtherObjects). - b.otherObjects.Store(resourceKey, otherObjectMessage(adapter, id, savedState, otherID)) - } - return movedID, moved, err -} - -// findByInternalID looks for a resource to be deleted that is not at its recorded ID: renamed out of -// band. A plan without config (destroy), or without this resource in it, has no new name to look at, -// as findMoved does, so the resource searches for it (DoFindByInternalID). As in findMoved, what is -// found is taken only if its internal ID matches. A search the user has no permission for finds -// nothing: the delete then goes to the recorded ID if the read there was forbidden too (see -// findDeleteTarget), else only drops the state entry, as it did before. Any other failure fails the -// plan, so the state keeps the record of the resource and the command can be run again. -func findByInternalID(ctx context.Context, adapter *dresources.Adapter, savedState any) (string, any, error) { - if !adapter.HasFindByInternalID() { - return "", nil, nil - } - savedInternalID, err := adapter.InternalID(savedState) - if err != nil || savedInternalID == "" { - return "", nil, err - } - id, err := retry.With(ctx, retry.IsTransientRead, func() (string, error) { - return adapter.DoFindByInternalID(ctx, savedState) - }) - if err != nil || id == "" { - return "", nil, ignoreMissingOrForbidden(ctx, err, fmt.Sprintf("searching %sfor %s %q, as a read at its recorded ID did not return it", - searchedParent(adapter, savedState), adapter.ResourceConfig().InternalIDField, savedInternalID)) - } - remote, err := retry.With(ctx, retry.IsTransientRead, func() (any, error) { - return adapter.DoRead(ctx, id) - }) - if err != nil { - return "", nil, ignoreMissingOrForbidden(ctx, err, fmt.Sprintf("reading id=%q, where %s %q was found", id, adapter.ResourceConfig().InternalIDField, savedInternalID)) - } - if ok, err := hasInternalIDOf(adapter, remote, savedInternalID); err != nil || !ok { + foundInternalID, err := adapter.InternalID(found) + if err != nil || foundInternalID != savedInternalID { return "", nil, err } return id, remote, nil } -// searchedParent names, followed by a space, the parent DoFindByInternalID lists: the recorded full -// name without its last part (the catalog of a schema, the schema of a volume). Empty if unknown. -func searchedParent(adapter *dresources.Adapter, savedState any) string { - if !adapter.HasIDFromState() { - return "" - } - recorded, err := adapter.IDFromState(savedState) - if i := strings.LastIndex(recorded, "."); err == nil && i > 0 { - return fmt.Sprintf("%q ", recorded[:i]) - } - return "" -} - -// ignoreMissingOrForbidden returns err unless it is nil, a missing resource or a permission error -// (logged as a warning). -func ignoreMissingOrForbidden(ctx context.Context, err error, what string) error { - if err == nil || apierr.IsMissing(err) { - return nil - } - if errors.Is(err, apierr.ErrPermissionDenied) { - logdiag.LogDiag(ctx, diag.Warningf("%s: %s", what, err)[0]) - return nil - } - return fmt.Errorf("%s: %w", what, err) -} - // missingInternalID reports whether savedState lacks the internal ID that remote, the remote state // remapped to the state type, has. func missingInternalID(adapter *dresources.Adapter, savedState, remote any) (bool, error) { diff --git a/bundle/direct/bundle_plan_test.go b/bundle/direct/bundle_plan_test.go index 27991b0a4aa..02fbf8ffe48 100644 --- a/bundle/direct/bundle_plan_test.go +++ b/bundle/direct/bundle_plan_test.go @@ -14,7 +14,6 @@ import ( "github.com/databricks/cli/libs/structs/structdiff" "github.com/databricks/cli/libs/structs/structpath" "github.com/databricks/cli/libs/structs/structvar" - "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/databricks/databricks-sdk-go/service/compute" "github.com/databricks/databricks-sdk-go/service/jobs" @@ -1022,72 +1021,34 @@ func TestOtherInternalID(t *testing.T) { assert.Empty(t, got) } -func TestFindDeleteTargetKeepsOwnResource(t *testing.T) { +func TestOtherObjectAtRecordedID(t *testing.T) { const key = "resources.schemas.schema1" adapters, err := dresources.InitAll(nil) require.NoError(t, err) remote := &catalog.SchemaInfo{FullName: "main.s1", SchemaId: "uuid-1"} - for name, state := range map[string][]byte{ + for name, tc := range map[string]struct { + state []byte + other bool + }{ // The deployment metadata service can hold an entry with only an ID. - "id-only entry": nil, - "same internal id": []byte(`{"catalog_name": "main", "name": "s1", "schema_id": "uuid-1"}`), - "internal id unset": []byte(`{"catalog_name": "main", "name": "s1"}`), + "id-only entry": {state: nil}, + "same internal id": {state: []byte(`{"catalog_name": "main", "name": "s1", "schema_id": "uuid-1"}`)}, + "internal id unset": {state: []byte(`{"catalog_name": "main", "name": "s1"}`)}, + "another internal id": {state: []byte(`{"catalog_name": "main", "name": "s1", "schema_id": "uuid-0"}`), other: true}, } { t.Run(name, func(t *testing.T) { b := &DeploymentBundle{Adapters: adapters} b.StateDB.OpenWithData("state.json", dstate.Database{State: map[string]dstate.ResourceEntry{ - key: {ID: "main.s1", State: state}, + key: {ID: "main.s1", State: tc.state}, }}) - id, got, err := b.findDeleteTarget(t.Context(), key, adapters["schemas"], "main.s1", remote, nil) + message, err := b.otherObjectAtRecordedID(key, adapters["schemas"], "main.s1", remote) require.NoError(t, err) - assert.Empty(t, id) - assert.Same(t, remote, got) - }) - } -} - -func TestFindDeleteTargetUnverifiedRead(t *testing.T) { - const key = "resources.schemas.schema1" - adapters, err := dresources.InitAll(nil) - require.NoError(t, err) - b := &DeploymentBundle{Adapters: adapters} - b.StateDB.OpenWithData("state.json", dstate.Database{State: map[string]dstate.ResourceEntry{ - key: {ID: "main.s1", State: []byte(`{"catalog_name": "main", "name": "s1", "schema_id": "uuid-1"}`)}, - }}) - - // A read that failed for another reason than a missing resource cannot show that what is at the - // recorded ID is ours: the plan fails instead of deleting it. - _, _, err = b.findDeleteTarget(t.Context(), key, adapters["schemas"], "main.s1", nil, apierr.ErrInternalError) - assert.ErrorIs(t, err, apierr.ErrInternalError) - - // A permission error searches by internal ID first: schemas/destroy_forbidden_at_recorded_name. -} - -func TestUnderMovedParent(t *testing.T) { - catalog := &movedParent{key: "resources.catalogs.c", recordedID: "main", id: "archive"} - schema := &movedParent{key: "resources.schemas.s", recordedID: "main.s", id: "main.t", deleted: true} - sameAsCatalog := &movedParent{key: "resources.schemas.main", recordedID: "main.main", id: "main.moved", deleted: true} - - for name, tt := range map[string]struct { - parents []*movedParent - id, wantID string - want *movedParent - }{ - "moved": {parents: []*movedParent{schema}, id: "main.s.v", wantID: "main.t.v", want: schema}, - "schema named like its catalog": {parents: []*movedParent{sameAsCatalog}, id: "main.main.v", wantID: "main.moved.v", want: sameAsCatalog}, - // The bundle records a child in config case and its renamed parent in UC's lower case. - "recorded in another case": {parents: []*movedParent{schema}, id: "main.S.v", wantID: "main.t.v", want: schema}, - "longest prefix": {parents: []*movedParent{catalog, schema}, id: "main.s.v", wantID: "main.t.v", want: schema}, - "only the catalog moved": {parents: []*movedParent{catalog}, id: "main.s.v", wantID: "archive.s.v", want: catalog}, - "not under the parent": {parents: []*movedParent{schema}, id: "main.sx.v", wantID: "", want: nil}, - "the parent itself": {parents: []*movedParent{schema}, id: "main.s", wantID: "", want: nil}, - "no parents": {parents: nil, id: "main.s.v", wantID: "", want: nil}, - } { - t.Run(name, func(t *testing.T) { - got, gotID := underMovedParent(tt.parents, tt.id) - assert.Same(t, tt.want, got) - assert.Equal(t, tt.wantID, gotID) + if tc.other { + assert.Contains(t, message, `its schema_id is "uuid-1", not "uuid-0" as recorded`) + } else { + assert.Empty(t, message) + } }) } } diff --git a/bundle/direct/dresources/README.md b/bundle/direct/dresources/README.md index f82b7fce402..06388f27b47 100644 --- a/bundle/direct/dresources/README.md +++ b/bundle/direct/dresources/README.md @@ -62,7 +62,7 @@ If the ID is built from the resource's own fields (a UC full name), implement `I If the backend also has its own ID that a rename keeps (a volume's `volume_id`), add it to the state type, copy it in `RemapState`, and name it in the config as `internal_id_field`. The framework records it on every save and skips its changes in the plan. When nothing is at the recorded ID, the plan reads at the ID the config gives the resource and takes what it finds only if the internal ID matches. This covers a rename done out of band, or by a deploy that stopped before re-keying the resource. -An object at the recorded ID with a different internal ID is someone else's (ours was renamed away and another created under its name): the plan treats ours as not there, so it never updates or deletes the other object. A resource to be deleted (by destroy, or removed from config) has no config to give its new name, so to delete one renamed out of band the resource needs `DoFindByInternalID`: it searches the parent (lists the schemas in the catalog, the volumes in the schema) for the recorded internal ID. Without it, the delete only drops the state entry of a resource not found at its recorded ID. A child whose parent the plan found at a new ID (a volume in a renamed schema, matched by full-name prefix; only a resource that lives in its UC parent: it has `parent_id_fields`, or `moves_with_parent` for a feature) is looked for under the parent's new ID and deleted there as itself if its internal ID matches. If it cannot be verified there and the parent is deleted, no delete is sent for it, but the plan lists it as going with the parent (`with_parent`), so approval and its `prevent_destroy` still apply, and `--select` of it alone fails: the parent has to be selected too. A parent outside the bundle that was renamed is not followed. The search is a listing: one list of the schemas in the catalog (or the volumes in the schema) per resource not found at its recorded ID, so a destroy in a catalog with many schemas pages through all of them. +An object at the recorded ID with a different internal ID is someone else's (ours was renamed away and another created under its name, or ours was deleted and created again): the plan treats ours as not there, so it never updates or deletes the other object. A deploy then looks for ours at the ID the config gives it, as above. A delete (by destroy, or of a resource removed from config) has no config to give a new name, so it only drops the state entry and leaves the other object in place. ## Empty states: IsEmptyState diff --git a/bundle/direct/dresources/adapter.go b/bundle/direct/dresources/adapter.go index c95c31662df..538cfcdd8e3 100644 --- a/bundle/direct/dresources/adapter.go +++ b/bundle/direct/dresources/adapter.go @@ -122,19 +122,9 @@ type IResource interface { // [Optional] IDFromState returns the ID the state gives the resource, for resource types whose ID // is built from their own fields (a UC full name). With it, an update_id that only follows a - // parent's rename is applied without calling DoUpdateWithID (see PlanEntry.StateOnly). The ID - // must be a UC full name: dot-joined, parent first, compared case-insensitively; the plan finds - // the resources in a renamed parent by it (see findInMovedParents). + // parent's rename is applied without calling DoUpdateWithID (see PlanEntry.StateOnly). // Example: func (*ResourceVolume) IDFromState(state *VolumeState) string IDFromState(state any) string - - // [Optional] DoFindByInternalID returns the ID of the resource whose internal ID (see - // internal_id_field) is the one in state, or "" if none is found. It looks where the resource - // can be without its name: a resource to be deleted (by destroy, or removed from config) has - // no config to give its new name after a rename out of band. The engine reads what it returns - // and checks the internal ID again. - // Example: func (r *ResourceSchema) DoFindByInternalID(ctx context.Context, state *SchemaState) (string, error) - DoFindByInternalID(ctx context.Context, state any) (string, error) } // Adapter wraps resource implementation, validates signatures and type consistency across methods @@ -159,7 +149,6 @@ type Adapter struct { doResize *calladapt.BoundCaller isGone *calladapt.BoundCaller idFromState *calladapt.BoundCaller - findByInternalID *calladapt.BoundCaller resourceConfig *ResourceLifecycleConfig generatedResourceConfig *ResourceLifecycleConfig @@ -205,7 +194,6 @@ func NewAdapter(typedNil any, resourceType string, client *databricks.WorkspaceC overrideChangeDesc: nil, isGone: nil, idFromState: nil, - findByInternalID: nil, internalIDPath: nil, resourceConfig: GetResourceConfig(resourceType), generatedResourceConfig: GetGeneratedResourceConfig(resourceType), @@ -338,11 +326,6 @@ func (a *Adapter) initMethods(resource any) error { return err } - a.findByInternalID, err = calladapt.PrepareCall(resource, reflect.TypeFor[IResource](), "DoFindByInternalID") - if err != nil { - return err - } - keyedSlicesCall, err := calladapt.PrepareCall(resource, reflect.TypeFor[IResource](), "KeyedSlices") if err != nil { return err @@ -455,13 +438,6 @@ func (a *Adapter) validate() error { a.internalIDPath = path } - if a.findByInternalID != nil { - if a.internalIDPath == nil { - return errors.New("DoFindByInternalID requires internal_id_field") - } - validations = append(validations, "DoFindByInternalID state", a.findByInternalID.InTypes[1], stateType) - } - if a.resourceConfig != nil && len(a.resourceConfig.ParentIDFields) > 0 && a.idFromState == nil { return errors.New("parent_id_fields requires IDFromState") } @@ -826,21 +802,6 @@ func (a *Adapter) InternalID(state any) (string, error) { return id, nil } -// HasFindByInternalID reports whether the resource implements DoFindByInternalID. -func (a *Adapter) HasFindByInternalID() bool { - return a.findByInternalID != nil -} - -// DoFindByInternalID returns the ID of the resource with the internal ID recorded in state, "" if -// none is found; see IResource.DoFindByInternalID. -func (a *Adapter) DoFindByInternalID(ctx context.Context, state any) (string, error) { - outs, err := a.findByInternalID.Call(ctx, state) - if err != nil { - return "", err - } - return outs[0].(string), nil -} - // RecordInternalID copies the internal ID from remoteState into state, which is about to be saved. // Config never sets it, so without this the saved state would lose it. func (a *Adapter) RecordInternalID(state, remoteState any) error { diff --git a/bundle/direct/dresources/config.go b/bundle/direct/dresources/config.go index e1ff6370f03..1e55636c57f 100644 --- a/bundle/direct/dresources/config.go +++ b/bundle/direct/dresources/config.go @@ -145,12 +145,6 @@ type ResourceLifecycleConfig struct { // resource's own and never follows it. Requires IDFromState. ParentIDFields []FieldRule `yaml:"parent_id_fields,omitempty"` - // MovesWithParent: the resource lives in a UC schema and moves with it on a rename, but its - // parent is only part of its full-name ID, so it has no ParentIDFields (a feature). A Delete of - // it is looked for under a parent renamed out of band, as for a resource with ParentIDFields - // (see findInMovedParents). - MovesWithParent bool `yaml:"moves_with_parent,omitempty"` - // NormalizeSlash: string field patterns the UC API strips trailing slashes from. // A change is skipped when local and remote differ only by trailing slashes. NormalizeSlash []FieldRule `yaml:"normalize_slash,omitempty"` @@ -226,7 +220,6 @@ var empty = ResourceLifecycleConfig{ ProvidedIDFields: nil, UpdatableIDFields: nil, ParentIDFields: nil, - MovesWithParent: false, NormalizeSlash: nil, IgnoreRemoteAdditions: nil, BackendDefaults: nil, diff --git a/bundle/direct/dresources/configs/feature_specs.yml b/bundle/direct/dresources/configs/feature_specs.yml index 8f2af9e7679..48edeb6e434 100644 --- a/bundle/direct/dresources/configs/feature_specs.yml +++ b/bundle/direct/dresources/configs/feature_specs.yml @@ -2,5 +2,3 @@ provided_id_fields: # UC lowercases the name; remote returns "main.ml.myspec" for config "main.ml.MySpec". - field: name reason: id_field -# Inside its UC schema: a rename of the schema moves it, and its ID with it. -moves_with_parent: true diff --git a/bundle/direct/dresources/configs/features.yml b/bundle/direct/dresources/configs/features.yml index 877d3cc883b..e8c72ce50d1 100644 --- a/bundle/direct/dresources/configs/features.yml +++ b/bundle/direct/dresources/configs/features.yml @@ -6,5 +6,3 @@ stable_output_fields: # name is the last component of full_name, which is immutable, so it cannot change on an # in-place update; see the stable_output_fields doc in config.go and ES-2202624. - field: name -# Inside its UC schema: a rename of the schema moves it, and its ID with it. -moves_with_parent: true diff --git a/bundle/direct/dresources/schema.go b/bundle/direct/dresources/schema.go index ce3b9b6a6dd..dc8ea85d305 100644 --- a/bundle/direct/dresources/schema.go +++ b/bundle/direct/dresources/schema.go @@ -164,27 +164,6 @@ func (*ResourceSchema) IDFromState(state *SchemaState) string { return state.CatalogName + "." + state.Name } -// DoFindByInternalID lists the schemas in the catalog for the one with the recorded schema_id. -func (r *ResourceSchema) DoFindByInternalID(ctx context.Context, state *SchemaState) (string, error) { - iter := r.client.Schemas.List(ctx, catalog.ListSchemasRequest{ - CatalogName: state.CatalogName, - IncludeBrowse: false, - MaxResults: 0, // The server's page size; the iterator follows the pages. - PageToken: "", - ForceSendFields: nil, - }) - for iter.HasNext(ctx) { - schema, err := iter.Next(ctx) - if err != nil { - return "", err - } - if schema.SchemaId == state.SchemaId { - return schema.FullName, nil - } - } - return "", nil -} - func (r *ResourceSchema) DoDelete(ctx context.Context, id string, _ *SchemaState) error { return r.client.Schemas.Delete(ctx, catalog.DeleteSchemaRequest{ FullName: id, diff --git a/bundle/direct/dresources/volume.go b/bundle/direct/dresources/volume.go index 5955de8c8aa..96d49e268af 100644 --- a/bundle/direct/dresources/volume.go +++ b/bundle/direct/dresources/volume.go @@ -165,30 +165,6 @@ func (*ResourceVolume) IDFromState(state *VolumeState) string { return state.CatalogName + "." + state.SchemaName + "." + state.Name } -// DoFindByInternalID lists the volumes in the schema for the one with the recorded volume_id. A -// volume whose schema was renamed is not found here; when the schema is in the bundle, the plan -// looks for it under the schema's new name (findInMovedParents). -func (r *ResourceVolume) DoFindByInternalID(ctx context.Context, state *VolumeState) (string, error) { - iter := r.client.Volumes.List(ctx, catalog.ListVolumesRequest{ - CatalogName: state.CatalogName, - SchemaName: state.SchemaName, - IncludeBrowse: false, - MaxResults: 0, // The server's page size; the iterator follows the pages. - PageToken: "", - ForceSendFields: nil, - }) - for iter.HasNext(ctx) { - volume, err := iter.Next(ctx) - if err != nil { - return "", err - } - if volume.VolumeId == state.VolumeId { - return volume.FullName, nil - } - } - return "", nil -} - func (r *ResourceVolume) DoDelete(ctx context.Context, id string, _ *VolumeState) error { return r.client.Volumes.DeleteByName(ctx, id) } diff --git a/bundle/direct/graph.go b/bundle/direct/graph.go index d2c13d02344..386c590f53c 100644 --- a/bundle/direct/graph.go +++ b/bundle/direct/graph.go @@ -1,7 +1,6 @@ package direct import ( - "cmp" "fmt" "maps" "slices" @@ -18,9 +17,6 @@ func makeGraph(plan *deployplan.Plan) (*dagrun.Graph, error) { g.AddNode(resourceKey) } - // The dependencies of a WithParent entry, which its parent's delete removes. - var moved []movedEdge - // Add edges based on depends_on field. // For deletions, reverse direction so children are deleted before parents. for resourceKey, entry := range plan.Plan { @@ -30,31 +26,10 @@ func makeGraph(plan *deployplan.Plan) (*dagrun.Graph, error) { isDelete := entry.Action == deployplan.Delete - if isDelete && entry.WithParent != "" { - // No delete is sent for it, so nothing has to wait for it: its state is dropped only - // once its parent's delete, which removes it, has succeeded. Its other dependencies must - // outlive that delete instead (see below). - if parent := slices.IndexFunc(entry.DependsOn, func(d deployplan.DependsOnEntry) bool { return d.Node == entry.WithParent }); parent >= 0 && plan.Plan[entry.WithParent] != nil { - g.AddDirectedEdge(entry.WithParent, resourceKey, entry.DependsOn[parent].Label) - for _, dep := range entry.DependsOn { - if dep.Node != entry.WithParent && plan.Plan[dep.Node] != nil { - moved = append(moved, movedEdge{from: entry.WithParent, to: dep.Node, label: dep.Label}) - } - } - } - continue - } - for _, dep := range entry.DependsOn { if _, exists := plan.Plan[dep.Node]; exists { if isDelete { g.AddDirectedEdge(resourceKey, dep.Node, dep.Label) - // The delete that removes a WithParent dependency is its parent's, so that one waits - // too, and does not run if this one fails, as for a dependency deleted on its own. - if parent := plan.Plan[dep.Node].WithParent; parent != "" && parent != resourceKey && plan.Plan[parent] != nil && - !slices.ContainsFunc(entry.DependsOn, func(d deployplan.DependsOnEntry) bool { return d.Node == parent }) { - g.AddDirectedEdge(resourceKey, parent, dep.Label) - } } else { g.AddDirectedEdge(dep.Node, resourceKey, dep.Label) } @@ -64,40 +39,5 @@ func makeGraph(plan *deployplan.Plan) (*dagrun.Graph, error) { } } - // The parent's delete waits for what its WithParent child depends on (an external location a - // volume's storage_location names), as the child's own delete would. A dependency that is - // itself deleted before the parent (a sibling in it) keeps its order: the edge would close a - // cycle through the parent. - slices.SortFunc(moved, func(x, y movedEdge) int { return cmp.Or(cmp.Compare(x.from, y.from), cmp.Compare(x.to, y.to)) }) - for _, e := range moved { - if !reaches(g, e.to, e.from) { - g.AddDirectedEdge(e.from, e.to, e.label) - } - } - return g, nil } - -type movedEdge struct { - from, to, label string -} - -// reaches reports whether there is a path from one node to another in g. -func reaches(g *dagrun.Graph, from, to string) bool { - seen := map[string]bool{from: true} - stack := []string{from} - for len(stack) > 0 { - n := stack[len(stack)-1] - stack = stack[:len(stack)-1] - if n == to { - return true - } - for _, e := range g.Adj[n] { - if !seen[e.To] { - seen[e.To] = true - stack = append(stack, e.To) - } - } - } - return false -} diff --git a/bundle/direct/graph_test.go b/bundle/direct/graph_test.go deleted file mode 100644 index 500c22f2438..00000000000 --- a/bundle/direct/graph_test.go +++ /dev/null @@ -1,163 +0,0 @@ -package direct - -import ( - "sync" - "testing" - - "github.com/databricks/cli/bundle/deployplan" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func TestMakeGraphDeletesWithParentAfterItsParent(t *testing.T) { - const ( - schemaKey = "resources.schemas.schema1" - volumeKey = "resources.volumes.volume1" - modelKey = "resources.registered_models.model1" - ) - schemaDep := deployplan.DependsOnEntry{Node: schemaKey, Label: "${resources.schemas.schema1.name}"} - plan := deployplan.NewPlanDirect() - plan.Plan[schemaKey] = &deployplan.PlanEntry{Action: deployplan.Delete, ID: "main.moved"} - // Verified under the moved schema: deleted as itself, before the schema. - plan.Plan[volumeKey] = &deployplan.PlanEntry{Action: deployplan.Delete, ID: "main.moved.v", DependsOn: []deployplan.DependsOnEntry{schemaDep}} - // Goes with the schema's delete: its state is dropped only once that delete succeeded. - plan.Plan[modelKey] = &deployplan.PlanEntry{Action: deployplan.Delete, Gone: true, WithParent: schemaKey, DependsOn: []deployplan.DependsOnEntry{schemaDep}} - - for _, schemaFails := range []bool{false, true} { - g, err := makeGraph(plan) - require.NoError(t, err) - require.NoError(t, g.DetectCycle()) - - var mu sync.Mutex - var order []string - failed := map[string]bool{} - g.Run(1, func(key string, failedDependency *string) bool { - mu.Lock() - defer mu.Unlock() - if failedDependency != nil { - failed[key] = true - return false - } - order = append(order, key) - return !schemaFails || key != schemaKey - }) - - if schemaFails { - assert.Equal(t, []string{volumeKey, schemaKey}, order) - assert.Equal(t, map[string]bool{modelKey: true}, failed) - } else { - assert.Equal(t, []string{volumeKey, schemaKey, modelKey}, order) - } - } -} - -func TestMakeGraphWithParentReferencingASibling(t *testing.T) { - const ( - schemaKey = "resources.schemas.schema1" - volume1Key = "resources.volumes.volume1" - volume2Key = "resources.volumes.volume2" - ) - plan := deployplan.NewPlanDirect() - plan.Plan[schemaKey] = &deployplan.PlanEntry{Action: deployplan.Delete, ID: "main.moved"} - plan.Plan[volume1Key] = &deployplan.PlanEntry{Action: deployplan.Delete, ID: "main.moved.v1", DependsOn: []deployplan.DependsOnEntry{{Node: schemaKey}}} - // volume2 names its schema through volume1: deleted before volume1 as usual, it would close a - // cycle through the schema it goes with. - plan.Plan[volume2Key] = &deployplan.PlanEntry{ - Action: deployplan.Delete, Gone: true, WithParent: schemaKey, - DependsOn: []deployplan.DependsOnEntry{{Node: volume1Key}, {Node: schemaKey}}, - } - - g, err := makeGraph(plan) - require.NoError(t, err) - require.NoError(t, g.DetectCycle()) - - var order []string - g.Run(1, func(key string, failedDependency *string) bool { - order = append(order, key) - return true - }) - assert.Equal(t, []string{volume1Key, schemaKey, volume2Key}, order) -} - -func TestMakeGraphDependentOfWithParentWaitsForItsParent(t *testing.T) { - const ( - schemaKey = "resources.schemas.schema1" - volumeKey = "resources.volumes.volume1" - jobKey = "resources.jobs.job1" - ) - plan := deployplan.NewPlanDirect() - plan.Plan[schemaKey] = &deployplan.PlanEntry{Action: deployplan.Delete, ID: "main.moved"} - plan.Plan[volumeKey] = &deployplan.PlanEntry{Action: deployplan.Delete, Gone: true, WithParent: schemaKey, DependsOn: []deployplan.DependsOnEntry{{Node: schemaKey}}} - // The job names only the volume, whose removal is the schema's delete. - plan.Plan[jobKey] = &deployplan.PlanEntry{Action: deployplan.Delete, DependsOn: []deployplan.DependsOnEntry{{Node: volumeKey}}} - - for _, jobFails := range []bool{false, true} { - g, err := makeGraph(plan) - require.NoError(t, err) - require.NoError(t, g.DetectCycle()) - - var mu sync.Mutex - var order []string - failed := map[string]bool{} - g.Run(1, func(key string, failedDependency *string) bool { - mu.Lock() - defer mu.Unlock() - if failedDependency != nil { - failed[key] = true - return false - } - order = append(order, key) - return !jobFails || key != jobKey - }) - - if jobFails { - assert.Equal(t, []string{jobKey}, order) - assert.Equal(t, map[string]bool{schemaKey: true, volumeKey: true}, failed) - } else { - assert.Equal(t, []string{jobKey, schemaKey, volumeKey}, order) - } - } -} - -func TestMakeGraphParentOfWithParentWaitsForItsDependencies(t *testing.T) { - const ( - locationKey = "resources.external_locations.loc" - schemaKey = "resources.schemas.schema1" - volumeKey = "resources.volumes.volume1" - ) - plan := deployplan.NewPlanDirect() - plan.Plan[locationKey] = &deployplan.PlanEntry{Action: deployplan.Delete} - plan.Plan[schemaKey] = &deployplan.PlanEntry{Action: deployplan.Delete, ID: "main.moved"} - // An external volume whose storage_location names the location: the schema's delete removes it. - plan.Plan[volumeKey] = &deployplan.PlanEntry{ - Action: deployplan.Delete, Gone: true, WithParent: schemaKey, - DependsOn: []deployplan.DependsOnEntry{{Node: schemaKey}, {Node: locationKey}}, - } - - for _, schemaFails := range []bool{false, true} { - g, err := makeGraph(plan) - require.NoError(t, err) - require.NoError(t, g.DetectCycle()) - - var mu sync.Mutex - var order []string - failed := map[string]bool{} - g.Run(1, func(key string, failedDependency *string) bool { - mu.Lock() - defer mu.Unlock() - if failedDependency != nil { - failed[key] = true - return false - } - order = append(order, key) - return !schemaFails || key != schemaKey - }) - - if schemaFails { - assert.Equal(t, []string{schemaKey}, order) - assert.Equal(t, map[string]bool{locationKey: true, volumeKey: true}, failed) - } else { - assert.Equal(t, []string{schemaKey, volumeKey, locationKey}, order) - } - } -} diff --git a/bundle/direct/pkg.go b/bundle/direct/pkg.go index f9b00cc730c..84990d0883e 100644 --- a/bundle/direct/pkg.go +++ b/bundle/direct/pkg.go @@ -55,10 +55,6 @@ type DeploymentBundle struct { // fields references, recorded before resolveReferences drops them (see addParentMoves). refTypes sync.Map - // otherObjects maps the key of a resource to be deleted to the message that another object holds - // its recorded ID, told once the plan is complete (see warnOtherObjects). - otherObjects sync.Map - // DeployDurations maps a resource key to the time.Duration its create, update or // recreate took. Only successfully deployed resources are recorded; Apply runs // nodes in parallel, hence the sync.Map. diff --git a/libs/testserver/handlers.go b/libs/testserver/handlers.go index 73270a1078e..46e762c0522 100644 --- a/libs/testserver/handlers.go +++ b/libs/testserver/handlers.go @@ -854,10 +854,6 @@ func AddDefaultHandlers(server *Server) { // Schemas: - server.Handle("GET", "/api/2.1/unity-catalog/schemas", func(req Request) any { - return req.Workspace.SchemasList(req) - }) - server.Handle("GET", "/api/2.1/unity-catalog/schemas/{full_name}", func(req Request) any { return MapGetUC(req.Workspace, req.Workspace.Schemas, req.Vars["full_name"], "Schema") }) @@ -1082,10 +1078,6 @@ func AddDefaultHandlers(server *Server) { // Volumes: looked up case-insensitively, see volumeKey. - server.Handle("GET", "/api/2.1/unity-catalog/volumes", func(req Request) any { - return req.Workspace.VolumesList(req) - }) - server.Handle("GET", "/api/2.1/unity-catalog/volumes/{full_name}", func(req Request) any { return MapGetUC(req.Workspace, req.Workspace.Volumes, volumeKey(req.Vars["full_name"]), "Volume") }) diff --git a/libs/testserver/schemas.go b/libs/testserver/schemas.go index d143e9620e1..c934adcdc15 100644 --- a/libs/testserver/schemas.go +++ b/libs/testserver/schemas.go @@ -3,9 +3,7 @@ package testserver import ( "encoding/json" "fmt" - "maps" "net/http" - "slices" "strings" "github.com/databricks/databricks-sdk-go/service/catalog" @@ -186,17 +184,3 @@ func (s *FakeWorkspace) SchemasDelete(req Request, fullName string) Response { } return Response{} } - -// SchemasList returns the schemas in a catalog, in one page. -func (s *FakeWorkspace) SchemasList(req Request) Response { - defer s.LockUnlock()() - - catalogName := req.URL.Query().Get("catalog_name") - result := catalog.ListSchemasResponse{Schemas: []catalog.SchemaInfo{}} - for _, key := range slices.Sorted(maps.Keys(s.Schemas)) { - if schema := s.Schemas[key]; schema.CatalogName == catalogName { - result.Schemas = append(result.Schemas, schema) - } - } - return Response{Body: result} -} diff --git a/libs/testserver/volumes.go b/libs/testserver/volumes.go index 7e860fe4d49..2a23d4471e4 100644 --- a/libs/testserver/volumes.go +++ b/libs/testserver/volumes.go @@ -3,9 +3,7 @@ package testserver import ( "encoding/json" "fmt" - "maps" "net/http" - "slices" "strings" "github.com/databricks/databricks-sdk-go/service/catalog" @@ -115,26 +113,3 @@ func (s *FakeWorkspace) VolumesUpdate(req Request, fullname string) Response { Body: existing, } } - -// VolumesList returns the volumes in a schema, in one page. Like UC, it fails for a schema that -// does not exist. -func (s *FakeWorkspace) VolumesList(req Request) Response { - defer s.LockUnlock()() - - catalogName := req.URL.Query().Get("catalog_name") - // Stored lowercased, see VolumesCreate. - schemaName := strings.ToLower(req.URL.Query().Get("schema_name")) - if _, ok := s.Schemas[catalogName+"."+schemaName]; !ok { - return Response{ - StatusCode: http.StatusNotFound, - Body: map[string]string{"error_code": "SCHEMA_DOES_NOT_EXIST", "message": fmt.Sprintf("Schema '%s.%s' does not exist.", catalogName, schemaName)}, - } - } - result := catalog.ListVolumesResponseContent{Volumes: []catalog.VolumeInfo{}} - for _, key := range slices.Sorted(maps.Keys(s.Volumes)) { - if volume := s.Volumes[key]; volume.CatalogName == catalogName && volume.SchemaName == schemaName { - result.Volumes = append(result.Volumes, volume) - } - } - return Response{Body: result} -} From c27c39cba1d5946dbf21d4c8b8b4a13e7d668c3c Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Fri, 27 Nov 2026 22:47:58 +0000 Subject: [PATCH 17/22] direct: separate advice for deploy and delete when another object is at the recorded ID - Deploy suggests pointing the config at the renamed object, or unbind and bind; delete says no delete is sent and suggests deleting it outside the bundle. - The Gone comments mention another object at the recorded ID. - foreign_at_recorded_name and recreated_at_recorded_name show the failed deploy's requests; the plan step uses plan_filter.py. aws: TestAccept/bundle/resources/schemas/foreign_at_recorded_name TestAccept/bundle/resources/schemas/destroy_after_rename_out_of_band TestAccept/bundle/resources/schemas/recreated_at_recorded_name TestAccept/bundle/resources/volumes/foreign_at_recorded_name Co-authored-by: Isaac --- .../foreign_at_recorded_name/output.txt | 51 ++++++++++++------- .../schemas/foreign_at_recorded_name/script | 8 +-- .../recreated_at_recorded_name/output.txt | 17 +++++-- .../schemas/recreated_at_recorded_name/script | 2 +- .../foreign_at_recorded_name/output.txt | 4 +- bundle/deployplan/action.go | 3 +- bundle/deployplan/plan.go | 5 +- bundle/direct/bundle_plan.go | 31 ++++++----- 8 files changed, 76 insertions(+), 45 deletions(-) diff --git a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt index e1cf6f7f397..1955bbd3710 100644 --- a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt +++ b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt @@ -18,23 +18,29 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged === The other schema has another schema_id, so the plan does not take it for the bundle's: it plans a create >>> [CLI] bundle plan --output json -Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it in place. If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle, or manage it again with 'bundle deployment bind' while it is in the config ('bundle deployment unbind' first if the state still has it). +Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle does not take it for its own. If the bundle's was renamed outside the bundle, set its name in the config to the new one. If this object is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and then 'bundle deployment bind' to manage it again. { - "resources.schemas.schema1": { - "action": "create", - "new_state": { - "value": { - "catalog_name": "main", - "comment": "c1", - "name": "test-schema-[UNIQUE_NAME]" - } - }, - "changes": { - "schema_id": { - "action": "skip", - "reason": "internal_id", - "old": "[SCHEMA_ID]" + "plan_version": [PLAN_VERSION], + "cli_version": "[CLI_VERSION]", + "lineage": "[UUID]", + "serial": 1, + "plan": { + "resources.schemas.schema1": { + "action": "create", + "new_state": { + "value": { + "catalog_name": "main", + "comment": "c1", + "name": "test-schema-[UNIQUE_NAME]" + } + }, + "changes": { + "schema_id": { + "action": "skip", + "reason": "internal_id", + "old": "[SCHEMA_ID]" + } } } } @@ -44,7 +50,7 @@ Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds an >>> musterr [CLI] bundle deploy Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... -Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it in place. If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle, or manage it again with 'bundle deployment bind' while it is in the config ('bundle deployment unbind' first if the state still has it). +Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle does not take it for its own. If the bundle's was renamed outside the bundle, set its name in the config to the new one. If this object is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and then 'bundle deployment bind' to manage it again. Error: cannot create resources.schemas.schema1: Schema 'test-schema-[UNIQUE_NAME]' already exists (400 SCHEMA_ALREADY_EXISTS) @@ -55,10 +61,21 @@ API message: Schema 'test-schema-[UNIQUE_NAME]' already exists Files: 2 uploaded, 0 deleted +>>> print_requests.py //unity-catalog +{ + "method": "POST", + "path": "/api/2.1/unity-catalog/schemas", + "body": { + "catalog_name": "main", + "comment": "c1", + "name": "test-schema-[UNIQUE_NAME]" + } +} + === Destroy does not delete the other schema: it only drops the bundle's from the state >>> [CLI] bundle destroy --auto-approve -Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it in place, and drops resources.schemas.schema1 from the deployment state. If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle, or manage it again with 'bundle deployment bind' while it is in the config ('bundle deployment unbind' first if the state still has it). +Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so no delete is sent for it, and resources.schemas.schema1 is dropped from the deployment state. If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle. All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default diff --git a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script index 3dd19c5f2e4..6f4ae3fa5d1 100644 --- a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script +++ b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script @@ -13,10 +13,6 @@ cleanup() { } trap cleanup EXIT -# Drop the remote_state fields that differ between clouds or from the test server. -plan() { - trace $CLI bundle plan --output json | jq .plan | normalize_uc_payload.py created_at updated_at updated_by metastore_id effective_predictive_optimization_flag storage_location -} title "Deploy a schema" trace $CLI bundle deploy @@ -29,11 +25,11 @@ add_repl "$($CLI schemas create "test-schema-${UNIQUE_NAME}" main | jq -r .schem print_requests.py //unity-catalog &> LOG.oob_requests title "The other schema has another schema_id, so the plan does not take it for the bundle's: it plans a create" -plan +trace $CLI bundle plan --output json | plan_filter.py title "Deploy leaves the other schema alone: the create fails on its name" trace musterr $CLI bundle deploy -print_requests.py //unity-catalog &> LOG.failed_deploy_requests +trace print_requests.py //unity-catalog title "Destroy does not delete the other schema: it only drops the bundle's from the state" trace $CLI bundle destroy --auto-approve diff --git a/acceptance/bundle/resources/schemas/recreated_at_recorded_name/output.txt b/acceptance/bundle/resources/schemas/recreated_at_recorded_name/output.txt index 139e1a4fcd2..23082ee9bc2 100644 --- a/acceptance/bundle/resources/schemas/recreated_at_recorded_name/output.txt +++ b/acceptance/bundle/resources/schemas/recreated_at_recorded_name/output.txt @@ -14,7 +14,7 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged === The new schema has another schema_id: the bundle does not take it for its own, and deploy fails on its name >>> [CLI] bundle plan -Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[NEW_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it in place. If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle, or manage it again with 'bundle deployment bind' while it is in the config ('bundle deployment unbind' first if the state still has it). +Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[NEW_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle does not take it for its own. If the bundle's was renamed outside the bundle, set its name in the config to the new one. If this object is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and then 'bundle deployment bind' to manage it again. create schemas.schema1 @@ -22,7 +22,7 @@ Plan: 1 to add, 0 to change, 0 to delete, 0 unchanged >>> musterr [CLI] bundle deploy Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... -Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[NEW_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle leaves it in place. If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle, or manage it again with 'bundle deployment bind' while it is in the config ('bundle deployment unbind' first if the state still has it). +Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[NEW_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle does not take it for its own. If the bundle's was renamed outside the bundle, set its name in the config to the new one. If this object is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and then 'bundle deployment bind' to manage it again. Error: cannot create resources.schemas.schema1: Schema 'test-schema-[UNIQUE_NAME]' already exists (400 SCHEMA_ALREADY_EXISTS) @@ -33,6 +33,17 @@ API message: Schema 'test-schema-[UNIQUE_NAME]' already exists Files: 2 uploaded, 0 deleted +>>> print_requests.py //unity-catalog +{ + "method": "POST", + "path": "/api/2.1/unity-catalog/schemas", + "body": { + "catalog_name": "main", + "comment": "c1", + "name": "test-schema-[UNIQUE_NAME]" + } +} + === Binding it again makes it the bundle's >>> [CLI] bundle deployment unbind schema1 @@ -44,7 +55,7 @@ Run 'bundle deploy' to deploy changes to your workspace >>> [CLI] bundle deploy Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... Updated schemas.schema1 -Files: 1 uploaded, 0 deleted +Files: 0 uploaded, 0 deleted Resources: 0 created, 1 changed, 0 deleted, 0 unchanged >>> [CLI] bundle plan diff --git a/acceptance/bundle/resources/schemas/recreated_at_recorded_name/script b/acceptance/bundle/resources/schemas/recreated_at_recorded_name/script index b3bee2efda2..6dfc75857f6 100644 --- a/acceptance/bundle/resources/schemas/recreated_at_recorded_name/script +++ b/acceptance/bundle/resources/schemas/recreated_at_recorded_name/script @@ -24,7 +24,7 @@ print_requests.py //unity-catalog &> LOG.oob_requests title "The new schema has another schema_id: the bundle does not take it for its own, and deploy fails on its name" trace $CLI bundle plan trace musterr $CLI bundle deploy -print_requests.py //unity-catalog &> LOG.failed_deploy_requests +trace print_requests.py //unity-catalog title "Binding it again makes it the bundle's" trace $CLI bundle deployment unbind schema1 diff --git a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt index 3364b361f1a..fd1c40bb64c 100644 --- a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt +++ b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt @@ -18,7 +18,7 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged === The other volume has another volume_id, so the plan does not take it for the bundle's: it plans a create >>> [CLI] bundle plan -Warning: resources.volumes.volume1: id="main.test-schema-[UNIQUE_NAME].myvolume" holds another object: its volume_id is "[FOREIGN_VOLUME_ID]", not "[VOLUME_ID]" as recorded, so this bundle leaves it in place. If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle, or manage it again with 'bundle deployment bind' while it is in the config ('bundle deployment unbind' first if the state still has it). +Warning: resources.volumes.volume1: id="main.test-schema-[UNIQUE_NAME].myvolume" holds another object: its volume_id is "[FOREIGN_VOLUME_ID]", not "[VOLUME_ID]" as recorded, so this bundle does not take it for its own. If the bundle's was renamed outside the bundle, set its name in the config to the new one. If this object is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and then 'bundle deployment bind' to manage it again. create volumes.volume1 @@ -27,7 +27,7 @@ Plan: 1 to add, 0 to change, 0 to delete, 0 unchanged === Destroy does not delete the other volume: it only drops the bundle's from the state >>> [CLI] bundle destroy --auto-approve -Warning: resources.volumes.volume1: id="main.test-schema-[UNIQUE_NAME].myvolume" holds another object: its volume_id is "[FOREIGN_VOLUME_ID]", not "[VOLUME_ID]" as recorded, so this bundle leaves it in place, and drops resources.volumes.volume1 from the deployment state. If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle, or manage it again with 'bundle deployment bind' while it is in the config ('bundle deployment unbind' first if the state still has it). +Warning: resources.volumes.volume1: id="main.test-schema-[UNIQUE_NAME].myvolume" holds another object: its volume_id is "[FOREIGN_VOLUME_ID]", not "[VOLUME_ID]" as recorded, so no delete is sent for it, and resources.volumes.volume1 is dropped from the deployment state. If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle. All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default diff --git a/bundle/deployplan/action.go b/bundle/deployplan/action.go index 698c11e56b1..f64ca6d1f7c 100644 --- a/bundle/deployplan/action.go +++ b/bundle/deployplan/action.go @@ -10,7 +10,8 @@ type Action struct { ResourceKey string ActionType ActionType // Gone mirrors PlanEntry.Gone: the delete is a state-only cleanup because the - // resource no longer exists remotely (confirmed by a read, or an empty-ID state entry). + // resource no longer exists remotely (confirmed by a read, another object at its ID, + // or an empty-ID state entry). Gone bool // StateOnly mirrors PlanEntry.StateOnly: applying the action only changes the state // (a delete of a resource without DoDelete, or a re-key). diff --git a/bundle/deployplan/plan.go b/bundle/deployplan/plan.go index 6fcaa333e08..07113d1d11c 100644 --- a/bundle/deployplan/plan.go +++ b/bundle/deployplan/plan.go @@ -131,8 +131,9 @@ type PlanEntry struct { DependsOn []DependsOnEntry `json:"depends_on,omitempty"` Action ActionType `json:"action,omitempty"` // Gone is set on Delete entries when the resource no longer exists remotely: planning - // confirmed it with a read, or the state entry has an empty ID (left by a recreate in - // an older CLI whose create step failed after the delete). Applying such an entry only + // confirmed it with a read, or found another object at its ID (an internal ID that is + // not the recorded one), or the state entry has an empty ID (left by a recreate in an + // older CLI whose create step failed after the delete). Applying such an entry only // removes it from the state, without calling the delete API, and approval prompts do // not list it as a deletion. Gone bool `json:"gone,omitempty"` diff --git a/bundle/direct/bundle_plan.go b/bundle/direct/bundle_plan.go index 9b53d0ede01..d1dbdadffdd 100644 --- a/bundle/direct/bundle_plan.go +++ b/bundle/direct/bundle_plan.go @@ -262,16 +262,16 @@ func (b *DeploymentBundle) CalculatePlan(ctx context.Context, client *databricks // Treat it as gone: apply cleans up state without re-issuing the delete. entry.Gone = true } else { - // Someone else's resource under the name ours had is never deleted: it is left in - // place, and only the state entry is dropped. + // Someone else's resource under the name ours had is never deleted: no delete is + // sent for it, and only the state entry is dropped. message, err := b.otherObjectAtRecordedID(resourceKey, adapter, id, remoteState) if err != nil { logdiag.LogError(ctx, fmt.Errorf("%s: %w", errorPrefix, err)) return false } if message != "" { - logdiag.LogDiag(ctx, diag.Warningf("%s: %s, and drops %s from the deployment state. %s", - resourceKey, message, resourceKey, otherObjectAdvice)[0]) + logdiag.LogDiag(ctx, diag.Warningf("%s: %s, so no delete is sent for it, and %s is dropped from the deployment state. %s", + resourceKey, message, resourceKey, otherObjectDeleteAdvice)[0]) entry.Gone = true remoteState = nil } @@ -382,8 +382,8 @@ func (b *DeploymentBundle) CalculatePlan(ctx context.Context, client *databricks } } if otherID != "" && remoteState == nil { - logdiag.LogDiag(ctx, diag.Warningf("%s: %s. %s", - resourceKey, otherObjectMessage(adapter, dbentry.ID, savedState, otherID), otherObjectAdvice)[0]) + logdiag.LogDiag(ctx, diag.Warningf("%s: %s, so this bundle does not take it for its own. %s", + resourceKey, otherObjectMessage(adapter, dbentry.ID, savedState, otherID), otherObjectDeployAdvice)[0]) } // We have a choice whether to include remoteState or remoteStateComparable from below. @@ -921,18 +921,23 @@ func otherInternalID(adapter *dresources.Adapter, savedState, remote any) (strin return found, nil } -// otherObjectMessage says that what is at the recorded ID id is not the bundle's: the plan leaves it -// alone. It is told when the bundle's own is not found elsewhere either; a resource deleted and -// created again out of band looks the same, hence otherObjectAdvice. +// otherObjectMessage says that what is at the recorded ID id is not the bundle's. It is told when +// the bundle's own is not found elsewhere either; a resource deleted and created again out of band +// looks the same, hence the advice the callers add. func otherObjectMessage(adapter *dresources.Adapter, id string, savedState any, otherID string) string { saved, _ := adapter.InternalID(savedState) - return fmt.Sprintf("id=%q holds another object: its %s is %q, not %q as recorded, so this bundle leaves it in place", + return fmt.Sprintf("id=%q holds another object: its %s is %q, not %q as recorded", id, adapter.ResourceConfig().InternalIDField, otherID, saved) } -// otherObjectAdvice follows otherObjectMessage, for a resource still in the config (deploy) as well as -// one removed from it (a Delete): bind needs the resource in the config, and a free state entry. -const otherObjectAdvice = "If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle, or manage it again with 'bundle deployment bind' while it is in the config ('bundle deployment unbind' first if the state still has it)." +// otherObjectDeployAdvice follows otherObjectMessage for a resource in the config. The bundle's own +// may still exist under another name (renamed out of band), which findMoved takes once the config +// names it. Bind needs a free state entry, hence unbind first. +const otherObjectDeployAdvice = "If the bundle's was renamed outside the bundle, set its name in the config to the new one. If this object is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and then 'bundle deployment bind' to manage it again." + +// otherObjectDeleteAdvice follows otherObjectMessage for a resource being deleted: its state entry +// is dropped, and it may no longer be in the config, so binding it is not an option. +const otherObjectDeleteAdvice = "If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle." // otherObjectAtRecordedID returns, for a resource to be deleted, what otherObjectMessage says when // remote, read at its recorded ID id, is another object than the one the state records; "" when it From 48f5e2dec478b0ff8cdeafe57ef912e13397eab7 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Sat, 28 Nov 2026 10:13:08 +0000 Subject: [PATCH 18/22] acc: cover a config that names the renamed schema or volume In schemas/ and volumes/foreign_at_recorded_name, a new step points the config at the renamed object: the plan finds it there by its internal ID (update_id), deploy sends nothing to the other object, and destroy deletes the bundle's at its new name. Their Badness notes are dropped, since destroy no longer leaves anything behind in them; schemas/destroy_after_rename_out_of_band still records the leak. aws: TestAccept/bundle/resources/schemas/foreign_at_recorded_name TestAccept/bundle/resources/schemas/destroy_after_rename_out_of_band TestAccept/bundle/resources/schemas/recreated_at_recorded_name TestAccept/bundle/resources/volumes/foreign_at_recorded_name Co-authored-by: Isaac --- .../databricks-moved.yml.tmpl | 9 ++ .../foreign_at_recorded_name/output.txt | 106 +++++++++++++----- .../schemas/foreign_at_recorded_name/script | 16 ++- .../foreign_at_recorded_name/test.toml | 1 - .../databricks-moved.yml.tmpl | 9 ++ .../foreign_at_recorded_name/output.txt | 78 ++++++++++++- .../volumes/foreign_at_recorded_name/script | 14 ++- .../foreign_at_recorded_name/test.toml | 1 - bundle/direct/bundle_plan.go | 6 +- 9 files changed, 197 insertions(+), 43 deletions(-) create mode 100644 acceptance/bundle/resources/schemas/foreign_at_recorded_name/databricks-moved.yml.tmpl create mode 100644 acceptance/bundle/resources/volumes/foreign_at_recorded_name/databricks-moved.yml.tmpl diff --git a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/databricks-moved.yml.tmpl b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/databricks-moved.yml.tmpl new file mode 100644 index 00000000000..293f74fc08e --- /dev/null +++ b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/databricks-moved.yml.tmpl @@ -0,0 +1,9 @@ +bundle: + name: test-bundle-$UNIQUE_NAME + +resources: + schemas: + schema1: + catalog_name: main + name: test-schema-moved-$UNIQUE_NAME + comment: c1 diff --git a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt index 1955bbd3710..6bd46614493 100644 --- a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt +++ b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/output.txt @@ -4,7 +4,7 @@ >>> [CLI] bundle deploy Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... Created schemas.schema1 -Files: 2 uploaded, 0 deleted +Files: 3 uploaded, 0 deleted Resources: 1 created, 0 changed, 0 deleted, 0 unchanged === Out of band, rename the schema away, and create another one under its name @@ -21,27 +21,19 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so this bundle does not take it for its own. If the bundle's was renamed outside the bundle, set its name in the config to the new one. If this object is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and then 'bundle deployment bind' to manage it again. { - "plan_version": [PLAN_VERSION], - "cli_version": "[CLI_VERSION]", - "lineage": "[UUID]", - "serial": 1, - "plan": { - "resources.schemas.schema1": { - "action": "create", - "new_state": { - "value": { - "catalog_name": "main", - "comment": "c1", - "name": "test-schema-[UNIQUE_NAME]" - } - }, - "changes": { - "schema_id": { - "action": "skip", - "reason": "internal_id", - "old": "[SCHEMA_ID]" - } - } + "action": "create", + "new_state": { + "value": { + "catalog_name": "main", + "comment": "c1", + "name": "test-schema-[UNIQUE_NAME]" + } + }, + "changes": { + "schema_id": { + "action": "skip", + "reason": "internal_id", + "old": "[SCHEMA_ID]" } } } @@ -72,14 +64,43 @@ Files: 2 uploaded, 0 deleted } } -=== Destroy does not delete the other schema: it only drops the bundle's from the state +=== Point the config at the bundle's renamed schema: the plan finds it there by its schema_id ->>> [CLI] bundle destroy --auto-approve -Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so no delete is sent for it, and resources.schemas.schema1 is dropped from the deployment state. If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle. +>>> [CLI] bundle plan --output json +{ + "id": "main.test-schema-moved-[UNIQUE_NAME]", + "action": "update_id", + "state_only": true, + "new_state": { + "value": { + "catalog_name": "main", + "comment": "c1", + "name": "test-schema-moved-[UNIQUE_NAME]" + } + }, + "changes": { + "name": { + "action": "skip", + "reason": "remote_already_set", + "old": "test-schema-[UNIQUE_NAME]", + "new": "test-schema-moved-[UNIQUE_NAME]", + "remote": "test-schema-moved-[UNIQUE_NAME]" + }, + "schema_id": { + "action": "skip", + "reason": "internal_id", + "old": "[SCHEMA_ID]", + "remote": "[SCHEMA_ID]" + } + } +} -All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default +=== Deploy takes the renamed schema for the bundle's, and sends nothing to the other one -Destroy: 0 deleted +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Files: 1 uploaded, 0 deleted +Resources: 0 created, 0 changed, 0 deleted, 1 unchanged >>> print_requests.py //unity-catalog @@ -89,6 +110,37 @@ Destroy: 0 deleted "schema_id": "[SCHEMA_ID]" } +>>> [CLI] schemas get main.test-schema-[UNIQUE_NAME] +{ + "full_name": "main.test-schema-[UNIQUE_NAME]", + "schema_id": "[FOREIGN_SCHEMA_ID]" +} + +=== Destroy deletes the bundle's schema at its new name, not the other one + +>>> [CLI] bundle destroy --auto-approve +The following resources will be deleted: + delete resources.schemas.schema1 + +This action will result in the deletion of the following UC schemas. Any underlying data may be lost: + delete resources.schemas.schema1 + +All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default + +Destroy: 1 deleted + +>>> print_requests.py //unity-catalog +{ + "method": "DELETE", + "path": "/api/2.1/unity-catalog/schemas/main.test-schema-moved-[UNIQUE_NAME]", + "q": { + "force": "true" + } +} + +>>> musterr [CLI] schemas get main.test-schema-moved-[UNIQUE_NAME] +Error: Schema 'main.test-schema-moved-[UNIQUE_NAME]' does not exist. + === The other schema is left alone >>> [CLI] schemas get main.test-schema-[UNIQUE_NAME] diff --git a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script index 6f4ae3fa5d1..ab07a2a06e5 100644 --- a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script +++ b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/script @@ -25,16 +25,26 @@ add_repl "$($CLI schemas create "test-schema-${UNIQUE_NAME}" main | jq -r .schem print_requests.py //unity-catalog &> LOG.oob_requests title "The other schema has another schema_id, so the plan does not take it for the bundle's: it plans a create" -trace $CLI bundle plan --output json | plan_filter.py +trace $CLI bundle plan --output json | plan_filter.py resources.schemas.schema1 title "Deploy leaves the other schema alone: the create fails on its name" trace musterr $CLI bundle deploy trace print_requests.py //unity-catalog -title "Destroy does not delete the other schema: it only drops the bundle's from the state" -trace $CLI bundle destroy --auto-approve +title "Point the config at the bundle's renamed schema: the plan finds it there by its schema_id" +envsubst < databricks-moved.yml.tmpl > databricks.yml +trace $CLI bundle plan --output json | plan_filter.py resources.schemas.schema1 + +title "Deploy takes the renamed schema for the bundle's, and sends nothing to the other one" +trace $CLI bundle deploy trace print_requests.py //unity-catalog trace $CLI schemas get "$MOVED_SCHEMA" | schema_fields +trace $CLI schemas get "$SCHEMA" | schema_fields + +title "Destroy deletes the bundle's schema at its new name, not the other one" +trace $CLI bundle destroy --auto-approve +trace print_requests.py //unity-catalog +trace musterr $CLI schemas get "$MOVED_SCHEMA" title "The other schema is left alone" trace $CLI schemas get "$SCHEMA" | schema_fields diff --git a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/test.toml b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/test.toml index 652d759a483..4272a80cf61 100644 --- a/acceptance/bundle/resources/schemas/foreign_at_recorded_name/test.toml +++ b/acceptance/bundle/resources/schemas/foreign_at_recorded_name/test.toml @@ -1,4 +1,3 @@ -Badness = "Destroy does not look for the bundle's schema after a rename out of band: it drops the state entry and leaves the renamed schema behind. It should find it by its schema_id." Cloud = true Ignore = [ diff --git a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/databricks-moved.yml.tmpl b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/databricks-moved.yml.tmpl new file mode 100644 index 00000000000..d9526ac454c --- /dev/null +++ b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/databricks-moved.yml.tmpl @@ -0,0 +1,9 @@ +bundle: + name: test-bundle-$UNIQUE_NAME + +resources: + volumes: + volume1: + catalog_name: main + schema_name: test-schema-$UNIQUE_NAME + name: myvolume-moved diff --git a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt index fd1c40bb64c..ac50b466877 100644 --- a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt +++ b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt @@ -4,7 +4,7 @@ >>> [CLI] bundle deploy Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... Created volumes.volume1 -Files: 3 uploaded, 0 deleted +Files: 4 uploaded, 0 deleted Resources: 1 created, 0 changed, 0 deleted, 0 unchanged === Out of band, rename the volume away, and create another one under its name @@ -24,14 +24,49 @@ create volumes.volume1 Plan: 1 to add, 0 to change, 0 to delete, 0 unchanged -=== Destroy does not delete the other volume: it only drops the bundle's from the state +=== Point the config at the bundle's renamed volume: the plan finds it there by its volume_id ->>> [CLI] bundle destroy --auto-approve -Warning: resources.volumes.volume1: id="main.test-schema-[UNIQUE_NAME].myvolume" holds another object: its volume_id is "[FOREIGN_VOLUME_ID]", not "[VOLUME_ID]" as recorded, so no delete is sent for it, and resources.volumes.volume1 is dropped from the deployment state. If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle. +>>> [CLI] bundle plan --output json +{ + "id": "main.test-schema-[UNIQUE_NAME].myvolume-moved", + "action": "update_id", + "state_only": true, + "new_state": { + "value": { + "catalog_name": "main", + "name": "myvolume-moved", + "schema_name": "test-schema-[UNIQUE_NAME]", + "volume_type": "MANAGED" + } + }, + "changes": { + "name": { + "action": "skip", + "reason": "remote_already_set", + "old": "myvolume", + "new": "myvolume-moved", + "remote": "myvolume-moved" + }, + "storage_location": { + "action": "skip", + "reason": "immutable_backend_value", + "remote": "s3://[METASTORE_NAME]/metastore/[UUID]/volumes/[VOLUME_ID]" + }, + "volume_id": { + "action": "skip", + "reason": "internal_id", + "old": "[VOLUME_ID]", + "remote": "[VOLUME_ID]" + } + } +} -All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default +=== Deploy takes the renamed volume for the bundle's, and sends nothing to the other one -Destroy: 0 deleted +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Files: 3 uploaded, 0 deleted +Resources: 0 created, 0 changed, 0 deleted, 1 unchanged >>> print_requests.py //unity-catalog @@ -41,6 +76,37 @@ Destroy: 0 deleted "volume_id": "[VOLUME_ID]" } +>>> [CLI] volumes read main.test-schema-[UNIQUE_NAME].myvolume +{ + "full_name": "main.test-schema-[UNIQUE_NAME].myvolume", + "volume_id": "[FOREIGN_VOLUME_ID]" +} + +=== Destroy deletes the bundle's volume at its new name, not the other one + +>>> [CLI] bundle destroy --auto-approve +The following resources will be deleted: + delete resources.volumes.volume1 + +This action will result in the deletion of the following volumes. +For managed volumes, the files stored in the volume are also deleted from your +cloud tenant within 30 days. For external volumes, the metadata about the volume +is removed from the catalog, but the underlying files are not deleted: + delete resources.volumes.volume1 + +All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default + +Destroy: 1 deleted + +>>> print_requests.py //unity-catalog +{ + "method": "DELETE", + "path": "/api/2.1/unity-catalog/volumes/main.test-schema-[UNIQUE_NAME].myvolume-moved" +} + +>>> musterr [CLI] volumes read main.test-schema-[UNIQUE_NAME].myvolume-moved +Error: Volume 'main.test-schema-[UNIQUE_NAME].myvolume-moved' does not exist. + === The other volume is left alone >>> [CLI] volumes read main.test-schema-[UNIQUE_NAME].myvolume diff --git a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script index 9f151855a86..a8fb7eb13fc 100644 --- a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script +++ b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script @@ -26,10 +26,20 @@ print_requests.py //unity-catalog &> LOG.oob_requests title "The other volume has another volume_id, so the plan does not take it for the bundle's: it plans a create" trace $CLI bundle plan -title "Destroy does not delete the other volume: it only drops the bundle's from the state" -trace $CLI bundle destroy --auto-approve +title "Point the config at the bundle's renamed volume: the plan finds it there by its volume_id" +envsubst < databricks-moved.yml.tmpl > databricks.yml +trace $CLI bundle plan --output json | plan_filter.py resources.volumes.volume1 + +title "Deploy takes the renamed volume for the bundle's, and sends nothing to the other one" +trace $CLI bundle deploy trace print_requests.py //unity-catalog trace $CLI volumes read "$SCHEMA.myvolume-moved" | volume_fields +trace $CLI volumes read "$SCHEMA.myvolume" | volume_fields + +title "Destroy deletes the bundle's volume at its new name, not the other one" +trace $CLI bundle destroy --auto-approve +trace print_requests.py //unity-catalog +trace musterr $CLI volumes read "$SCHEMA.myvolume-moved" title "The other volume is left alone" trace $CLI volumes read "$SCHEMA.myvolume" | volume_fields diff --git a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/test.toml b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/test.toml index dd82528e899..4272a80cf61 100644 --- a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/test.toml +++ b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/test.toml @@ -1,4 +1,3 @@ -Badness = "Destroy does not look for the bundle's volume after a rename out of band: it drops the state entry and leaves the renamed volume behind. It should find it by its volume_id." Cloud = true Ignore = [ diff --git a/bundle/direct/bundle_plan.go b/bundle/direct/bundle_plan.go index d1dbdadffdd..3263fff4754 100644 --- a/bundle/direct/bundle_plan.go +++ b/bundle/direct/bundle_plan.go @@ -921,9 +921,9 @@ func otherInternalID(adapter *dresources.Adapter, savedState, remote any) (strin return found, nil } -// otherObjectMessage says that what is at the recorded ID id is not the bundle's. It is told when -// the bundle's own is not found elsewhere either; a resource deleted and created again out of band -// looks the same, hence the advice the callers add. +// otherObjectMessage describes the object at the recorded ID id, whose internal ID is otherID and +// not the one savedState records. Callers add the advice, because a resource deleted and created +// again out of band looks the same. func otherObjectMessage(adapter *dresources.Adapter, id string, savedState any, otherID string) string { saved, _ := adapter.InternalID(savedState) return fmt.Sprintf("id=%q holds another object: its %s is %q, not %q as recorded", From 53426c4f5d0ab66990230327919538dba1d5f714 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Sat, 28 Nov 2026 10:44:35 +0000 Subject: [PATCH 19/22] acc: drop the cloud-specific storage_location from the volume plan entry Its bucket URL differs per cloud (s3, gs, abfss), so volumes/foreign_at_recorded_name failed on Azure and GCP. aws: TestAccept/bundle/resources/schemas/foreign_at_recorded_name TestAccept/bundle/resources/schemas/destroy_after_rename_out_of_band TestAccept/bundle/resources/schemas/recreated_at_recorded_name TestAccept/bundle/resources/volumes/foreign_at_recorded_name Co-authored-by: Isaac --- .../resources/volumes/foreign_at_recorded_name/output.txt | 5 ----- .../bundle/resources/volumes/foreign_at_recorded_name/script | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt index ac50b466877..9d28edb22bd 100644 --- a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt +++ b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/output.txt @@ -47,11 +47,6 @@ Plan: 1 to add, 0 to change, 0 to delete, 0 unchanged "new": "myvolume-moved", "remote": "myvolume-moved" }, - "storage_location": { - "action": "skip", - "reason": "immutable_backend_value", - "remote": "s3://[METASTORE_NAME]/metastore/[UUID]/volumes/[VOLUME_ID]" - }, "volume_id": { "action": "skip", "reason": "internal_id", diff --git a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script index a8fb7eb13fc..6f6c30042db 100644 --- a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script +++ b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script @@ -28,7 +28,7 @@ trace $CLI bundle plan title "Point the config at the bundle's renamed volume: the plan finds it there by its volume_id" envsubst < databricks-moved.yml.tmpl > databricks.yml -trace $CLI bundle plan --output json | plan_filter.py resources.volumes.volume1 +trace $CLI bundle plan --output json | plan_filter.py resources.volumes.volume1 | normalize_uc_payload.py storage_location title "Deploy takes the renamed volume for the bundle's, and sends nothing to the other one" trace $CLI bundle deploy From 3b5e0bc82a517027231da2fe3378f38933209844 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Sun, 29 Nov 2026 10:09:20 +0000 Subject: [PATCH 20/22] acc: show destroy leaving another object at the recorded name alone schemas/destroy_with_other_at_recorded_name: while the config still names the recorded schema, destroy warns, sends no DELETE, drops the entry, and leaves both the other schema and the bundle's renamed one in place. The Badness note records the leak, which the follow-up fixes. aws: TestAccept/bundle/resources/schemas/foreign_at_recorded_name TestAccept/bundle/resources/schemas/destroy_after_rename_out_of_band TestAccept/bundle/resources/schemas/recreated_at_recorded_name TestAccept/bundle/resources/schemas/destroy_with_other_at_recorded_name TestAccept/bundle/resources/volumes/foreign_at_recorded_name Co-authored-by: Isaac --- .../databricks.yml.tmpl | 9 ++++ .../out.test.toml | 2 + .../output.txt | 49 +++++++++++++++++++ .../script | 36 ++++++++++++++ .../test.toml | 8 +++ 5 files changed, 104 insertions(+) create mode 100644 acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/databricks.yml.tmpl create mode 100644 acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/out.test.toml create mode 100644 acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/output.txt create mode 100644 acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/script create mode 100644 acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/test.toml diff --git a/acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/databricks.yml.tmpl b/acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/databricks.yml.tmpl new file mode 100644 index 00000000000..d73aacf0d14 --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/databricks.yml.tmpl @@ -0,0 +1,9 @@ +bundle: + name: test-bundle-$UNIQUE_NAME + +resources: + schemas: + schema1: + catalog_name: main + name: test-schema-$UNIQUE_NAME + comment: c1 diff --git a/acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/out.test.toml b/acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/out.test.toml new file mode 100644 index 00000000000..0f167effef1 --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/out.test.toml @@ -0,0 +1,2 @@ +Cloud = true +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/output.txt b/acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/output.txt new file mode 100644 index 00000000000..6e300221caa --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/output.txt @@ -0,0 +1,49 @@ + +=== Deploy a schema + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default/files... +Created schemas.schema1 +Files: 2 uploaded, 0 deleted +Resources: 1 created, 0 changed, 0 deleted, 0 unchanged + +=== Out of band, rename the schema away, and create another one under its name + +>>> [CLI] schemas update main.test-schema-[UNIQUE_NAME] --new-name test-schema-moved-[UNIQUE_NAME] +{ + "full_name": "main.test-schema-moved-[UNIQUE_NAME]", + "schema_id": "[SCHEMA_ID]" +} + +=== Destroy sends no delete for the other schema: it warns and only drops the bundle's entry from the state + +>>> [CLI] bundle destroy --auto-approve +Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so no delete is sent for it, and resources.schemas.schema1 is dropped from the deployment state. If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle. + +All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default + +Destroy: 0 deleted + +>>> print_requests.py //unity-catalog + +>>> [CLI] bundle summary --output json +{ + "id": null, + "modified_status": "created" +} + +=== The other schema is left alone + +>>> [CLI] schemas get main.test-schema-[UNIQUE_NAME] +{ + "full_name": "main.test-schema-[UNIQUE_NAME]", + "schema_id": "[FOREIGN_SCHEMA_ID]" +} + +=== The bundle's renamed schema is left behind + +>>> [CLI] schemas get main.test-schema-moved-[UNIQUE_NAME] +{ + "full_name": "main.test-schema-moved-[UNIQUE_NAME]", + "schema_id": "[SCHEMA_ID]" +} diff --git a/acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/script b/acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/script new file mode 100644 index 00000000000..bee3c7c9514 --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/script @@ -0,0 +1,36 @@ +envsubst < databricks.yml.tmpl > databricks.yml + +SCHEMA="main.test-schema-${UNIQUE_NAME}" +MOVED_SCHEMA="main.test-schema-moved-${UNIQUE_NAME}" + +cleanup() { + # Whatever destroy left behind. + errcode $CLI schemas delete "$MOVED_SCHEMA" --force > LOG.cleanup 2>&1 + errcode $CLI schemas delete "$SCHEMA" --force >> LOG.cleanup 2>&1 + + # Consume the cleanup's requests so none are left recorded. + print_requests.py //unity-catalog &> LOG.cleanup_requests +} +trap cleanup EXIT + +title "Deploy a schema" +trace $CLI bundle deploy +print_requests.py //unity-catalog &> LOG.deploy_requests +add_repl "$($CLI schemas get "$SCHEMA" | jq -r .schema_id)" SCHEMA_ID + +title "Out of band, rename the schema away, and create another one under its name" +trace $CLI schemas update "$SCHEMA" --new-name "test-schema-moved-${UNIQUE_NAME}" | schema_fields +add_repl "$($CLI schemas create "test-schema-${UNIQUE_NAME}" main | jq -r .schema_id)" FOREIGN_SCHEMA_ID +print_requests.py //unity-catalog &> LOG.oob_requests + +# The config still names the schema at its old name. +title "Destroy sends no delete for the other schema: it warns and only drops the bundle's entry from the state" +trace $CLI bundle destroy --auto-approve +trace print_requests.py //unity-catalog +trace $CLI bundle summary --output json | jq '.resources.schemas.schema1 | {id, modified_status}' + +title "The other schema is left alone" +trace $CLI schemas get "$SCHEMA" | schema_fields + +title "The bundle's renamed schema is left behind" +trace $CLI schemas get "$MOVED_SCHEMA" | schema_fields diff --git a/acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/test.toml b/acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/test.toml new file mode 100644 index 00000000000..652d759a483 --- /dev/null +++ b/acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/test.toml @@ -0,0 +1,8 @@ +Badness = "Destroy does not look for the bundle's schema after a rename out of band: it drops the state entry and leaves the renamed schema behind. It should find it by its schema_id." +Cloud = true + +Ignore = [ + ".databricks", + ".gitignore", + "databricks.yml", +] From d69a66dcdeb14449cb671354e94acc554c14e710 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Sun, 29 Nov 2026 10:22:25 +0000 Subject: [PATCH 21/22] direct: delete warning says a renamed copy is left behind; plainer changelog - The delete warning names the resource key once and says the bundle's renamed copy is left in place and must be deleted outside the bundle. - otherInternalID's comment names both causes. - replay_tfstate.py matches names lowercased on both sides, one lookup. - Plainer changelog fragment. aws: TestAccept/bundle/resources/schemas/foreign_at_recorded_name TestAccept/bundle/resources/schemas/destroy_after_rename_out_of_band TestAccept/bundle/resources/schemas/recreated_at_recorded_name TestAccept/bundle/resources/schemas/destroy_with_other_at_recorded_name TestAccept/bundle/resources/volumes/foreign_at_recorded_name Co-authored-by: Isaac --- .nextchanges/bundles/schema-volume-identity.md | 2 +- acceptance/bin/replay_tfstate.py | 14 ++++++-------- .../destroy_with_other_at_recorded_name/output.txt | 2 +- bundle/direct/bundle_plan.go | 13 +++++++------ 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/.nextchanges/bundles/schema-volume-identity.md b/.nextchanges/bundles/schema-volume-identity.md index 8c7917b49eb..4f70baad412 100644 --- a/.nextchanges/bundles/schema-volume-identity.md +++ b/.nextchanges/bundles/schema-volume-identity.md @@ -1 +1 @@ -* Fixed the direct deployment engine taking a schema or volume that someone else created under the name of the bundle's own, after it was renamed out of band, for the bundle's: `bundle deploy` no longer updates it and `bundle destroy` no longer deletes it; destroy drops the bundle's entry from the state and warns. A schema or volume deleted and created again outside the bundle is no longer the bundle's either: deploy fails on its name and destroy leaves it, until it is bound again with `bundle deployment unbind` and `bundle deployment bind`. ([#8056](https://github.com/databricks/cli/pull/8056)) +* The direct deployment engine no longer updates or deletes a schema or volume at the bundle's recorded name when its `schema_id` or `volume_id` differs from the recorded one: `bundle deploy` plans a create instead, and `bundle destroy` only drops the bundle's entry from the state, with a warning. A schema or volume deleted and created again outside the bundle is therefore no longer taken for the bundle's: deploy fails on its name until it is bound again with `bundle deployment unbind` and `bundle deployment bind`. ([#8056](https://github.com/databricks/cli/pull/8056)) diff --git a/acceptance/bin/replay_tfstate.py b/acceptance/bin/replay_tfstate.py index 4b4ee67a979..d481c050d3e 100755 --- a/acceptance/bin/replay_tfstate.py +++ b/acceptance/bin/replay_tfstate.py @@ -125,10 +125,10 @@ def main(): continue for nf in NAME_FIELDS: if attrs.get(nf): - name_to_attrs[str(attrs[nf])] = attrs - # UC lowercases names (a schema created as MySchema is stored as myschema), so the - # tfstate may hold the lowercased form of the name the create request sent. - name_to_attrs.setdefault(str(attrs[nf]).lower(), attrs) + # Keyed lowercased: UC lowercases names (a schema created as MySchema is stored as + # myschema), so the tfstate may hold the lowercased form of the name the create + # request sent. + name_to_attrs[str(attrs[nf]).lower()] = attrs id_map = {} for req in requests: @@ -152,12 +152,10 @@ def main(): # map its recorded id to the freshly minted one. create_name = None if isinstance(body, dict): - create_name = next((str(body[nf]) for nf in NAME_FIELDS if body.get(nf)), None) - if create_name is not None and create_name not in name_to_attrs: - create_name = create_name.lower() + create_name = next((str(body[nf]).lower() for nf in NAME_FIELDS if body.get(nf)), None) if req["method"] == "POST" and create_name in name_to_attrs: attrs = name_to_attrs[create_name] - if str(attrs["id"]) != create_name: + if str(attrs["id"]).lower() != create_name: # Backend-minted primary id (jobs, pipelines, volumes): map recorded -> minted. new_id = next((str(resp[f]) for f in ID_FIELDS if f in resp), None) if new_id is None: diff --git a/acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/output.txt b/acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/output.txt index 6e300221caa..39705caa888 100644 --- a/acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/output.txt +++ b/acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/output.txt @@ -18,7 +18,7 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged === Destroy sends no delete for the other schema: it warns and only drops the bundle's entry from the state >>> [CLI] bundle destroy --auto-approve -Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so no delete is sent for it, and resources.schemas.schema1 is dropped from the deployment state. If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle. +Warning: resources.schemas.schema1: id="main.test-schema-[UNIQUE_NAME]" holds another object: its schema_id is "[FOREIGN_SCHEMA_ID]", not "[SCHEMA_ID]" as recorded, so no delete is sent for it, and its entry is dropped from the deployment state. If the bundle's was renamed outside the bundle, it is left in place under its new name: delete it outside the bundle. If this object is the bundle's (deleted and created again outside the bundle), delete it outside the bundle. All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle-[UNIQUE_NAME]/default diff --git a/bundle/direct/bundle_plan.go b/bundle/direct/bundle_plan.go index 3263fff4754..99037e74b53 100644 --- a/bundle/direct/bundle_plan.go +++ b/bundle/direct/bundle_plan.go @@ -270,8 +270,8 @@ func (b *DeploymentBundle) CalculatePlan(ctx context.Context, client *databricks return false } if message != "" { - logdiag.LogDiag(ctx, diag.Warningf("%s: %s, so no delete is sent for it, and %s is dropped from the deployment state. %s", - resourceKey, message, resourceKey, otherObjectDeleteAdvice)[0]) + logdiag.LogDiag(ctx, diag.Warningf("%s: %s, so no delete is sent for it, and its entry is dropped from the deployment state. %s", + resourceKey, message, otherObjectDeleteAdvice)[0]) entry.Gone = true remoteState = nil } @@ -900,8 +900,8 @@ func isParentIDField(adapter *dresources.Adapter, path *structpath.PathNode) boo } // otherInternalID returns the internal ID of remote, read at the recorded ID, if it is another -// object than the one savedState records: ours was renamed away and another one created under its -// old name. It is "" when the resource is ours, or when either side has no internal ID to compare. +// object than the one savedState records: someone else's, created under the name ours had after +// ours was renamed away, or ours deleted and created again outside the bundle. It is "" when the resource is ours, or when either side has no internal ID to compare. func otherInternalID(adapter *dresources.Adapter, savedState, remote any) (string, error) { if !adapter.HasInternalID() || remote == nil { return "", nil @@ -936,8 +936,9 @@ func otherObjectMessage(adapter *dresources.Adapter, id string, savedState any, const otherObjectDeployAdvice = "If the bundle's was renamed outside the bundle, set its name in the config to the new one. If this object is the bundle's (deleted and created again outside the bundle), run 'bundle deployment unbind' and then 'bundle deployment bind' to manage it again." // otherObjectDeleteAdvice follows otherObjectMessage for a resource being deleted: its state entry -// is dropped, and it may no longer be in the config, so binding it is not an option. -const otherObjectDeleteAdvice = "If it is the bundle's (deleted and created again outside the bundle), delete it outside the bundle." +// is dropped, and it may no longer be in the config, so binding it is not an option. The bundle's +// own, if renamed outside the bundle, is not looked for, so the user has to delete it. +const otherObjectDeleteAdvice = "If the bundle's was renamed outside the bundle, it is left in place under its new name: delete it outside the bundle. If this object is the bundle's (deleted and created again outside the bundle), delete it outside the bundle." // otherObjectAtRecordedID returns, for a resource to be deleted, what otherObjectMessage says when // remote, read at its recorded ID id, is another object than the one the state records; "" when it From efce9ca747bc260bb512a406dff72d8fbcd0a000 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Mon, 30 Nov 2026 10:11:15 +0000 Subject: [PATCH 22/22] acc: use || true for cleanup in the new tests, errcode is gone main removed the errcode helper (#7975). The EXIT-trap cleanup in schemas/destroy_with_other_at_recorded_name, schemas/recreated_at_recorded_name and volumes/foreign_at_recorded_name now uses `cmd > LOG.cleanup 2>&1 || true`, as .agents/rules/testing.md asks. aws: TestAccept/bundle/resources/schemas/foreign_at_recorded_name TestAccept/bundle/resources/schemas/destroy_after_rename_out_of_band TestAccept/bundle/resources/schemas/recreated_at_recorded_name TestAccept/bundle/resources/schemas/destroy_with_other_at_recorded_name TestAccept/bundle/resources/volumes/foreign_at_recorded_name Co-authored-by: Isaac --- .../schemas/destroy_with_other_at_recorded_name/script | 4 ++-- .../resources/schemas/recreated_at_recorded_name/script | 2 +- .../bundle/resources/volumes/foreign_at_recorded_name/script | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/script b/acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/script index bee3c7c9514..f5efb87ee69 100644 --- a/acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/script +++ b/acceptance/bundle/resources/schemas/destroy_with_other_at_recorded_name/script @@ -5,8 +5,8 @@ MOVED_SCHEMA="main.test-schema-moved-${UNIQUE_NAME}" cleanup() { # Whatever destroy left behind. - errcode $CLI schemas delete "$MOVED_SCHEMA" --force > LOG.cleanup 2>&1 - errcode $CLI schemas delete "$SCHEMA" --force >> LOG.cleanup 2>&1 + $CLI schemas delete "$MOVED_SCHEMA" --force > LOG.cleanup 2>&1 || true + $CLI schemas delete "$SCHEMA" --force >> LOG.cleanup 2>&1 || true # Consume the cleanup's requests so none are left recorded. print_requests.py //unity-catalog &> LOG.cleanup_requests diff --git a/acceptance/bundle/resources/schemas/recreated_at_recorded_name/script b/acceptance/bundle/resources/schemas/recreated_at_recorded_name/script index 6dfc75857f6..5e51f296741 100644 --- a/acceptance/bundle/resources/schemas/recreated_at_recorded_name/script +++ b/acceptance/bundle/resources/schemas/recreated_at_recorded_name/script @@ -4,7 +4,7 @@ SCHEMA="main.test-schema-${UNIQUE_NAME}" cleanup() { trace $CLI bundle destroy --auto-approve - errcode $CLI schemas delete "$SCHEMA" --force > LOG.cleanup 2>&1 + $CLI schemas delete "$SCHEMA" --force > LOG.cleanup 2>&1 || true # Consume the destroy's requests so none are left recorded. print_requests.py //unity-catalog &> LOG.destroy_requests diff --git a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script index 6f6c30042db..f061dbed48a 100644 --- a/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script +++ b/acceptance/bundle/resources/volumes/foreign_at_recorded_name/script @@ -4,7 +4,7 @@ envsubst < databricks.yml.tmpl > databricks.yml SCHEMA="main.test-schema-${UNIQUE_NAME}" cleanup() { - errcode $CLI schemas delete "$SCHEMA" --force > LOG.cleanup 2>&1 + $CLI schemas delete "$SCHEMA" --force > LOG.cleanup 2>&1 || true # Consume the destroy's requests so none are left recorded. print_requests.py //unity-catalog &> LOG.destroy_requests