Get All DBT Table/Model names from a tag inside another model

The problem I’m having

GETTING ALL dbt TABLE/MODEL NAMES FROM A TAG INSIDE ANOTHER MODEL USING A QUERY OR MACRO

The context of why I’m trying to do this

I Need all the table names from a tag and generate dynamic alter statements for those tables

This will get you a list of the names of the models which have a specific tag

{% macro get_models_with_tag(tag) %}

{% set models_with_tag = [] %}
{% for model in graph.nodes.values() | selectattr("resource_type", "equalto", "model") %}

    {% if tag in model.config.tags %}
        {{ models_with_tag.append(model.name) }}
    {% endif %}

{% endfor %}

{{ return(models_with_tag) }}

{% endmacro %}

You can call it on your model like

...
{% set models_with_tag = get_models_with_tag('my_tag') %}
...
1 Like

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