dbt-labs/dbt-codegen
Indexed by TopGit from live GitHub metadata: dbt-labs/dbt-codegen has 677 stars, written primarily in Makefile. Macros that generate dbt code
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
Snapshot
Top contributors
Show top contributors
dbt-codegen
Macros that generate dbt code, and log it to the command line.
Contents
- dbt-codegen
- Contents
- Installation instructions
- Macros
- generate_source (source)
- Arguments
- Usage:
- generate_base_model (source)
- Arguments:
- Usage:
- create_base_models (source)
- Arguments:
- Usage:
- base_model_creation (source)
- Arguments:
- Usage:
- generate_model_yaml (source)
- Arguments:
- Usage:
- generate_model_import_ctes (source)
- Arguments:
- Usage:
- generate_unit_test_template (source)
- Arguments:
- Usage:
- generate_source (source)
- Contributing
Installation instructions
New to dbt packages? Read more about them here.
- Include this package in your
packages.ymlfile — check here for the latest version number:
packages:
- package: dbt-labs/codegen
version: X.X.X ## update to latest version here
- Run
dbt depsto install the package.
Macros
generate_source (source)
This macro generates lightweight YAML for a Source, which you can then paste into a schema file.
Arguments
schema_name(required): The schema name that contains your source datadatabase_name(optional, default=target.database): The database that your source data is in.table_names(optional, default=none): A list of tables that you want to generate the source definitions for.generate_columns(optional, default=False): Whether you want to add the column names to your source definition.include_descriptions(optional, default=False): Whether you want to add description placeholders to your source definition.include_data_types(optional, default=True): Whether you want to add data types to your source columns definitions.table_pattern(optional, default='%'): A table prefix / postfix that you want to subselect from all available tables within a given schema.exclude(optional, default=''): A string you want to exclude from the selection criterianame(optional, default=schema_name): The name of your sourceinclude_database(optional, default=False): Whether you want to add the database to your source definitioninclude_schema(optional, default=False): Whether you want to add the schema to your source definitioncase_sensitive_databases(optional, default=False): Whether you want database names to be in lowercase, or to match the case in the source table — not compatible with Redshiftcase_sensitive_schemas(optional, default=False): Whether you want schema names to be in lowercase, or to match the case in the source table — not compatible with Redshiftcase_sensitive_tables(optional, default=False): Whether you want table names to be in lowercase, or to match the case in the source table — not compatible with Redshiftcase_sensitive_cols(optional, default=False): Whether you want column names to be in lowercase, or to match the case in the source table
Outputting to a file
If you use the dbt run-operation approach it is possible to output directly to a file by piping the output to a new file and using the --quiet CLI flag:
dbt --quiet run-operation generate_source --args '{"table_names": ["orders"]}' > models/staging/jaffle_shop/_sources.yml
Usage:
- Copy the macro into a statement tab in the dbt Cloud IDE, or into an analysis file, and compile your code
{{ codegen.generate_source('raw_jaffle_shop') }}
or for multiple arguments
{{ codegen.generate_source(schema_name= 'jaffle_shop', database_name= 'raw') }}
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": "jaffle_shop", "database_name": "raw", "table_names":["table_1", "table_2"]}'
or if you want to include column names and data types:
$ dbt run-operation generate_source --args '{"schema_name": "jaffle_shop", "generate_columns": true}'
or if you want to include column names without data types (the behavior dbt-codegen <= v0.9.0):
$ dbt run-operation generate_source --args '{"schema_name": "jaffle_shop", "generate_columns": true, "include_data_types": false}'
- The YAML for the source will be logged to the command line
version: 2
sources:
- name: raw_jaffle_shop
database: raw
schema: raw_jaffle_shop
tables:
- name: customers
description: ""
- name: orders
description: ""
- name: payments
description: ""
- Paste the output in to a schema
.ymlfile, 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.
Arguments:
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.leading_commas(optional, default=False): Whether you want your commas to be leading (vs trailing).case_sensitive_cols(optional, default=False): Whether your source table has case sensitive column names. If true, keeps the case of the column names from the source.materialized(optional, default=None): Set materialization style (e.g. table, view, incremental) inside of the model'sconfigblock. If not set, materialization style will be controlled bydbt_project.yml
Usage:
- Create a source for the table you wish to create a base model on top of.
- Copy the macro into a statement tab in the dbt Cloud IDE, or into an analysis file, and compile your code
{{ codegen.generate_base_model(
source_name='raw_jaffle_shop',
table_name='customers',
materialized='table'
) }}
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.
create_base_models (source)
This macro generates a series of terminal commands (appended with the && to allow for subsequent execution) that execute the base_model_creation bash script. This bash script will write the output of the generate_base_model macro into a new model file in your local dbt project.
Note: This macro is not compatible with the dbt Cloud IDE.
Arguments:
source_name(required): The source you wish to generate base model SQL for.tables(required): A list of all tables you want to generate the base models for.
Usage:
- Create a source for the table you wish to create a base model on top of.
- Copy the macro into a statement tab into your local IDE, and run your code
dbt run-operation codegen.create_base_models --args '{source_name: my-source, tables: ["this-table","that-table"]}'
base_model_creation (source)
This bash script when executed from your local IDE will create model files in your dbt project instance that contain the outputs of the generate_base_model macro.
Note: This macro is not compatible with the dbt Cloud IDE.
Arguments:
source_name(required): The source you wish to generate base model SQL for.table_name(required): A single table name for which you want to generate the base model.
Usage:
- Create a source for the table you wish to create a base model on top of.
- Copy the macro into a statement tab into your local IDE, and run your code
source dbt_packages/codegen/bash_scripts/base_model_creation.sh "source_name" "table_name"
generate_model_yaml (source)
This macro generates the YAML for a list of model(s), which you can then paste into a schema.yml file.
Arguments:
model_names(required): The model(s) you wish to generate YAML for.upstream_descriptions(optional, default=False): Whether you want to include descriptions for identical column names from upstream models and sources.include_data_types(optional, default=True): Whether you want to add data types to your model column definitions.
Usage:
- Create a model.
- Copy the macro into a statement tab in the dbt Cloud IDE, or into an analysis file, and compile your code
{{ codegen.generate_model_yaml(
model_names=['customers']
) }}
You can use the helper function codegen.get_models and specify a directory and/or prefix to get a list of all matching models, to be passed into model_names list.
{% set models_to_generate = codegen.get_models(directory='marts', prefix='fct_') %}
{{ codegen.generate_model_yaml(
model_names = models_to_generate
) }}
Alternatively, call the macro as an operation:
$ dbt run-operation generate_model_yaml --args '{"model_names": ["customers"]}'
- The YAML for a base model(s) will be logged to the command line
version: 2
models:
- name: customers
description: ""
columns:
- name: customer_id
data_type: integer
description: ""
- name: customer_name
data_type: text
description: ""
- Paste the output in to a schema.yml file, and refactor as required.
generate_model_import_ctes (source)
This macro generates the SQL for a given model with all references pulled up into import CTEs, which you can then paste back into the model.
Arguments:
model_name(required): The model you wish to generate SQL with import CTEs for.leading_commas(optional, default=False): Whether you want your commas to be leading (vs trailing).
Usage:
- Create a model with your original SQL query
- Copy the macro into a statement tab in the dbt Cloud IDE, or into an analysis file, and compile your code
{{ codegen.generate_model_import_ctes(
model_name = 'my_dbt_model'
) }}
Alternatively, call the macro as an operation:
$ dbt run-operation generate_model_import_ctes --args '{"model_name": "my_dbt_model"}'
- The new SQL - with all references pulled up into import CTEs - will be logged to the command line
with customers as (
select * from {{ ref('stg_customers') }}
),
orders as (
select * from {{ ref('stg_orders') }}
),
payments as (
select * from {{ ref('stg_payments') }}
),
customer_orders as (
select
customer_id,
min(order_date) as first_order,
max(order_date) as most_recent_order,
count(order_id) as number_of_orders
from orders
group by customer_id
),
customer_payments as (
select
orders.customer_id,
sum(amount) as total_amount
from payments
left join orders on
payments.order_id = orders.order_id
group by orders.customer_id
),
final as (
select
customers.customer_id,
customers.first_name,
customers.last_name,
customer_orders.first_order,
customer_orders.most_recent_order,
customer_orders.number_of_orders,
customer_payments.total_amount as customer_lifetime_value
from customers
left join customer_orders
on customers.customer_id = customer_orders.customer_id
left join customer_payments
on customers.customer_id = customer_payments.customer_id
)
select * from final
- Replace the contents of the model's current SQL file with the compiled or logged code
generate_unit_test_template (source)
This macro generates the unit testing YAML for a given model with all references included as given inputs (along with their columns), plus the columns within the expected output.
Arguments:
model_name(required): The model you wish to generate unit testing YAML for.inline_columns(optional, default=False): Whether you want all columns on the same line.
Usage:
- Create a model with your original SQL query
- Call the macro as an operation:
$ dbt run-operation generate_unit_test_template --args '{"model_name": "order_items", "inline_columns": true}'
- The new YAML - with all given inputs included - will be logged to the command line
unit_tests:
- name: unit_test_order_items
model: order_items
given:
- input: ref("stg_order_items")
rows:
- col_a:
col_b:
expect:
rows:
- id:
- Create a new YAML file with the compiled or logged code.
- Add column values for the given inputs and expected output.
Contributing
To contirbute code to this package, please follow the steps outlined in the integration_tests directory's README file.
Related repositories
Master programming by recreating your favorite technologies from scratch.
A curated meta-list of curated lists organized by technology domain. The repository acts as a directory pointing to hundreds of specialized awesome lists covering programming languages, platforms, frameworks, and tooling. All content is community-contributed under the CC0 public domain dedication.
Public APIs is a community-curated GitHub repository listing free, publicly accessible APIs across a wide range of categories, with auth type, HTTPS, and CORS noted for each entry. It's a browsable reference, not a library to install.
REA is an MCP server and CLI that gives AI coding agents the ability to reverse engineer binaries, JavaScript apps, .NET assemblies, and websites. It hooks into Hopper, Ghidra, or IDA Pro for native binary work, or runs static JavaScript analysis with just Node.js. Every investigation returns an Evidence bundle that includes what was found, what was not, and the reasoning path. Setup registers REA with supported agents and walks you through connecting an analysis engine. The CLI also works standalone for terminal-first workflows.
Quick answers
Does dbt-labs/dbt-codegen have any tags?
TopGit's last sync did not record any GitHub topics for dbt-labs/dbt-codegen. GitHub topics appear in the right sidebar of a repository page; that's the authoritative place to check.
How active is development on dbt-labs/dbt-codegen?
The most recent commit recorded on dbt-labs/dbt-codegen was 1 month ago, based on the GitHub push timestamp. The repository has 134 forks — one of the better signals of community interest.
How many stars does dbt-labs/dbt-codegen have?
dbt-labs/dbt-codegen has 677 GitHub stars — refresh the page for the live number, or check github.com/dbt-labs/dbt-codegen. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
What is dbt-labs/dbt-codegen?
dbt-labs/dbt-codegen (dbt-labs/dbt-codegen) is a Makefile project on GitHub. From the project's own README: Macros that generate dbt code
What language is dbt-labs/dbt-codegen written in?
dbt-labs/dbt-codegen is written primarily in Makefile. GitHub's language field is based on the largest share of bytes in the default branch.
Read full README in the tab above.
Is dbt-codegen worth your time?
ChatGPT, Claude and Perplexity can all read this page. Ask one of them what it makes of dbt-codegen.