DBT : Automate selection of different table name in development and production in DBT using macro


Macros are a powerful tool in dbt (Data Build Tool) that allow you to reuse code and make your SQL code more dynamic. One use case for macros is to select different table names based on the environment you are in (development or production).

Here’s how you can use macros to achieve this:

Define a macro in your macros folder:

{% macro table_name(environment) %}
  {% if environment == 'prod' %}
  {% elif environment == 'dev' %}
  {% else %}
  {% endif %}
{% endmacro %}

In your SQL model, reference the macro and pass in the environment as a parameter:

select *
from {{table_name(var.environment)}}

Set the environment variable in your dbt_project.yml file:

  environment: dev

This way, when you run dbt run in your development environment, the table_dev will be selected, and when you run dbt run in production, the table_prod will be selected.

By using macros in this way, you can maintain separate table names for development and production and avoid hardcoding values in your SQL code. This makes it easier to switch between environments and reduces the risk of accidentally using the wrong data.

Get more useful articles on dbt

  1. ,
Author: user

Leave a Reply