Skip to content

Commit d6b6064

Browse files
authored
direct: Support schemas deletion and recreation (#3341)
## Changes Implement missing delete and recreate flows for schemas. ## Tests New acceptance test.
1 parent afdd9f4 commit d6b6064

File tree

10 files changed

+113
-2
lines changed

10 files changed

+113
-2
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
resources:
2+
schemas:
3+
schema1:
4+
name: myschema
5+
catalog_name: main
6+
comment: COMMENT1
7+
# Note, uncommenting causes different requests sent between TF and direct
8+
#storage_root: ""

acceptance/bundle/resources/schemas/out.test.toml renamed to acceptance/bundle/resources/schemas/recreate/out.test.toml

File renamed without changes.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
>>> [CLI] bundle deploy
3+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files...
4+
Deploying resources...
5+
Updating deployment state...
6+
Deployment complete!
7+
8+
>>> print_requests
9+
{
10+
"method": "POST",
11+
"path": "/api/2.1/unity-catalog/schemas",
12+
"body": {
13+
"catalog_name": "main",
14+
"comment": "COMMENT1",
15+
"name": "myschema"
16+
}
17+
}
18+
19+
>>> [CLI] bundle summary -o json
20+
{
21+
"schema1": {
22+
"catalog_name": "main",
23+
"comment": "COMMENT1",
24+
"id": "main.myschema",
25+
"name": "myschema",
26+
"url": "[DATABRICKS_URL]/explore/data/main/myschema?o=[NUMID]"
27+
}
28+
}
29+
30+
=== Update catalog_name and re-deploy
31+
>>> update_file.py databricks.yml catalog_name: main catalog_name: newmain
32+
33+
>>> [CLI] bundle plan
34+
recreate schemas.schema1
35+
36+
>>> [CLI] bundle deploy --auto-approve
37+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files...
38+
The following UC schemas will be deleted or recreated. Any underlying data may be lost:
39+
recreate schema schema1
40+
Deploying resources...
41+
Updating deployment state...
42+
Deployment complete!
43+
44+
>>> print_requests
45+
{
46+
"method": "DELETE",
47+
"path": "/api/2.1/unity-catalog/schemas/main.myschema"
48+
}
49+
{
50+
"method": "POST",
51+
"path": "/api/2.1/unity-catalog/schemas",
52+
"body": {
53+
"catalog_name": "newmain",
54+
"comment": "COMMENT1",
55+
"name": "myschema"
56+
}
57+
}
58+
59+
>>> musterr [CLI] schemas get main.myschema
60+
Error: Resource catalog.SchemaInfo not found: main.myschema
61+
62+
Exit code (musterr): 1
63+
64+
>>> [CLI] schemas get newmain.myschema
65+
{
66+
"catalog_name":"newmain",
67+
"comment":"COMMENT1",
68+
"full_name":"newmain.myschema",
69+
"name":"myschema"
70+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
echo "*" > .gitignore
2+
trace $CLI bundle deploy
3+
4+
print_requests() {
5+
jq 'select(.method != "GET" and (.path | contains("/unity")))' < out.requests.txt
6+
rm out.requests.txt
7+
}
8+
9+
trace print_requests
10+
trace $CLI bundle summary -o json | jq .resources.schemas
11+
12+
title "Update catalog_name and re-deploy"
13+
trace update_file.py databricks.yml "catalog_name: main" "catalog_name: newmain"
14+
trace $CLI bundle plan
15+
trace $CLI bundle deploy --auto-approve
16+
trace print_requests
17+
18+
trace musterr $CLI schemas get main.myschema
19+
trace $CLI schemas get newmain.myschema
20+
rm out.requests.txt

acceptance/bundle/resources/schemas/databricks.yml renamed to acceptance/bundle/resources/schemas/update/databricks.yml

File renamed without changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Local = true
2+
Cloud = false
3+
4+
[EnvMatrix]
5+
DATABRICKS_CLI_DEPLOYMENT = ["terraform", "direct-exp"]
File renamed without changes.
File renamed without changes.

bundle/terranova/tnresources/resource.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,12 @@ var SupportedResources = map[string]ResourceSettings{
8080
New: reflect.ValueOf(NewResourceSchema),
8181
ConfigType: reflect.TypeOf(ResourceSchema{}.config),
8282
DeleteFN: DeleteSchema,
83-
// RecreateFields: TODO
83+
// TF: https://github.com/databricks/terraform-provider-databricks/blob/03a2515/catalog/resource_schema.go#L14
84+
RecreateFields: mkMap(
85+
".name",
86+
".catalog_name",
87+
".storage_root",
88+
),
8489
},
8590
"volumes": {
8691
New: reflect.ValueOf(NewResourceVolume),

bundle/terranova/tnresources/schema.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ func (r *ResourceSchema) DoUpdate(ctx context.Context, id string) (string, error
5252
}
5353

5454
func DeleteSchema(ctx context.Context, client *databricks.WorkspaceClient, id string) error {
55-
// TODO: implement schema deletion
55+
err := client.Schemas.Delete(ctx, catalog.DeleteSchemaRequest{FullName: id, Force: true})
56+
if err != nil {
57+
return SDKError{Method: "Schemas.Delete", Err: err}
58+
}
5659
return nil
5760
}
5861

0 commit comments

Comments
 (0)