66from databricks .bundles .core ._transform import _transform
77
88if TYPE_CHECKING :
9+ from databricks .bundles .catalog ._models .schema import Schema , SchemaParam
910 from databricks .bundles .jobs ._models .job import Job , JobParam
1011 from databricks .bundles .pipelines ._models .pipeline import Pipeline , PipelineParam
1112
@@ -57,6 +58,7 @@ def load_resources(bundle: Bundle) -> Resources:
5758 def __init__ (self ):
5859 self ._jobs = dict [str , "Job" ]()
5960 self ._pipelines = dict [str , "Pipeline" ]()
61+ self ._schemas = dict [str , "Schema" ]()
6062 self ._locations = dict [tuple [str , ...], Location ]()
6163 self ._diagnostics = Diagnostics ()
6264
@@ -68,6 +70,10 @@ def jobs(self) -> dict[str, "Job"]:
6870 def pipelines (self ) -> dict [str , "Pipeline" ]:
6971 return self ._pipelines
7072
73+ @property
74+ def schemas (self ) -> dict [str , "Schema" ]:
75+ return self ._schemas
76+
7177 @property
7278 def diagnostics (self ) -> Diagnostics :
7379 """
@@ -91,6 +97,7 @@ def add_resource(
9197 :param location: optional location of the resource in the source code
9298 """
9399
100+ from databricks .bundles .catalog import Schema
94101 from databricks .bundles .jobs import Job
95102 from databricks .bundles .pipelines import Pipeline
96103
@@ -101,6 +108,8 @@ def add_resource(
101108 self .add_job (resource_name , resource , location = location )
102109 case Pipeline ():
103110 self .add_pipeline (resource_name , resource , location = location )
111+ case Schema ():
112+ self .add_schema (resource_name , resource , location = location )
104113 case _:
105114 raise ValueError (f"Unsupported resource type: { type (resource )} " )
106115
@@ -168,6 +177,38 @@ def add_pipeline(
168177
169178 self ._pipelines [resource_name ] = pipeline
170179
180+ def add_schema (
181+ self ,
182+ resource_name : str ,
183+ schema : "SchemaParam" ,
184+ * ,
185+ location : Optional [Location ] = None ,
186+ ) -> None :
187+ """
188+ Adds a schema to the collection of resources. Resource name must be unique across all schemas.
189+
190+ :param resource_name: unique identifier for the schema
191+ :param schema: the schema to add, can be Schema or dict
192+ :param location: optional location of the schema in the source code
193+ """
194+ from databricks .bundles .catalog import Schema
195+
196+ schema = _transform (Schema , schema )
197+ path = ("resources" , "schemas" , resource_name )
198+ location = location or Location .from_stack_frame (depth = 1 )
199+
200+ if self ._schemas .get (resource_name ):
201+ self .add_diagnostic_error (
202+ msg = f"Duplicate resource name '{ resource_name } ' for a schema. Resource names must be unique." ,
203+ location = location ,
204+ path = path ,
205+ )
206+ else :
207+ if location :
208+ self .add_location (path , location )
209+
210+ self ._schemas [resource_name ] = schema
211+
171212 def add_location (self , path : tuple [str , ...], location : Location ) -> None :
172213 """
173214 Associate source code location with a path in the bundle configuration.
@@ -244,6 +285,9 @@ def add_resources(self, other: "Resources") -> None:
244285 for name , pipeline in other .pipelines .items ():
245286 self .add_pipeline (name , pipeline )
246287
288+ for name , schema in other .schemas .items ():
289+ self .add_schema (name , schema )
290+
247291 for path , location in other ._locations .items ():
248292 self .add_location (path , location )
249293
0 commit comments