Macros that generate dbt code, and log it to the command line.
generate_source (source)
This macro generates lightweight YAML for a Source, which you can then paste into a schema file.
schema_name
(required): The schema name that contains your source datadatabase_name
(optional, default=target.database): The database that your source data is in.generate_columns
(optional, default=False): Whether you want to add the column names to your source definition.
- Use the macro (in dbt Develop, in a scratch file, or in a run operation) like so:
{{ codegen.generate_source('raw_jaffle_shop') }}
Alternatively, call the macro as an operation:
$ dbt run-operation generate_source --args 'schema_name: raw_jaffle_shop'
or
# for multiple arguments, use the dict syntax
$ dbt run-operation generate_source --args '{"schema_name": "raw_jaffle_shop", "database_name": "raw"}'
- The YAML for the source will be logged to the command line
version: 2
sources:
- name: raw_jaffle_shop
database: raw
tables:
- name: customers
- name: orders
- name: payments
- Paste the output in to a schema
.yml
file, and refactor as required.
generate_base_model (source)
This macro generates the SQL for a base model, which you can then paste into a model.
source_name
(required): The source you wish to generate base model SQL for.table_name
(required): The source table you wish to generate base model SQL for.
- Create a source for the table you wish to create a base model on top of.
- Use the macro (in dbt Develop, or in a scratch file), and compile your code
{{
codegen.generate_base_model(
source_name='raw_jaffle_shop',
table_name='customers'
)
}}
Alternatively, call the macro as an operation:
$ dbt run-operation generate_base_model --args '{"source_name": "raw_jaffle_shop", "table_name": "customers"}'
- The SQL for a base model will be logged to the command line
with source as (
select * from {{ source('raw_jaffle_shop', 'customers') }}
),
renamed as (
select
id,
first_name,
last_name,
email,
_elt_updated_at
from source
)
select * from renamed
- Paste the output in to a model, and refactor as required.