自定义架构名称的宏不适用于 dbt 包

Ray*_*Mar 5 dbt

我在 dbt 包中使用自定义架构名称时遇到问题。

我使用dbt 文档中提供的宏。

{% macro generate_schema_name(custom_schema_name, node) -%}

    {%- set default_schema = target.schema -%}
    {%- if custom_schema_name is none -%}

        {{ default_schema }}

    {%- else -%}

        {{ default_schema }}_{{ custom_schema_name | trim }}

    {%- endif -%}

{%- endmacro %}
Run Code Online (Sandbox Code Playgroud)

我把这个宏放在我的 dbt 包中dbt package

最后我在另一个dbt项目dbt项目中使用这个dbt包。

这是我的 dbt 项目中的 dbt_project.yml :

name: 'covid_france'
version: '0.0.1'
config-version: 2

profile: 'default'

source-paths: ["models"]
analysis-paths: ["analysis"]
test-paths: ["tests"]
data-paths: ["data"]
macro-paths: ["macros"]
snapshot-paths: ["snapshots"]

target-path: "target"  
clean-targets:         
    - "target"
    - "dbt_modules"
Run Code Online (Sandbox Code Playgroud)

我的 dbt 包中的 dbt_project.yml :

name: 'covid_france'
version: '0.0.1'
config-version: 2

profile: 'default'

source-paths: ["models"]
analysis-paths: ["analysis"]
test-paths: ["tests"]
data-paths: ["data"]
macro-paths: ["macros"]
snapshot-paths: ["snapshots"]

target-path: "target"  
clean-targets:         
    - "target"
    - "dbt_modules"

models:
    covid_france:
        stg:
            materialized: table
            schema: stg
        ods:
            materialized: table
            process-airbyte-outputs:
                schema: ods
            unions:
                schema: ods
        prs:
            materialized: view
      
Run Code Online (Sandbox Code Playgroud)

当我尝试运行我的 dbt 项目时,它会导入 dbt 包,但不会应用应该从自定义架构名称中删除主架构前缀(在profiles.yml中提供)的宏例如:我的配置文件中提供的架构.yml 是“prs”。我还有其他名为 ods 和 stg 的自定义架构。但是当 dbt 运行时,它会创建 prs、prs_ods 和 prs_stg。

当我直接在 dbt 项目中使用该宏时,该宏可以正常工作(而不是将其放入我在 dbt 项目中使用的 dbt 包中)

先感谢您 !

小智 0

我相信您还需要定义作业的目标,因为宏取决于目标模式。例如

dbt run --models my_model --target dev
Run Code Online (Sandbox Code Playgroud)

在 dbt 云作业中,您可以定义为

      "settings": {"threads": 1,"target_name": "prod"},
Run Code Online (Sandbox Code Playgroud)