检测 table 以查看 CI 中的物化变化

Detect table to view materialization change in CI

是否有一种简单的方法来检测 CI 中实现的变化,以避免 dbt 运行 失败并出现错误

Compilation Error in model stores_stores
(models/marts/core/blah.sql)   Trying to create view
`blah`.`dbt`.`blah`, but it currently exists as a
table. Either drop `blah`.`dbt`.`blah` manually,
or run dbt with `--full-refresh` and dbt will drop it for you.

谢谢!

有办法,但不是特别“容易”。

您可以利用 artifacts that dbt generates

  • manifest.json:由compileruntestdocs generatels
  • 制作
  • run_results.json:由runtestseedsnapshotdocs generate
  • 制作
  • catalog.json:由docs generate
  • 制作

run_resultsmanifest 中都可以找到实体化更改的信息。但是,在您向 CI 添加检查以尽早失败的上下文中,您希望在从 dbt run 收到错误之前得到通知。所以你实际上可以用 dbt compile.

生成 manifest.json

在清单的nodes键中,每个节点都会有一个config.materialized键,您可以查看。您可以使用命令行或 python 对其进行解析,并将结果存储到 JSON 文件中,该文件包含每个模型的具体化信息。例如,该文件可以签入您的代码。

cat target/manifest.json | jq '.nodes | to_entries | map({node: .key, materialized: .value.config.materialized})' > old_state.json

然后在您更改了 dbt 代码后,您需要 运行

dbt compile  # generates new manifest.json
cat target/manifest.json | jq '.nodes | to_entries | map({node: .key, materialized: .value.config.materialized})' > new_state.json

然后您可以比较两个状态,例如diff 在命令行中。我将在此处放置示例输出:

$ diff old_state.json new_state.json
12c12
<     "materialized": "table"
---
>     "materialized": "view"

正如我所说,这并不“容易”,但我希望我的回答能给您一些关于如何继续获得您想要的东西的想法。如果您对更多细节感兴趣,可以查看 my blog post on the topic.