on-run-end 컨텍스트 변수

on-run-end 컨텍스트 변수

on-run-end 컨텍스트 변수들은 dbt 실행이 끝날 때 on-run-end 훅(hook)에서 쓸 수 있어요. on-run-end 훅 밖에서 쓰면 none으로 평가되니 주의해야 해요!

출처: 문서

본문

schemas

schemas 컨텍스트 변수는 dbt 실행 동안 dbt가 모델을 빌드한 스키마들을 참조하는 데 쓸 수 있어요. 이 변수는 dbt 실행이 끝날 때 특정 사용자에게 이 스키마들에 대한 사용 권한(usage)을 부여하는 데 사용할 수 있어요.

예시:

dbt_project.yml


on-run-end:
 - "{% for schema in schemas %}grant usage on schema {{ schema }} to db_reader;{% endfor %}"

실제로는 이 코드를 매크로로 넣는 것도 나쁘지 않아요:

macros/grants.sql


{% macro grant_usage_to_schemas(schemas, user) %}
  {% for schema in schemas %}
    grant usage on schema {{ schema }} to {{ user }};
  {% endfor %}
{% endmacro %}

dbt_project.yml


on-run-end:
 - "{{ grant_usage_to_schemas(schemas, 'user') }}"

database_schemas

database_schemas 컨텍스트 변수는 dbt 실행 동안 dbt가 모델을 빌드한 데이터베이스 스키마를 참조하는 데 쓸 수 있어요. 이 변수는 schemas 변수와 비슷하며, dbt 실행이 여러 다른 데이터베이스에 리소스를 빌드할 때 사용해야 해요.

예시:

macros/grants.sql


{% macro grant_usage_to_schemas(database_schemas, user) %}
  {% for (database, schema) in database_schemas %}
    grant usage on {{ database }}.{{ schema }} to {{ user }};
  {% endfor %}
{% endmacro %}

dbt_project.yml


on-run-end:
 - "{{ grant_usage_to_schemas(database_schemas, user) }}"

Results

results 변수에는 dbt job에서 실행된 리소스 하나당 하나의 요소를 가진 Result 객체의 리스트가 들어 있어요. Result 객체는 on-run-end 컨텍스트의 Jinja 안에서 run results JSON artifact를 채울 정보에 접근할 수 있게 해줘요.

사용 예시:

macros/log_results.sql

{% macro log_results(results) %}

  {% if execute %}
  {{ log("========== Begin Summary ==========", info=True) }}
  {% for res in results -%}
    {% set line -%}
        node: {{ res.node.unique_id }}; status: {{ res.status }} (message: {{ res.message }})
    {%- endset %}

    {{ log(line, info=True) }}
  {% endfor %}
  {{ log("========== End Summary ==========", info=True) }}
  {% endif %}

{% endmacro %}

dbt_project.yml


on-run-end: "{{ log_results(results) }}"

결과:

12:48:17 | Concurrency: 1 threads (target='dev')
12:48:17 |
12:48:17 | 1 of 2 START view model dbt_jcohen.abc............................... [RUN]
12:48:17 | 1 of 2 OK created view model dbt_jcohen.abc.......................... [CREATE VIEW in 0.11s]
12:48:17 | 2 of 2 START table model dbt_jcohen.def.............................. [RUN]
12:48:17 | 2 of 2 ERROR creating table model dbt_jcohen.def..................... [ERROR in 0.09s]
12:48:17 |
12:48:17 | Running 1 on-run-end hook
========== Begin Summary ==========
node: model.testy.abc; status: success (message: CREATE VIEW)
node: model.testy.def; status: error (message: Database Error in model def (models/def.sql)
  division by zero
  compiled SQL at target/run/testy/models/def.sql)
========== End Summary ==========
12:48:17 | 1 of 1 START hook: testy.on-run-end.0................................ [RUN]
12:48:17 | 1 of 1 OK hook: testy.on-run-end.0................................... [OK in 0.00s]
12:48:17 |
12:48:17 |
12:48:17 | Finished running 1 view model, 1 table model, 1 hook in 1.94s.

더 알아보기 (Learn more)

  • on-run-end 훅에서만 사용 가능한 변수들(schemas, database_schemas, results)이에요.
  • 관련 개념: on-run-start/on-run-end 훅, Result 객체, run results JSON artifact.