getting metadata at runtime in macro

Hello ppl -
I have this case

model1 
{{ config(materialized="table", schema="clean") }}

model2
{{ config(materialized="table", schema="stg") }}

then

{%- macro round_percentage(col) -%}
    ROUND( {{ col }},3)
{%- endmacro -%}

use by model1 or 2 etc
can I make round_percentage able to recognise in which schema is running and and behave in accordance

{%- macro round_percentage(col) -%}
if schema = clean
    ROUND( {{ col }},3)
if schema = stg
    ROUND( {{ col }},6)
{%- endmacro -%}

I would like to do without passing extra paramas to macro call ofc

Off the top of my head, target.schema will probably do this? Otherwise {{ model.config.schema }} (see model | dbt Developer Hub and graph | dbt Developer Hub).

If you use the model variable, take careful note of the warnings on the graph documentation - the data inside of it is unreliable during the initial parse (when execute=false)

1 Like