jinja2 - dbt jinja 中是否有所有*有效*数据库和模式组合的变量列表?
问题描述
在 dbt 运行后,在此示例中对宏 ( ) 进行变体grant_select_on_schemas.sql
以在雪花实例上设置授权。我的问题是我继承了一个非标准的 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
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 %}
目前,我遇到了这个宏的问题,该宏试图针对我的配置文件{{ 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 个月前也有同样的问题(我什至仔细检查了这不是我在问这个问题,因为那会很尴尬)。
查看database_schemas
dbt 文档深处的变量:
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 %}
推荐阅读
- jquery - 窗口调整大小问题以检测锚点
- python - 我可以将轴范围传递给 seaborn 中的 PairGrid 吗?
- python-3.x - 将具有 L2 正则化的梯度下降转换为代码
- javascript - 为什么onclick="ShowLocalDate()" 只工作一次,它遇到的第一个
- sql-server - SQL Server 尝试捕获无法创建、更改表和添加 PK 约束
- ios - 精炼 Swift API GET 函数
- facebook - 访问 Facebook Messenger 消息
- scala - 如何在 Spark DataFrame 列中放置 Scala 数组?
- python - Pandas - 通过将组中的重复项目视为新项目来对组内的项目进行排名
- java - JDBC SQL Server 查询超时