Skip to content

Instantly share code, notes, and snippets.

@aleenprd
Last active January 13, 2026 12:37
Show Gist options
  • Select an option

  • Save aleenprd/5adca04f6aae219e8160ca98bf564a48 to your computer and use it in GitHub Desktop.

Select an option

Save aleenprd/5adca04f6aae219e8160ca98bf564a48 to your computer and use it in GitHub Desktop.
find_leaf_nodes
```
{% macro find_leaf_nodes(
resource_types=['model', 'snapshot', 'source', 'seed'],
starts_with=[],
regex_rules=[],
exclude_paths=['undesired_path/path/path']
) %}
{# 1. Validation and Setup #}
{% if not execute %}
{{ return([]) }}
{% endif %}
{% set re = modules.re %}
{% set leaf_nodes = [] %}
{% set referenced_ids = [] %}
{# 2. Build a set of all IDs that are "Parents" (referenced by others) #}
{% for node in graph.nodes.values() | selectattr("resource_type", "in", ["model", "snapshot", "seed"]) %}
{% if node.depends_on and node.depends_on.nodes %}
{% for parent_id in node.depends_on.nodes %}
{% do referenced_ids.append(parent_id) %}
{% endfor %}
{% endif %}
{% endfor %}
{# 3. Collect Candidate Resources #}
{% set candidates = [] %}
{% for node in graph.nodes.values() %}
{% if node.resource_type in resource_types %}
{% do candidates.append(node) %}
{% endif %}
{% endfor %}
{% if 'source' in resource_types %}
{% for source in graph.sources.values() %}
{% do candidates.append(source) %}
{% endfor %}
{% endif %}
{# 4. Filter and Identify Leaves #}
{% for node in candidates %}
{% set is_leaf = node.unique_id not in referenced_ids %}
{# Create a namespace to handle variable scope inside loops #}
{% set ns = namespace(match_prefix=true, match_regex=true, path_excluded=false) %}
{# -- Filter: Exclude Paths -- #}
{% if exclude_paths | length > 0 and node.original_file_path %}
{% for path_prefix in exclude_paths %}
{% if node.original_file_path.startswith(path_prefix) %}
{% set ns.path_excluded = true %}
{% endif %}
{% endfor %}
{% endif %}
{# -- Filter: Starts With (OR logic) -- #}
{% if starts_with | length > 0 %}
{% set ns.match_prefix = false %}
{% for prefix in starts_with %}
{% if node.name.startswith(prefix) %}
{% set ns.match_prefix = true %}
{% endif %}
{% endfor %}
{% endif %}
{# -- Filter: Regex Rules (OR logic) -- #}
{% if regex_rules | length > 0 %}
{% set ns.match_regex = false %}
{% for pattern in regex_rules %}
{% if re.match(pattern, node.name) %}
{% set ns.match_regex = true %}
{% endif %}
{% endfor %}
{% endif %}
{# Add to result if all conditions are met #}
{% if is_leaf and ns.match_prefix and ns.match_regex and not ns.path_excluded %}
{% do leaf_nodes.append(node) %}
{% endif %}
{% endfor %}
{# 5. Output / Logging #}
{% if leaf_nodes | length > 0 %}
{{ log("", info=True) }}
{{ log("Found " ~ leaf_nodes | length ~ " leaf node(s):", info=True) }}
{{ log("--------------------------------------------------", info=True) }}
{# Sort by resource_type #}
{% for node in leaf_nodes | sort(attribute='resource_type') %}
{% if node.resource_type == 'source' %}
{{ log("- [source] " ~ node.database ~ "." ~ node.schema ~ "." ~ node.name, info=True) }}
{% else %}
{{ log("- [" ~ node.resource_type ~ "] " ~ node.name ~ " (" ~ node.original_file_path ~ ")", info=True) }}
{% endif %}
{% endfor %}
{{ log("--------------------------------------------------", info=True) }}
{{ log("SUGGESTED ACTIONS:", info=True) }}
{{ log("1. Configure the resource with `enabled: false`.", info=True) }}
{{ log(" See docs: https://docs.getdbt.com/reference/resource-configs/enabled", info=True) }}
{{ log("2. Or remove the model file and DROP the table/view from your database.", info=True) }}
{{ log("--------------------------------------------------", info=True) }}
{{ log("", info=True) }}
{% else %}
{{ log("No leaf nodes found matching the criteria.", info=True) }}
{% endif %}
{{ return(leaf_nodes) }}
{% endmacro %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment