首页 > 解决方案 > 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.

我错过了什么?

标签: jinja2snowflake-cloud-data-platformdbt

解决方案


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

查看database_schemasdbt 文档深处的变量:

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

推荐阅读