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

Is there a variable list of all *valid* databases & schemas combinations in dbt jinja?

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

示例:

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.

我错过了什么?

我加入 Whosebug 只是为了回答你的问题,因为我在 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 %}