question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jinja context variable for selected resources

See original GitHub issue

Describe the feature

A Jinja context variable that includes the list of resources (by unique_id) selected in the current invocation:

{% if 'model.package_name.my_model' in selected_resources %}
  ...
{% endif %}

This would have similar caveats to graph and execute, because it wouldn’t be populated at parse time, only at compile/execute/runtime.

Bonus points if it includes DAG information, i.e. the order in which resources will be queued for execution.

Use cases:

  • An on-run-start hook that wants to know all resources about to execute. This is available in the on-run-end context, in the form of the Results object.
  • Use standard node selection syntax as the input to a macro. E.g. run audit_helper.compare_relations() for each resource selected/built by a CI job, to ensure no regressions, by looping over selected_resources in a run-operation (https://github.com/fishtown-analytics/dbt/issues/2740)
    • Could we do a half-decent version of this with Results today in an on-run-end hook?
    • Is this better as post-hook on each model?

Describe alternatives you’ve considered

  • Adding a property to manifest nodes: selected: true|false. Then users would access the same graph context variable and use that property. I understand that some users already do something similar, e.g. inspect the tags on each node, to emulate selection criteria. We could also consider selection_criteria, detailing why a given node has been selected. This could be included in the list output and a helpful tool for visualizing selection groupings.

  • Make the selection criteria directly available, via flags.args.models and flags.args.exclude: https://github.com/fishtown-analytics/dbt/issues/2253#issuecomment-606637983

Additional context

Further down the road:

Who will this benefit?

This comes up in a surprising number of more-complex use cases.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
b-percommented, Apr 12, 2022

No worries, I’ll create the issue and draft a PR

1reaction
jtcohen6commented, Apr 12, 2022

Very cool! @b-per Any chance I could ask you to open an issue in https://github.com/dbt-labs/docs.getdbt.com to get this documented for v1.1?

Read more comments on GitHub >

github_iconTop Results From Across the Web

selected_resources | dbt Developer Hub
The selected_resources context variable is a list of all the resources selected by the current dbt command selector. Its value depends on the...
Read more >
Context and Environment — Jinja Documentation
This context supports variables on multiple layers and lazy (deferred) objects. Often your application has a request object, database connection object or ...
Read more >
API — Jinja Documentation (3.0.x)
When a template accesses a variable the template does not define, Jinja looks up the variable in the context, after that the variable...
Read more >
Understanding Jinja - Salt Project Documentation
This would import the test template variable or macro, not the test state element, from the file ... By adding with context to...
Read more >
How to pass selected, named arguments to Jinja2's include ...
This way you can pass multiple variables to Jinja2 Include statement ... have access to the variables of the active context by default...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found