Reputation: 2763
Doing a variation on this example for a macro (grant_select_on_schemas.sql
) to set grants on a snowflake instance after dbt runs. My issue is that I've inherited a non-standard dbt build configuration which includes some statically defined non-target model locations.
Examples:
snowflake-instance
|
|> raw_db
|> elt_schema_1
|> elt_schema_2
|> elt_schema_3
|> utils_db
|> calendar_schema_1
|> staging_db
|> elt_staging_1
|> elt_staging_2
|> elt_staging_3
|> analytics_db
|> core_models
|> mart_1
|> mart_2
profiles.yml
target: prod
outputs:
prod:
type: snowflake
account: my-account.region-1
role: my-role
# User/password auth
user: <user>
password: <pass>
database: raw_db
warehouse: my-warehouse
schema: PUBLIC
threads: 2
client_session_keep_alive: False
query_tag: my-dbt-local
dbt-project.yml
models:
my-pro:
+materialized: table
utils:
+database: UTILS
+materialized: table
calendar:
+schema: calendar_schema_1
staging:
+database: staging_db
+materialized: view
elt_staging_1:
+schema: elt_staging_1
elt_staging_2:
+schema: elt_staging_2
elt_staging_3:
+schema: elt_staging_3
grant_select_on_schemas.sql
-- macros/grants/grant_select_on_schemas.sql
{% macro grant_select_on_schemas(schemas, role) %}
{% for schema in schemas %}
{% for role in roles %}
grant usage on schema {{ schema }} to role {{ role }};
grant select on all tables in schema {{ schema }} to role {{ role }};
grant select on all views in schema {{ schema }} to role {{ role }};
grant select on future tables in schema {{ schema }} to role {{ role }};
grant select on future views in schema {{ schema }} to role {{ role }};
{% endfor %}
{% endfor %}
{% endmacro %}
Currently, I'm running into the issue with this macro that the macro is attempting to run on against all schemas on my profile's {{ target.database }}
(which is currently set to staging_db
) and as a consequence is erroring when attempting things like:
> Database Error
> 002003 (02000): SQL compilation error:
> Schema 'staging_db.core_models' does not exist or not authorized.
What am I missing?
Upvotes: 5
Views: 1760
Reputation: 256
I joined stackoverflow just to answer your question because I had the same one 6-8+ months ago (I even double checked that this wasn't me asking the question because that would be embarrassing).
Checkout the database_schemas
variable that lives in the depths of the dbt documentation:
https://docs.getdbt.com/reference/dbt-jinja-functions/on-run-end-context#database_schemas
You should be able to add it in as such:
{% macro grant_select_on_schemas(database_schemas, role) %}
{% for (database,schema) in database_schemas %}
{% for role in roles %}
grant usage on schema {{ database }}.{{ schema }} to role {{ role }};
grant select on all tables in schema {{ database }}.{{ schema }} to role {{ role }};
grant select on all views in schema {{ database }}.{{ schema }} to role {{ role }};
grant select on future tables in schema {{ database }}.{{ schema }} to role {{ role }};
grant select on future views in schema {{ database }}.{{ schema }} to role {{ role }};
{% endfor %}
{% endfor %}
{% endmacro %}
Upvotes: 2