updated_at

updated_at

updated_at은 스냅샷 쿼리 결과에서 레코드 행이 마지막으로 수정된 시점을 나타내는 컬럼이에요. timestamp 전략을 쓸 때 필수이며, 데이터 플랫폼에 따라 ISO 날짜 문자열과 unix epoch 정수를 지원할 수 있어요.

출처: 문서

본문

(dbt v1.9 이상 적용) snapshots/snapshots.yml

snapshots:
  - name: snapshot
    relation: source('my_source', 'my_table')
    config:
      strategy: timestamp
      updated_at: column_name

dbt_project.yml

snapshots:
  <resource-path>:
    +strategy: timestamp
    +updated_at: column_name

(dbt v1.9 이상 적용)

주의: updated_at 컬럼의 데이터 타입이 어댑터가 설정한 기본값과 일치하지 않으면 경고가 나와요.

Description

updated_at은 스냅샷 쿼리 결과에서 레코드 행이 마지막으로 수정된 시점을 나타내는 컬럼이에요.

이 파라미터는 timestamp 전략을 쓸 때 필수예요. updated_at 필드는 데이터 플랫폼에 따라 ISO 날짜 문자열과 unix epoch 정수를 지원할 수 있어요.

Default

기본값은 없어요.

Examples

updated_at 컬럼 이름 사용하기

(dbt v1.9 이상 적용)

snapshots/orders_snapshot.yml

snapshots:
  - name: orders_snapshot
    relation: source('jaffle_shop', 'orders')
    config:
      schema: snapshots
      unique_key: id
      strategy: timestamp
      updated_at: updated_at

두 컬럼을 Coalesce해 신뢰할 수 있는 updated_at 컬럼 만들기

레코드가 갱신될 때만 updated_at 컬럼이 채워지는 데이터 소스를 생각해 볼게요. 그러면 null 값은 레코드가 생성된 뒤 갱신된 적이 없다는 뜻이에요.

updated_at 설정은 표현식이 아니라 컬럼 이름만 받기 때문에, snapshot 쿼리를 coalesce된 컬럼을 포함하도록 수정해야 해요.

(dbt v1.9 이상 적용)

  1. 변환을 수행하는 스테이징 모델을 만들어요. models/ 디렉터리에 updated_atcreated_at 컬럼을 coalesce해서 새 컬럼 updated_at_for_snapshot으로 만드는 스테이징 모델을 설정하는 SQL 파일을 생성해요.

models/staging_orders.sql

select  * coalesce (updated_at, created_at) as updated_at_for_snapshot
from {{ source('jaffle_shop', 'orders') }}
  1. YAML 파일에서 스냅샷 설정을 정의해요. snapshots/ 디렉터리에 스냅샷을 정의하고 방금 만든 updated_at_for_snapshot 스테이징 모델을 참조하는 YAML 파일을 생성해요.

snapshots/orders_snapshot.yml

snapshots:
  - name: orders_snapshot
    relation: ref('staging_orders')
    config:
      schema: snapshots
      unique_key: id
      strategy: timestamp
      updated_at: updated_at_for_snapshot
  1. dbt snapshot을 실행해 스냅샷을 실행해요.

또는 필요한 변환을 수행하는 ephemeral 모델을 만든 뒤, 스냅샷의 relation 키에서 이 모델을 참조할 수도 있어요.

더 알아보기 (Learn more)

  • Snapshots — 스냅샷과 timestamp 전략.
  • strategy — 변경 감지 전략 설정.