Is there a way to select models that depend on specific macro?

The problem I’m having

I changed a macro and want to update (run) all models that depend on the macro. Is there a way to do so?

What I’ve already tried

Usually, I check the models that depend on the macro in the code and select it manually

I checked Syntax overview | dbt Developer Hub and dbt-core issues for “is:issue is:open select macro” and didn’t find any relevant information.

1 Like

Hi @artem.popov I don’t think you can select them this way. But I created this macro that can help you

{% macro get_models_based_on_macro(macro) %}

    {% if execute %}

        {% set models_list = [] %}

        {% for node in graph.nodes.values() if "macro."~macro in node.depends_on.macros %}

            {{ models_list.append(node.name) }}

        {% endfor %}

            {{ log(models_list|join(' '), info=True) }}

    {% endif %}

{% endmacro %}

Then you can call this macro in your CLI (DON’T FORGET TO PASS THE NAME OF THE PACKAGE, if it is your root project, it is the name of your project)

dbt run-operation get_models_based_on_macro --args '{macro: my_package.my_macro}'

THe it will print in the terminal a list of the models that depend on the macro like

18:10:28  model_1 model_2

Then just copy and paste it in your dbt run

Thanks for a workaround! Unfortunately, it doesn’t include macro to macro to model dependencies, only direct usage in models.

I will post a feature request in dbt-core github, as the workaround is still not easy to use on daily basis

Looks like state-based selection is fits my initial goal of selecting models affected by code change

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.