gpt4 book ai didi

jinja2 - dbt jinja 中是否有所有*有效*数据库和模式组合的变量列表?

转载 作者:行者123 更新时间:2023-12-05 05:55:43 31 4
gpt4 key购买 nike

this example 上做一个变体用于宏 (grant_select_on_schemas.sql) 在 dbt 运行后在雪花实例上设置授权。我的问题是我继承了一个非标准的 dbt 构建配置,其中包括一些静态定义的非目标模型位置。

例子:

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

配置文件.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 %}

目前,我遇到了这个宏的问题,该宏试图针对我个人资料的 {{ target.database }}(当前设置为 staging_db),因此在尝试类似以下操作时出错:

> Database Error
> 002003 (02000): SQL compilation error:
> Schema 'staging_db.core_models' does not exist or not authorized.

我错过了什么?

最佳答案

我加入 stackoverflow 只是为了回答您的问题,因为 6-8 个月前我也遇到过同样的问题(我什至仔细检查了这不是我问的问题,因为那样会很尴尬)。

检查位于 dbt 文档深处的 database_schemas 变量:

https://docs.getdbt.com/reference/dbt-jinja-functions/on-run-end-context#database_schemas

你应该可以这样添加它:

{% 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 %}

关于jinja2 - dbt jinja 中是否有所有*有效*数据库和模式组合的变量列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69398772/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com