Building into custom schemas or databases

Custom schemas and databases

By default, all assets are built in the schema/dataset specified in the Branch environment. Each branch can be configured to write to its own schema and dataset.

Sometimes, it might be useful to build some assets in a different schema or database than the one specified in the Branch environment menu.

For example, you may want to build all your staging assets in a staging schema, and all your mart assets in a separate schema, while everything else is built in the default schema from the Branch environment menu.

This approach of using custom schemas and databases facilitates the segregation of assets into various schemas and databases. The naming convention for a custom schema combines the branch's schema with the custom schema name: <branch_schema>_<custom_schema>.

For instance, if you chose the branch assets to be published in the schema/dataset 'y42_main', and you want to build all staging assets in a staging schema named staging, the resulting schema name will be y42_main_staging.

Configuring custom schemas

To assign a unique schema to an asset, utilize the schema configuration key. This can be done in two ways:

  • Assign to an individual model via a configuration block within that model.
  • Set for a group of assets in a specific subdirectory through the dbt_project.yml file.
models/staging/my_model.sql

_10
{{ config(schema='staging_schema') }}
_10
select ...

dbt_project.yml

_10
models:
_10
y42_project:
_10
staging:
_10
+materialized: view
_10
+schema: staging # Assets in `models/staging/ will be built in the "<branch_name>_staging" schema.
_10
mart:
_10
+schema: mart # Assets in `models/mart/ will be built in the "<branch_name>_mart" schema.
_10
# All other assets will be built in the "<branch_name>" schema.

Configuring custom databases

To define custom databases for certain assets, specify these in the dbt_project.yml file or within the configuration block of an asset.

models/staging/my_model.sql

_10
{{ config(database="staging_db") }}
_10
select * from ...

dbt_project.yml

_10
models:
_10
y42_project:
_10
+database: staging_db