-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug] Calling a macro in a pre- or post-hook #7128
Comments
Acceptance criteria
|
This issue has been marked as Stale because it has been open for 180 days with no activity. If you would like the issue to remain open, please comment on the issue or else it will be closed in 7 days. |
Although we are closing this issue as stale, it's not gone forever. Issues can be reopened if there is renewed community interest. Just add a comment to notify the maintainers. |
This would be very beneficial for us as we maintain a multi-tenant architecture and would allow us to have dbt docs generate for our models that run across tenants |
The issue is still relevant, still experiencing inconsistent behaviour. |
Ive just bump into this issue myself when we started using post-hook in our dbt project |
Same. I refactored all the individual |
Multiple people have reported the desire to put all configs within Resolving this issue will provide that capability. In the meantime, a workaround is provided below along with a reproducible example (reprex). ReprexThis works when running
{{ config(
pre_hook="{{ some_macro() }}"
) }}
select 1 as id And so does this:
name: my_project
profile: my_profile
models:
my_project:
+pre-hook: "{{ some_macro() }}" But this doesn't:
models:
- name: my_model
config:
pre_hook: "{{ some_macro() }}" And it gives this error message:
WorkaroundIn the meantime, the workaround is to put hook configs within model files instead of {{ config(
pre_hook="{{ some_macro() }}"
) }}
... |
@dbeatty10 What would you suggest for python models? |
@rbs392 See below for something you could try out for dbt python models. I didn't try it out personally, so let me know if it works or not 🙏 Let's suppose you have a python model at the path name: my_project
profile: my_profile
models:
my_project:
subfolder_1:
subfolder_2:
my_python_model:
+pre-hook: "{{ some_macro() }}" |
Yeah this seems to work 😄 |
We're not able to prioritize this ourselves right now, so labeling this as "help wanted". |
This is also an issue when I try to apply macro to post-hook within seed property file. Since you cannot add config directly to seed file, the only workaround is to use |
I ran into this myself this week. The solution would be late-rendering hooks that appear within The relevant code appears to be The analogous code for |
To expand on the use-case of adding a post hook to a seed in a properties YAML file, here's an example: seeds:
- name: seed_name
config:
post_hook: "alter table {{ this }} alter column id set not null"
columns:
- name: id But when running
If instead this worked, then this would be a useful alternative to add to #10551 for adding database constraints to a seed (or snapshot). |
[Preview: Calling a macro in a hook](https://docs-getdbt-com-git-dbeatty10-patch-2-dbt-labs.vercel.app/docs/build/hooks-operations#calling-a-macro-in-a-hook) [Preview: pre-hook & post-hook ](https://docs-getdbt-com-git-dbeatty10-patch-2-dbt-labs.vercel.app/reference/resource-configs/pre-hook-post-hook) ## What are you changing in this pull request and why? We have a couple concrete examples of calling a macro in a hook (like [here](https://docs.getdbt.com/reference/project-configs/on-run-start-on-run-end#call-a-macro-to-grant-privileges) and [here](https://docs.getdbt.com/reference/resource-configs/pre-hook-post-hook#apache-spark-analyze-tables-after-creation)). But we don't currently have any co-located canonical examples of the different locations where hooks can be configured: 1. SQL model config 1. `properties.yml` \* 1. `dbt_project.yml` \* Calling a macro in a hook within a properties YAML file was fixed in v1.8+ by dbt-labs/dbt-core#10603. ## Other pages to review Once dbt-labs/dbt-core#7128 is resolved, we should also review these pages to ensure they include examples of configs across `dbt_project` YAML files, properties YAML files, and node-level config in SQL files: - https://docs.getdbt.com/reference/resource-configs/pre-hook-post-hook - https://docs.getdbt.com/reference/model-configs#apply-configurations-to-one-model-only - https://docs.getdbt.com/reference/seed-configs#apply-the-schema-configuration-to-one-seed-only ## Additional context Inspired by dbt-labs/dbt-core#10522 ## Checklist - [x] Review the [Content style guide](https://github.com/dbt-labs/docs.getdbt.com/blob/current/contributing/content-style-guide.md) so my content adheres to these guidelines. --------- Co-authored-by: Mirna Wong <[email protected]>
Hi team, this bug seems to be back for YAML-defined snapshots.
This is my snapshot definition in a Jaffle shop project:
The macro is defined as:
Lmk if you need more info. |
UPDATE: |
@lucidviews Thanks for connecting this to #10965. That issue seems to precisely describe what you are observing for |
Is this a new bug in dbt-core?
Current Behavior
There's 3 ways to define a pre-hook for a model:
dbt_project.yml
properties.yml
)my_model.sql
)Calling a macro in a pre-hook works for 1. and 3. but not 2:
produces a compilation error
Expected Behavior
The behavior should be consistent across 1. 2. 3. Ideally, calling a macro for a pre-hook should work in property files too.
Steps To Reproduce
See Current Behavior
Relevant log output
No response
Environment
Which database adapter are you using with dbt?
snowflake
Additional Context
https://getdbt.slack.com/archives/C50NEBJGG/p1677527674055049
The text was updated successfully, but these errors were encountered: