Skip to content

Commit 54cdadd

Browse files
authored
[Python] Add schemas support (#3389)
## Changes Add schema resource type support to Python ## Why It makes it possible to define schemas with Python code, similar to jobs and pipelines ## Tests Acceptance and unit tests
1 parent d26d11d commit 54cdadd

File tree

18 files changed

+374
-1
lines changed

18 files changed

+374
-1
lines changed

NEXT_CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
### Dependency updates
1010

1111
### Bundles
12+
* Add support for schemas in Python support ([#3389])(https://github.com/databricks/cli/pull/3389))
1213

1314
* Updated templates to use the new "environment_version" property instead of the deprecated "client" property ([#3554](https://github.com/databricks/cli/pull/3554)).
1415

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
bundle:
2+
name: my_project
3+
4+
sync: {paths: []} # don't need to copy files
5+
6+
experimental:
7+
python:
8+
resources:
9+
- "resources:load_resources"
10+
mutators:
11+
- "mutators:update_schema"
12+
13+
resources:
14+
schemas:
15+
my_schema_1:
16+
name: "My Schema"
17+
catalog_name: "my_catalog"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from dataclasses import replace
2+
3+
from databricks.bundles.core import schema_mutator
4+
from databricks.bundles.schemas import Schema
5+
6+
7+
@schema_mutator
8+
def update_schema(schema: Schema) -> Schema:
9+
assert isinstance(schema.name, str)
10+
11+
return replace(schema, name=f"{schema.name} (updated)")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Local = true
2+
Cloud = false
3+
4+
[EnvMatrix]
5+
DATABRICKS_CLI_DEPLOYMENT = ["terraform", "direct-exp"]
6+
UV_ARGS = ["--with-requirements requirements-latest.txt --no-cache"]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
>>> uv run --with-requirements requirements-latest.txt --no-cache -q [CLI] bundle validate --output json
3+
{
4+
"experimental": {
5+
"python": {
6+
"mutators": [
7+
"mutators:update_schema"
8+
],
9+
"resources": [
10+
"resources:load_resources"
11+
]
12+
}
13+
},
14+
"resources": {
15+
"schemas": {
16+
"my_schema_1": {
17+
"catalog_name": "my_catalog",
18+
"name": "My Schema (updated)"
19+
},
20+
"my_schema_2": {
21+
"catalog_name": "my_catalog_2",
22+
"name": "My Schema (2) (updated)"
23+
}
24+
}
25+
}
26+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from databricks.bundles.core import Resources
2+
3+
4+
def load_resources() -> Resources:
5+
resources = Resources()
6+
7+
resources.add_schema(
8+
"my_schema_2",
9+
{
10+
"name": "My Schema (2)",
11+
"catalog_name": "my_catalog_2",
12+
},
13+
)
14+
15+
return resources
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
echo "$DATABRICKS_BUNDLES_WHEEL" > "requirements-latest.txt"
2+
3+
trace uv run $UV_ARGS -q $CLI bundle validate --output json | \
4+
jq "pick(.experimental.python, .resources)"
5+
6+
rm -fr .databricks __pycache__
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Local = true
2+
Cloud = false # tests don't interact with APIs
3+
4+
[EnvMatrix]
5+
UV_ARGS = [
6+
# pipelines are only supported in the latest version of the wheel
7+
"--with-requirements requirements-latest.txt --no-cache",
8+
]

experimental/python/codegen/codegen/packages.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
RESOURCE_NAMESPACE = {
66
"resources.Job": "jobs",
77
"resources.Pipeline": "pipelines",
8+
"resources.Schema": "schemas",
89
"resources.Volume": "volumes",
910
}
1011

experimental/python/databricks/bundles/core/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"load_resources_from_modules",
1919
"load_resources_from_package_module",
2020
"pipeline_mutator",
21+
"schema_mutator",
2122
"variables",
2223
"volume_mutator",
2324
]
@@ -40,6 +41,7 @@
4041
ResourceMutator,
4142
job_mutator,
4243
pipeline_mutator,
44+
schema_mutator,
4345
volume_mutator,
4446
)
4547
from databricks.bundles.core._resources import Resources

0 commit comments

Comments
 (0)