macrosjinja2configdbt

DBT - get a ref model config variables inside a macro


I am using macros to automate and make generic DBT incremental procedures.

Problem:

I try to get some config-variables from a model i use as a ref in another model. for each of my models i keep a timestamp_column in config. I want to be able to get the timestamp_column value from config whenever i need. I didn't find any proper way to do so.

Code Example:

for example in my _models.yml:

  - name: L0__game_runs
    config:
      materialized: incremental
      time_interval: hour
      timestamp_column: time_started

  - name: L1__drafts
    config:
      materialized: incremental
      time_interval: hour
      timestamp_column: created_at

TheL1__drafts model query is referring L0__game_runs like:

with
    {% if is_incremental() %}
        {% set self_max_timestamp = get_table_max_timestamp(this) %}
         drafts as {{ (generate_source_query(ref('L0__game_runs'), self_max_timestamp)) }}
    {% else %}
          drafts as (select * from {{ ref('L0__game_runs') }}),
      {% endif %}
    
    <REST OF SQL COMES HERE...>

generate_source_query.sql macro:

{% macro generate_source_query(source_model, self_max_timestamp) %}
    {%- set source_timestamp_column = source_model.get('timestamp_column') -%}
    <REST OF MACRO COMES HERE...> 
{% endmacro %}

but source_timestamp_columnreturn empty string result. didn't find any other way to fix it. Any ideas?


Solution

  • After a lot of digging, i found a way to solve it - using the DBT graph context which get all DBT models configs (and more).

    I wrote a macro to retrieve a ref model config values. its a bit naive but it good enough for me right now:

    {% macro get_model_config_values(model_ref) %}
        {%- set table_name = model_ref.identifier -%}
    
        {% for node in graph.nodes.values() %}
            {%- set model_name = node.unique_id.split('.')[-1] -%}
            {%- if table_name == model_name -%}
                {%- set model_config = node.config -%}
                {{ return(model_config) }}
            {%- endif -%}
        {% endfor %}
    {% endmacro %}