Skip to content

Commit

Permalink
General examples of calling a macro in a hook (#5972)
Browse files Browse the repository at this point in the history
[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]>
  • Loading branch information
dbeatty10 and mirnawong1 authored Sep 5, 2024
1 parent 2399e86 commit a451264
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
35 changes: 35 additions & 0 deletions website/docs/docs/build/hooks-operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,41 @@ You can use hooks to provide database-specific functionality not available out-o

You can also use a [macro](/docs/build/jinja-macros#macros) to bundle up hook logic. Check out some of the examples in the reference sections for [on-run-start and on-run-end hooks](/reference/project-configs/on-run-start-on-run-end) and [pre- and post-hooks](/reference/resource-configs/pre-hook-post-hook).

<File name='models/<model_name>.sql'>

```sql
{{ config(
pre_hook=[
"{{ some_macro() }}"
]
) }}
```

</File>

<File name='models/properties.yml'>

```yaml
models:
- name: <model_name>
config:
pre_hook:
- "{{ some_macro() }}"
```
</File>
<File name='dbt_project.yml'>
```yaml
models:
<project_name>:
+pre-hook:
- "{{ some_macro() }}"
```
</File>
## About operations
Operations are [macros](/docs/build/jinja-macros#macros) that you can run using the [`run-operation`](/reference/commands/run-operation) command. As such, operations aren't actually a separate resource in your dbt project — they are just a convenient way to invoke a macro without needing to run a model.
Expand Down
36 changes: 36 additions & 0 deletions website/docs/reference/resource-configs/pre-hook-post-hook.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ select ...
```


</File>

<File name='models/properties.yml'>

```yml
models:
- name: [<model_name>]
config:
[pre_hook](/reference/resource-configs/pre-hook-post-hook): <sql-statement> | [<sql-statement>]
[post_hook](/reference/resource-configs/pre-hook-post-hook): <sql-statement> | [<sql-statement>]
```
</File>
</TabItem>
Expand All @@ -66,6 +78,18 @@ seeds:

</File>

<File name='seeds/properties.yml'>

```yml
seeds:
- name: [<seed_name>]
config:
[pre_hook](/reference/resource-configs/pre-hook-post-hook): <sql-statement> | [<sql-statement>]
[post_hook](/reference/resource-configs/pre-hook-post-hook): <sql-statement> | [<sql-statement>]
```
</File>
</TabItem>
<TabItem value="snapshots">
Expand Down Expand Up @@ -102,6 +126,18 @@ select ...

</File>

<File name='snapshots/properties.yml'>

```yml
snapshots:
- name: [<snapshot_name>]
config:
[pre_hook](/reference/resource-configs/pre-hook-post-hook): <sql-statement> | [<sql-statement>]
[post_hook](/reference/resource-configs/pre-hook-post-hook): <sql-statement> | [<sql-statement>]
```
</File>
</TabItem>
</Tabs>
Expand Down

0 comments on commit a451264

Please sign in to comment.