### Start Postgres container with make setup-db Source: https://github.com/dbt-labs/dbt-codegen/blob/main/integration_tests/README.md Starts the Postgres container defined in docker-compose.yml using the Makefile wrapper. ```shell make setup-db ``` -------------------------------- ### Start Postgres container with docker-compose up Source: https://github.com/dbt-labs/dbt-codegen/blob/main/integration_tests/README.md Alternative to make setup-db that directly runs docker-compose to start the Postgres container in detached mode. ```shell docker-compose up --detach postgres ``` -------------------------------- ### Install dbt-core and Postgres adapter Source: https://github.com/dbt-labs/dbt-codegen/blob/main/integration_tests/README.md Installs dbt-core and the Postgres adapter specifically, using either the Makefile wrapper or pip directly. ```shell make dev target=postgres # or python3 -m pip install dbt-core dbt-postgres ``` -------------------------------- ### Install dbt-core and adapter (generic) Source: https://github.com/dbt-labs/dbt-codegen/blob/main/integration_tests/README.md Installs dbt-core and the adapter for the chosen target warehouse using either the Makefile wrapper or pip directly. ```shell make dev target=[postgres|redshift|...] # or python3 -m pip install dbt-core dbt-[postgres|redshift|...] ``` -------------------------------- ### dbt run-operation --args syntax examples Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/configuration.md Two syntaxes for passing arguments to dbt run-operation: inline dict and key-value. Keys must match macro parameter names exactly. Requires dbt deps first so package macros and dbt_utils are installed. ```bash dbt run-operation generate_source --args '{"schema_name": "jaffle_shop", "database_name": "raw", "table_names": ["table_1", "table_2"]}' ``` ```bash dbt run-operation create_base_models --args '{source_name: my-source, tables: ["this-table","that-table"]}' ``` -------------------------------- ### Example YAML output from generate_source Source: https://github.com/dbt-labs/dbt-codegen/blob/main/README.md Example YAML output logged to the command line by generate_source. Paste this output into a schema .yml file and refactor as required. ```yaml version: 2 sources: - name: raw_jaffle_shop database: raw schema: raw_jaffle_shop tables: - name: customers description: "" - name: orders description: "" - name: payments description: "" ``` -------------------------------- ### Example YAML output for a base model Source: https://github.com/dbt-labs/dbt-codegen/blob/main/README.md Example output logged to the command line after running the macro. Paste this YAML into a schema.yml file and refactor as required. ```yaml version: 2 models: - name: customers description: "" columns: - name: customer_id data_type: integer description: "" - name: customer_name data_type: text description: "" ``` -------------------------------- ### Upgrade pip and setuptools Source: https://github.com/dbt-labs/dbt-codegen/blob/main/integration_tests/README.md Upgrades pip and setuptools before installing dbt dependencies. ```shell python3 -m pip install --upgrade pip setuptools ``` -------------------------------- ### Log output from create_base_models Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/api-reference/create_base_models.md Example log output from the create_base_models operation. The commands are joined with ' && \n' so that a failure stops execution. ```text Run these commands in your shell to generate the models: source dbt_packages/codegen/bash_scripts/base_model_creation.sh "my-source" "this-table" && source dbt_packages/codegen/bash_scripts/base_model_creation.sh "my-source" "that-table" ``` -------------------------------- ### generate_source Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/api-reference/generate_source.md Documentation for the generate_source macro, including parameters, usage examples, and error handling. ```APIDOC ## generate_source ### Description Generates YAML source definitions for a given schema, optionally including columns, data types, and descriptions. It performs live schema introspection and can be called inline in Jinja or as a dbt run-operation. ### Method Macro / Operation ### Endpoint `codegen.generate_source` ### Parameters #### schema_name (string) - Required - The name of the schema to generate sources for. #### database_name (string) - Optional - The name of the database containing the schema. #### table_names (list of strings) - Optional - A list of table names to filter the generated sources. #### generate_columns (boolean) - Optional - Whether to include column definitions in the generated YAML. #### include_data_types (boolean) - Optional - Whether to include data types for columns. #### include_descriptions (boolean) - Optional - Whether to include descriptions for tables and columns. #### include_schema (boolean) - Optional - Whether to include the schema name in the source definition. #### case_sensitive_schemas (boolean) - Optional - Whether to treat schema names as case-sensitive. #### case_sensitive_tables (boolean) - Optional - Whether to treat table names as case-sensitive. #### case_sensitive_cols (boolean) - Optional - Whether to treat column names as case-sensitive. ### Request Example ```sql {{ codegen.generate_source('raw_jaffle_shop') }} ``` ### Response #### Success Response Returns a YAML string representing the source definition. #### Response Example ```yaml version: 2 sources: - name: raw_jaffle_shop tables: - name: customers - name: orders - name: payments ``` ### Error Handling If the specified schema does not exist or is unreachable, the macro raises a dbt-utils/relation exception. There is no graceful fallback; validate the schema before calling or use `codegen.get_tables_in_schema` to check reachability. ``` -------------------------------- ### Run integration tests for a specific target (postgres example) Source: https://github.com/dbt-labs/dbt-codegen/blob/main/integration_tests/README.md Runs the integration tests for a specific database target, shown here with postgres. Use either the make target or the shell script. ```shell make test target=postgres # or ./run_test.sh postgres ``` -------------------------------- ### get_resource_from_unique_id usage example Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/api-reference/helpers.md Example of calling the macro with a model unique ID to retrieve the node dictionary and access its name property. ```sql {% set node = codegen.get_resource_from_unique_id('model.orders') %} {{ node.name }} ``` -------------------------------- ### Reload virtual environment Source: https://github.com/dbt-labs/dbt-codegen/blob/main/integration_tests/README.md Reloads the virtual environment after installing dependencies. ```shell source .venv/bin/activate ``` -------------------------------- ### base_model_creation.sh script Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/api-reference/create_base_models.md Full source of the base_model_creation.sh script. It truncates the output file, runs generate_base_model, and appends the SQL starting at line 3 to strip CLI output. ```bash #!/bin/bash echo "" > models/stg_"$1"__"$2".sql dbt --quiet run-operation codegen.generate_base_model --args '{"source_name": "'"$1"'", "table_name": "'"$2"'"}' | tail -n +3 >> models/stg_"$1"__"$2".sql ``` -------------------------------- ### generate_base_model Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/api-reference/generate_base_model.md Documentation for the generate_base_model macro, including parameters, usage examples, and error handling. ```APIDOC ## generate_base_model ### Description Generates a base model SQL file for a given source table, producing a `with source as` and `renamed as` CTE structure. The macro logs the generated SQL to the console. ### Method Macro (dbt Jinja) ### Parameters - **source_name** (string) - Required - The name of the source. - **table_name** (string) - Required - The name of the table within the source. - **materialized** (string) - Optional - The materialization config for the generated model (e.g., 'table'). - **leading_commas** (boolean) - Optional - If true, places commas at the beginning of each column line. - **case_sensitive_cols** (boolean) - Optional - If true, preserves column case sensitivity. ### Usage Examples #### Basic call, inline Jinja ```sql {{ codegen.generate_base_model( source_name='raw_jaffle_shop', table_name='customers' ) }} ``` Logs: ```sql 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 ``` #### With materialization config and leading commas ```sql {{ codegen.generate_base_model( source_name='raw_jaffle_shop', table_name='customers', materialized='table', leading_commas=True ) }} ``` Logs: ```sql {{ config(materialized='table') }} with source as ( select * from {{ source('raw_jaffle_shop', 'customers') }} ), renamed as ( select id , first_name , last_name from source ) select * from renamed ``` #### As an operation ```bash dbt run-operation generate_base_model --args '{"source_name": "raw_jaffle_shop", "table_name": "customers", "case_sensitive_cols": true}' ``` ### Error Handling Calling the macro for a source that does not exist aborts compilation with a dbt "source not found" error — there is no fallback path inside the macro. Guard the call with a fresh parse check if you want to fail early with your own message: ```sql {% if source('raw_jaffle_shop', 'customers') is not none %} {{ codegen.generate_base_model(source_name='raw_jaffle_shop', table_name='customers') }} {% else %} {{ exceptions.raise_compiler_error("Source raw_jaffle_shop.customers is not defined") }} {% endif %} ``` (`source()` errors on unknown sources rather than returning `none` in strict dbt versions; adapt the guard to your dbt version's behavior.) ``` -------------------------------- ### generate_model_yaml Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/api-reference/generate_model_yaml.md Documentation for the generate_model_yaml macro, including parameters, usage examples, output format, and error handling. ```APIDOC ## generate_model_yaml ### Description Generates a YAML schema file for the specified dbt models, including column names and data types. Optionally includes upstream column descriptions and can exclude data types. ### Method Macro (Jinja) / dbt run-operation ### Parameters - **model_names** (list) - Required - List of model names to generate YAML for. - **upstream_descriptions** (boolean) - Optional - When true, includes descriptions from upstream columns. Default is false. - **include_data_types** (boolean) - Optional - When false, excludes data types from the generated YAML. Default is true. ### Usage Examples #### Basic call ```sql {{ codegen.generate_model_yaml( model_names=['customers'] ) }} ``` #### Feeding a discovered model list (README pattern) ```sql {% set models_to_generate = codegen.get_models(directory='marts', prefix='fct_') %} {{ codegen.generate_model_yaml( model_names = models_to_generate ) }} ``` #### Upstream descriptions (non-trivial option) ```bash dbt run-operation generate_model_yaml --args '{"model_names": ["child_model"], "upstream_descriptions": true, "include_data_types": false}' ``` ### Output Example ```yaml version: 2 models: - name: customers description: "" columns: - name: customer_id data_type: integer description: "" - name: customer_name data_type: text description: "" ``` ### Error Handling Passing a string instead of a list aborts compilation with the error: "The `model_names` argument must always be a list, even if there is only one model." Guard in consumer code: ```sql {% set names = ['customers'] %} {% if names is string %} {{ exceptions.raise_compiler_error("Pass model_names as a list") }} {% else %} {{ codegen.generate_model_yaml(model_names=names) }} {% endif %} ``` ``` -------------------------------- ### get_models macro signature and usage Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/api-reference/helpers.md The macro signature shows the two optional parameters. The usage example filters models in the 'marts' directory with a 'fct_' prefix and passes the result to generate_model_yaml. The macro returns model names from graph.nodes where resource_type == 'model', filtered by directory and prefix; directory comparison drops the filename using the OS-specific separator. ```sql {% macro get_models(directory=None, prefix=None) %} ``` ```sql {% set fct_models = codegen.get_models(directory='marts', prefix='fct_') %} {{ codegen.generate_model_yaml(model_names=fct_models) }} ``` -------------------------------- ### Generated base model SQL output Source: https://github.com/dbt-labs/dbt-codegen/blob/main/README.md Example output of the generate_base_model macro for the raw_jaffle_shop.customers source. The SQL is logged to the command line and can be pasted into a model and refactored as needed. ```sql 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 ``` -------------------------------- ### Emit a formatted column in a consumer project Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/api-reference/data_type_formatting.md Use in a consumer project to emit a formatted column. It creates a relation, gets the first column, and calls codegen.format_column to produce the formatted output. ```sql {% set relation = api.Relation.create(database=target.database, schema='raw', identifier='customers') %} {% set column = adapter.get_columns_in_relation(relation) | first %} {{ codegen.format_column(column).formatted }} ``` -------------------------------- ### Generated SQL with import CTEs Source: https://github.com/dbt-labs/dbt-codegen/blob/main/README.md Example output of the generate_model_import_ctes macro: the original model SQL with all references pulled up into import CTEs. This logged SQL should replace the contents of the model's current SQL file. ```sql 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 ``` -------------------------------- ### Run entire project and all tests with make dev and make test Source: https://github.com/dbt-labs/dbt-codegen/blob/main/integration_tests/README.md Runs the entire project and all tests using make dev and make test. Replace {your_target} with your database target. ```shell make dev target={your_target} make test target={your_target} ``` -------------------------------- ### Run all integration tests like CI (make or run_test.sh) Source: https://github.com/dbt-labs/dbt-codegen/blob/main/integration_tests/README.md Runs the full integration test suite locally, mimicking Circle CI. Replace [postgres|redshift|...] with your target database. Use either the make target or the shell script. ```shell make test target=[postgres|redshift|...] # or ./run_test.sh [postgres|redshift|...] ``` -------------------------------- ### Create and activate Python virtual environment Source: https://github.com/dbt-labs/dbt-codegen/blob/main/integration_tests/README.md Creates and activates a Python virtual environment in the repository root. ```shell python3 -m venv .venv source .venv/bin/activate ``` -------------------------------- ### Call get_tables_in_schema with table_pattern Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/api-reference/helpers.md Example usage of `codegen.get_tables_in_schema` to fetch tables matching the pattern `orders%` in schema `raw_jaffle_shop` and print the resulting list. ```sql {% set tables = codegen.get_tables_in_schema('raw_jaffle_shop', table_pattern='orders%') %} {{ tables }} ``` -------------------------------- ### create_base_models macro wrapper Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/api-reference/create_base_models.md Public wrapper macro that dispatches to the adapter-specific implementation. It is the entry point for the create_base_models operation. ```sql {% macro create_base_models(source_name, tables) %} {{ return(adapter.dispatch('create_base_models', 'codegen')(source_name, tables)) }} {% endmacro %} ``` -------------------------------- ### build_dict_column_descriptions macro implementation Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/api-reference/helpers.md Builds a global dictionary of column descriptions by looping through all direct parent models of the given model. It calls `add_model_column_descriptions_to_dict` for each dependency, deriving the resource type from the first part of the full model name and the node name from the last part. Returns nothing when `execute` is `False`. ```sql {# build a global dictionary looping through all the direct parents models #} {# if the same column name exists with different descriptions it is overwritten at each loop #} {% macro build_dict_column_descriptions(model_name) %} {% if execute %} {% set glob_dict = {} %} {% for full_model in codegen.get_model_dependencies(model_name) %} {% do codegen.add_model_column_descriptions_to_dict( full_model.split('.')[0], full_model.split('.')[-1], glob_dict ) %} {% endfor %} {{ return(glob_dict) }} {% endif %} {% endmacro %} ``` -------------------------------- ### Error handling: passing a string instead of a list Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/api-reference/generate_model_yaml.md Passing a string instead of a list to model_names aborts compilation with the shown error. This example demonstrates the failure mode. ```sql -- fails: dbt compilation error {{ codegen.generate_model_yaml(model_names='customers') }} ``` ```text Compilation Error ... The `model_names` argument must always be a list, even if there is only one model. ``` -------------------------------- ### Run create_base_models macro Source: https://github.com/dbt-labs/dbt-codegen/blob/main/README.md Run the create_base_models macro to generate terminal commands that execute the base_model_creation bash script for each specified table. Requires a source to be created first and is not compatible with the dbt Cloud IDE. ```sql dbt run-operation codegen.create_base_models --args '{source_name: my-source, tables: ["this-table","that-table"]}' ``` -------------------------------- ### Run tox supported tests (make test_tox) Source: https://github.com/dbt-labs/dbt-codegen/blob/main/integration_tests/README.md Runs the integration tests using tox, as done in GitHub workflows. Replace target with your database target. ```shell make test_tox target=postgres ``` -------------------------------- ### default__create_base_models macro Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/api-reference/create_base_models.md Dispatch implementation for the default adapter. The body is not shown in the source; it generates shell commands that invoke the base_model_creation.sh script. ```sql {% macro default__create_base_models(source_name, tables) %} ``` -------------------------------- ### Run generate_source with dict syntax for multiple arguments Source: https://github.com/dbt-labs/dbt-codegen/blob/main/README.md Run the macro as a dbt operation using the dict syntax for multiple arguments, including a list of table_names. ```bash # 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"]}' ``` -------------------------------- ### Model already using CTEs (re-joined body): generate_model_import_ctes with leading_commas=True Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/api-reference/generate_model_import_ctes.md Use when the model already starts with a WITH clause. The import CTE chain is prepended and the original WITH is re-joined with a leading comma (\g<1>,). ```sql {{ codegen.generate_model_import_ctes(model_name='my_dbt_model', leading_commas=True) }} ``` -------------------------------- ### generate_unit_test_template Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/api-reference/generate_unit_test_template.md Documentation for the generate_unit_test_template macro, including its signature, parameters, return value, and output structure. ```APIDOC ## `generate_unit_test_template` ### Description Generates a dbt unit-test YAML skeleton for a given model. Every direct upstream dependency becomes a `given` input with its columns as empty placeholder rows, and the model's own columns become the `expect.rows` columns. Logs the YAML to the command line; the developer pastes it into a YAML file and fills in row values. ### Method Macro (Jinja) ### Signature ```jinja {% macro generate_unit_test_template(model_name, inline_columns=false) %} ``` ### Parameters - **model_name** (string) - Required - The model to generate the unit test template for. - **inline_columns** (boolean) - Optional, default `false` - When `True`, render each column placeholder as a single inline mapping (`- {col_a: , col_b: }`); when `False`, one line per column. ### Return Value `string` - The unit-test YAML (no `version:` header - output begins directly with `unit_tests:`) or `None` when `execute` is `False`. When `execute` is `True` the string is both printed and returned. ### Example ```jinja {{ codegen.generate_unit_test_template('my_model') }} ``` ### Output Structure ```yaml unit_tests: - name: unit_test_ model: overrides: macros: is_incremental: true # iff config['materialized'] == 'incremental' given: [] # iff no dependencies AND not incremental - input: source("", "") # for source dependencies - input: ref("") # for model dependencies rows: - : : - input: this # iff incremental (and relation exists) rows: - : expect: rows: - : : # iff model relation exists ``` ``` -------------------------------- ### Add dbt-codegen to packages.yml Source: https://github.com/dbt-labs/dbt-codegen/blob/main/README.md Add the dbt-codegen package to your dbt project's packages.yml file. Replace X.X.X with the latest version from the dbt Hub. After adding, run dbt deps to install the package. ```yaml packages: - package: dbt-labs/codegen version: X.X.X ## update to latest version here ``` -------------------------------- ### Run generate_source as a dbt operation with YAML args Source: https://github.com/dbt-labs/dbt-codegen/blob/main/README.md Run the macro as a dbt operation using the YAML argument syntax for a single schema_name argument. ```bash $ dbt run-operation generate_source --args 'schema_name: raw_jaffle_shop' ``` -------------------------------- ### get_resource_from_unique_id macro definition Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/api-reference/helpers.md Retrieves the resource dictionary for a given unique ID. Routes IDs starting with 'source.' to graph.sources, 'exposure.' to graph.exposure, 'metric.' to graph.metrics, and everything else to graph.nodes. Unknown IDs raise a KeyError-style undefined error. Used by generate_unit_test_template to resolve dependencies. ```sql {# retrieve entire resource dictionary based on unique id #} {% macro get_resource_from_unique_id(resource_unique_id) %} {% set resource_type = resource_unique_id.split('.')[0] %} {% if resource_type == 'source' %} {% set resource = graph.sources[resource_unique_id] %} {% elif resource_type == 'exposure' %} {% set resource = graph.exposure[resource_unique_id] %} {% elif resource_type == 'metric' %} {% set resource = graph.metrics[resource_unique_id] %} {% else %} {% set resource = graph.nodes[resource_unique_id] %} {% endif %} {{ return(resource) }} {% endmacro %} ``` -------------------------------- ### generate_model_yaml public wrapper macro Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/api-reference/generate_model_yaml.md Public wrapper macro that dispatches to the adapter-specific implementation. It accepts model_names, upstream_descriptions, and include_data_types parameters. Note the '-' whitespace control on the Jinja delimiters. ```sql {% macro generate_model_yaml(model_names=[], upstream_descriptions=False, include_data_types=True) -%} {{ return(adapter.dispatch('generate_model_yaml', 'codegen')(model_names, upstream_descriptions, include_data_types)) }} {%- endmacro %} ``` -------------------------------- ### Call generate_model_yaml with a list of model names Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/errors.md Use this snippet to call codegen.generate_model_yaml with a list of model names. The model_names argument must always be a list, even for a single model; passing a string raises a compilation error. This example shows the required list syntax and the correct invocation. ```sql {% set names = ['customers'] %} {# always a list #} {{ codegen.generate_model_yaml(model_names=names) }} ``` -------------------------------- ### Error handling with guard Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/api-reference/generate_base_model.md Shows how to guard the macro call with a fresh parse check to fail early with a custom error message if the source does not exist. Note that source() may error on unknown sources in strict dbt versions. ```sql {% if source('raw_jaffle_shop', 'customers') is not none %} {{ codegen.generate_base_model(source_name='raw_jaffle_shop', table_name='customers') }} {% else %} {{ exceptions.raise_compiler_error("Source raw_jaffle_shop.customers is not defined") }} {% endif %} ``` -------------------------------- ### create_base_models (macro) Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/api-reference/create_base_models.md Generates a series of terminal commands that invoke the base_model_creation.sh script for each table in the provided list. The commands are logged to stdout and can be executed in a local shell. ```APIDOC ## create_base_models (macro) ### Description Generates a series of terminal commands, joined with ` && \n`, that invoke the `base_model_creation.sh` bash script for each table in the provided list. The commands are logged to stdout and can be executed in a local shell to generate base model files. ### Method Macro (dbt run-operation) ### Endpoint `codegen.create_base_models` ### Parameters #### Required Parameters - **source_name** (string) - Required - The source to generate base models for; coerced to a string and embedded in each shell command. - **tables** (list) - Required - List of table names; one shell command is produced per table. ### Return Value None - the macro does not return or print generated content. It logs to stdout via `log(..., info=True)`. ### Request Example ```bash dbt run-operation codegen.create_base_models --args '{source_name: my-source, tables: ["this-table","that-table"]}' ``` ### Response #### Success Response (log output) ``` Run these commands in your shell to generate the models: source dbt_packages/codegen/bash_scripts/base_model_creation.sh "my-source" "this-table" && source dbt_packages/codegen/bash_scripts/base_model_creation.sh "my-source" "that-table" ``` ### Notes - The generated commands are meant to be pasted into a local shell. - Not compatible with the dbt Cloud IDE. - The macro does not validate that source_name or table_name exist; errors surface from `dbt run-operation`. ``` -------------------------------- ### Run generate_base_model as a dbt operation Source: https://github.com/dbt-labs/dbt-codegen/blob/main/README.md Run the generate_base_model macro as a dbt operation from the command line. Pass the source_name and table_name arguments as a JSON string. ```bash $ dbt run-operation generate_base_model --args '{"source_name": "raw_jaffle_shop", "table_name": "customers"}' ``` -------------------------------- ### Basic call, inline Jinja Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/api-reference/generate_source.md Calls generate_source with a single schema name argument. The output is a YAML source definition with the schema name and a list of tables. ```sql {{ codegen.generate_source('raw_jaffle_shop') }} ``` ```yaml version: 2 sources: - name: raw_jaffle_shop tables: - name: customers - name: orders - name: payments ``` -------------------------------- ### Basic call to generate_model_yaml Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/api-reference/generate_model_yaml.md Basic call to generate_model_yaml with a list of model names. The output YAML includes model and column metadata with empty descriptions and data types. ```sql {{ codegen.generate_model_yaml( model_names=['customers'] ) }} ``` ```yaml version: 2 models: - name: customers description: "" columns: - name: customer_id data_type: integer description: "" - name: customer_name data_type: text description: "" ``` -------------------------------- ### Basic call, inline Jinja Source: https://github.com/dbt-labs/dbt-codegen/blob/main/_autodocs/api-reference/generate_base_model.md Calls generate_base_model with source_name and table_name to generate a base model SQL. The output is shown in the logs. ```sql {{ codegen.generate_base_model( source_name='raw_jaffle_shop', table_name='customers' ) }} ``` ```sql 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 ```