I have a dbt_project.yml
like:
name: rdb
profile: rdb
source-paths: ['models']
version: "0.1"
models:
rdb:
schema: cin
materialized: table
post-hook: 'grant select on {{ this }} to rer'
on-run-end:
# TODO: fix
- 'grant usage on schema "{{ target.schema }}" to rer'
DBT has worked really well. But with the on-run-end
entry, it fails with Compilation Error 'target' is undefined
. With that line commented out, it works fine.
Am I making a basic mistake? Thanks!
Your post-hook should actually look like this:
on-run-end:
- "{% for schema in schemas %}grant usage on schema {{ schema }} to rer;{% endfor %}"
The dbt docs for the on-run-end context explain this in detail, but long story short: because a dbt run may touch tables in different schemas on the target database, there is no single target.schema
value to which you can apply the grant statement. Instead, the context provides you with a list of schema names called schemas
that you need to loop through. That list has one or more elements.
The target
in dbt is the configuration data for the adapter, like account, user, port, or schema. this
is about the database object that is being written, and also includes a field schema
. Finally, the on-run-end
context provides the list of schemas, so that you are not forced to make redundant grant statements for each table or view, but can make just a single grant for each schema.