Skip to content

Commit 240a44d

Browse files
authored
Use force flag for schema deletion (#4705)
## Changes - Use https://docs.databricks.com/api/workspace/schemas/delete#force when deleting schemas - No longer delete all the contained resources manually. ## Tests Existing tests. I haven't check if there is integration test for this. Resolves #3388, resolves #3375
1 parent 3dbac01 commit 240a44d

File tree

3 files changed

+5
-120
lines changed

3 files changed

+5
-120
lines changed

NEXT_CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### New Features and Improvements
66

7+
* Faster and more reliable schema deletion. It now uses schemas/delete call with force=true flag instead of manually listing and deleting all resources. [#4705](https://github.com/databricks/terraform-provider-databricks/pull/4705)
8+
79
### Bug Fixes
810

911
* Fix validation of S3 bucket name in `databricks_aws_unity_catalog_policy` and `databricks_aws_bucket_policy` [#4691](https://github.com/databricks/terraform-provider-databricks/pull/4691)

catalog/resource_schema.go

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package catalog
22

33
import (
44
"context"
5-
"strings"
65

76
"github.com/databricks/databricks-sdk-go/service/catalog"
87
"github.com/databricks/terraform-provider-databricks/common"
@@ -157,53 +156,7 @@ func ResourceSchema() common.Resource {
157156
if err != nil {
158157
return err
159158
}
160-
if force {
161-
// delete all tables & views
162-
tables, err := w.Tables.ListAll(ctx, catalog.ListTablesRequest{
163-
CatalogName: strings.Split(name, ".")[0],
164-
SchemaName: strings.Split(name, ".")[1],
165-
})
166-
if err != nil {
167-
return err
168-
}
169-
for _, t := range tables {
170-
w.Tables.DeleteByFullName(ctx, t.FullName)
171-
}
172-
// delete all volumes
173-
volumes, err := w.Volumes.ListAll(ctx, catalog.ListVolumesRequest{
174-
CatalogName: strings.Split(name, ".")[0],
175-
SchemaName: strings.Split(name, ".")[1],
176-
})
177-
if err != nil {
178-
return err
179-
}
180-
for _, v := range volumes {
181-
w.Volumes.DeleteByName(ctx, v.FullName)
182-
}
183-
// delete all functions
184-
functions, err := w.Functions.ListAll(ctx, catalog.ListFunctionsRequest{
185-
CatalogName: strings.Split(name, ".")[0],
186-
SchemaName: strings.Split(name, ".")[1],
187-
})
188-
if err != nil {
189-
return err
190-
}
191-
for _, f := range functions {
192-
w.Functions.DeleteByName(ctx, f.FullName)
193-
}
194-
// delete all models
195-
models, err := w.RegisteredModels.ListAll(ctx, catalog.ListRegisteredModelsRequest{
196-
CatalogName: strings.Split(name, ".")[0],
197-
SchemaName: strings.Split(name, ".")[1],
198-
})
199-
if err != nil {
200-
return err
201-
}
202-
for _, m := range models {
203-
w.RegisteredModels.DeleteByFullName(ctx, m.FullName)
204-
}
205-
}
206-
return w.Schemas.DeleteByFullName(ctx, name)
159+
return w.Schemas.Delete(ctx, catalog.DeleteSchemaRequest{FullName: name, Force: force})
207160
},
208161
}
209162
}

catalog/resource_schema_test.go

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ func TestUpdateSchemaForceNew(t *testing.T) {
388388
func TestDeleteSchema(t *testing.T) {
389389
qa.ResourceFixture{
390390
MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) {
391-
w.GetMockSchemasAPI().EXPECT().DeleteByFullName(mock.Anything, "b.a").Return(nil)
391+
w.GetMockSchemasAPI().EXPECT().Delete(mock.Anything, catalog.DeleteSchemaRequest{FullName: "b.a"}).Return(nil)
392392
},
393393
Resource: ResourceSchema(),
394394
Delete: true,
@@ -405,77 +405,7 @@ func TestDeleteSchema(t *testing.T) {
405405
func TestForceDeleteSchema(t *testing.T) {
406406
qa.ResourceFixture{
407407
MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) {
408-
t := w.GetMockTablesAPI().EXPECT()
409-
t.ListAll(mock.Anything, catalog.ListTablesRequest{
410-
CatalogName: "b",
411-
SchemaName: "a",
412-
}).Return([]catalog.TableInfo{
413-
{
414-
CatalogName: "b",
415-
SchemaName: "a",
416-
Name: "c",
417-
FullName: "b.a.c",
418-
TableType: "MANAGED",
419-
},
420-
{
421-
CatalogName: "b",
422-
SchemaName: "a",
423-
Name: "d",
424-
FullName: "b.a.d",
425-
TableType: "VIEW",
426-
},
427-
}, nil)
428-
t.DeleteByFullName(mock.Anything, "b.a.c").Return(nil)
429-
t.DeleteByFullName(mock.Anything, "b.a.d").Return(nil)
430-
v := w.GetMockVolumesAPI().EXPECT()
431-
v.ListAll(mock.Anything, catalog.ListVolumesRequest{
432-
CatalogName: "b",
433-
SchemaName: "a",
434-
}).Return([]catalog.VolumeInfo{
435-
{
436-
CatalogName: "b",
437-
SchemaName: "a",
438-
Name: "c",
439-
FullName: "b.a.c",
440-
VolumeType: catalog.VolumeTypeManaged,
441-
},
442-
{
443-
CatalogName: "b",
444-
SchemaName: "a",
445-
Name: "d",
446-
FullName: "b.a.d",
447-
VolumeType: catalog.VolumeTypeExternal,
448-
},
449-
}, nil)
450-
v.DeleteByName(mock.Anything, "b.a.c").Return(nil)
451-
v.DeleteByName(mock.Anything, "b.a.d").Return(nil)
452-
f := w.GetMockFunctionsAPI().EXPECT()
453-
f.ListAll(mock.Anything, catalog.ListFunctionsRequest{
454-
CatalogName: "b",
455-
SchemaName: "a",
456-
}).Return([]catalog.FunctionInfo{
457-
{
458-
CatalogName: "b",
459-
SchemaName: "a",
460-
Name: "c",
461-
FullName: "b.a.c",
462-
},
463-
}, nil)
464-
f.DeleteByName(mock.Anything, "b.a.c").Return(nil)
465-
m := w.GetMockRegisteredModelsAPI().EXPECT()
466-
m.ListAll(mock.Anything, catalog.ListRegisteredModelsRequest{
467-
CatalogName: "b",
468-
SchemaName: "a",
469-
}).Return([]catalog.RegisteredModelInfo{
470-
{
471-
CatalogName: "b",
472-
SchemaName: "a",
473-
Name: "c",
474-
FullName: "b.a.c",
475-
},
476-
}, nil)
477-
m.DeleteByFullName(mock.Anything, "b.a.c").Return(nil)
478-
w.GetMockSchemasAPI().EXPECT().DeleteByFullName(mock.Anything, "b.a").Return(nil)
408+
w.GetMockSchemasAPI().EXPECT().Delete(mock.Anything, catalog.DeleteSchemaRequest{FullName: "b.a", Force: true}).Return(nil)
479409
},
480410
Resource: ResourceSchema(),
481411
Delete: true,

0 commit comments

Comments
 (0)