dbt (Data Build Tool) — Data Engineering

dbt does the transformation (the T of ELT) inside the warehouse: each model is one SELECT file and dbt creates its table or view. source() points at a source…

By now, Bookshop has plenty of transformation SQL: the cleaning, dim_book, fact_sales, the weekly winners table. Someone has to remember the order to run it in, and test the results every night. dbt (data build tool) can take that over. It does the T of ELT: transformation inside the warehouse. Each table you want is written as one file holding one SELECT, and dbt works out the order, creates the table or view, runs the tests and writes the documentation. It's like a restaurant kitchen. Each recipe sits on its own card, and the chilli sauce card says "use the fried shallots from card 3". The head chef reads every card, knows the shallots come first, cooks in that order, then tastes each dish before it goes out. If the chilli sauce is too salty, the fried rice that uses it is not served. A recipe card is a model, "use card 3" is ref(), tasting is a test, and the head chef is dbt build.

Models: one file, one SELECT

Bookshop's dbt project ran for real on a laptop with dbt Core 1.12.5 (released 15 September 2026), the dbt-duckdb 1.11.0 adapter and DuckDB 1.5.6. The app's data (5 MySQL tables) has already been loaded into the bookshop schema by the EL step; dbt only does the transformation.

One day after that release, on 16 September 2026, dbt Labs and Fivetran released dbt v2 (GA): the dbt engine fully rewritten in Rust. The Python line this lesson uses is now called dbt v1. Both are licensed under Apache 2.0 and keep getting security support. Other options:

Version When to pick it
dbt v1 (Python, the dbt-core package plus an adapter such as dbt-duckdb) The default in this lesson: every Python adapter and almost every existing tutorial uses it.
dbt v2 (Rust, python -m pip install dbt) A large project that needs faster parsing and builds. The adapters ship inside it, and DuckDB is generally available on v2 (CLI only). First check your warehouse's adapter status on the "Supported data platforms" page of the dbt documentation.
dbt parse --use-v2-parser in dbt 1.12 Tests whether v2 can read your v1 project, before you switch. If it succeeds, your project is ready.
bookshop/
├── dbt_project.yml
├── models/
│   ├── staging/   _sources.yml, _staging.yml, stg_orders.sql, stg_order_items.sql, stg_customers.sql
│   └── marts/     _marts.yml, dim_book.sql, dim_customer.sql, fact_sales.sql, weekly_best_sellers.sql
├── seeds/         dim_date.csv
├── snapshots/     snap_books.yml
└── tests/         assert_paid_price_matches_catalog.sql

One model = one .sql file holding one SELECT = one table or view in the warehouse, named after the file. A staging model tidies one source table (renames columns, casts types); a mart model builds a table ready to use. This is models/staging/stg_orders.sql:

select
    id as order_id,
    customer_id,
    created_at,
    cast(replace(substr(cast(created_at as varchar), 1, 10), '-', '') as integer) as date_key
from {{ source('bookshop', 'orders') }}

ref() and the DAG

A model that uses another model names it with ref(), not with a table name. Part of models/marts/fact_sales.sql:

from {{ ref('stg_order_items') }} as i
join {{ ref('stg_orders') }} as o on o.order_id = i.order_id
join {{ ref('dim_customer') }} as p on p.customer_id = o.customer_id
join {{ ref('dim_book') }} as b
    on b.book_id = i.book_id
    and o.created_at >= b.valid_from
    and o.created_at < b.valid_to

From each ref() dbt records an arrow: stg_order_items must exist before fact_sales. All the arrows form a DAG (directed acyclic graph, as in the Airflow lesson): sources, staging, marts, up to weekly_best_sellers, the weekly best-seller table the owner reads. The DAG also drives selection: a trailing + means "this model and everything that reads it". The output of dbt ls --select stg_orders+ --resource-type model:

bookshop.marts.fact_sales
bookshop.marts.weekly_best_sellers
bookshop.staging.stg_orders

ref() is also what lets the project move: on the laptop ref('stg_orders') becomes "bookshop"."main"."stg_orders", in the production warehouse it becomes the production schema, without changing a single file.

Materialization: view, table, incremental

Materialization is how dbt stores a model's result. In dbt_project.yml, every staging model becomes a view and every mart a table:

models:
  bookshop:
    staging:
      +materialized: view
    marts:
      +materialized: table
Materialization What is stored When read Fits
view the query only recomputed every time it is called light staging
table the query result as physical data directly, fast marts read often
incremental physical data, new rows added each build directly, fast a big, growing fact

So the difference: a view is stored as a query that runs when it is called, a table is stored as physical data. If on Saturday morning the EL step has loaded order 15 into bookshop but dbt has not run, stg_orders (a view) already holds 15 orders while fact_sales (a table) stays at 18 rows until the next build.

fact_sales is incremental. The top and bottom of its file:

{{ config(materialized='incremental', unique_key=['order_id', 'book_key']) }}

select ...
{% if is_incremental() %}
where o.date_key >= (select max(date_key) from {{ this }})
{% endif %}

Sources and freshness

_sources.yml registers the source tables and can give them a freshness rule: how recent the newest data must be.

sources:
  - name: bookshop
    schema: bookshop
    tables:
      - name: orders
        config:
          loaded_at_field: created_at
          freshness:
            warn_after: {count: 24, period: hour}
            error_after: {count: 48, period: hour}

dbt source freshness, run on Sunday 11 October in the evening:

1 of 1 START freshness of bookshop.orders ...................................... [RUN]
1 of 1 WARN freshness of bookshop.orders ....................................... [WARN in 0.01s]

The latest order (10 October 10:15:30) is about 28.6 hours old: past 24 hours, not yet 48, so WARN. This is the timeliness check from the data quality lesson, written once in YAML.

Tests: checks that build along

Tests are written in YAML next to the model, under data_tests: (the name since dbt 1.8; tests: is still accepted). dbt Core has four built-in generic tests: unique, not_null, accepted_values, relationships. Part of models/staging/_staging.yml and models/marts/_marts.yml:

models:
  - name: stg_order_items
    data_tests:
      - unique:
          column_name: "order_id || '-' || book_id"
  - name: fact_sales
    columns:
      - name: book_key
        data_tests:
          - relationships:
              arguments:
                to: ref('dim_book')
                field: book_key
  - name: dim_customer
    columns:
      - name: city
        data_tests:
          - accepted_values:
              arguments:
                values: ['London', 'Bristol', 'Manchester', 'Unknown']

Test arguments (to, field, values) go under arguments:. Older tutorials put them straight under the test's name, and dbt 1.12 answers that with a MissingArgumentsPropertyInGenericTestDeprecation warning. Each test compiles to a SELECT that looks for rows breaking the rule, exactly as in the data quality lesson: it passes on 0 rows. You can also write a test yourself as an SQL file in tests/ (a singular test): assert_paid_price_matches_catalog.sql looks for items whose paid price differs from the catalogue price, and finds item 16. dbt build runs models and their tests interleaved, following the DAG. The end of the first build's log:

19 of 19 OK created sql table model main.weekly_best_sellers ................... [OK in 0.02s]
Finished running 1 incremental model, 1 seed, 1 snapshot, 3 table models, 10 data tests, 3 view models in 0 hours 0 minutes and 1.12 seconds (1.12s).
Done. PASS=17 WARN=2 ERROR=0 SKIP=0 NO-OP=0 REUSED=0 TOTAL=19

The two WARNs are not_null on Maya's city (given severity: warn) and item 16's price. Now the dedup in stg_order_items is deleted, so duplicate item 9 slips through, and that night dbt build runs again. Next morning the owner opens weekly_best_sellers. Guess first: does she see This Earth of Mankind 4 in London, yesterday's numbers, or an error? The build log:

9 of 19 FAIL 1 unique_stg_order_items_order_id_book_id ......................... [FAIL 1 in 0.01s]
16 of 19 SKIP relation main.fact_sales ......................................... [SKIP]
19 of 19 SKIP relation main.weekly_best_sellers ................................ [SKIP]
Done. PASS=12 WARN=2 ERROR=1 SKIP=4 NO-OP=0 REUSED=0 TOTAL=19

The unique test fails because the pair 6-2 appears twice, and every model downstream is SKIPPED. weekly_best_sellers is not rebuilt, so the owner still reads yesterday's build, not This Earth of Mankind 4, and whoever is on duty sees an ERROR. dbt test runs only the tests, dbt run builds only the models with no tests; dbt build runs both in DAG order.

Snapshots: price history

On Saturday 10 October at 08:00, This Earth of Mankind drops to 24. A snapshot records every version of a source row, exactly SCD type 2 from the star schema lesson:

snapshots:
  - name: snap_books
    relation: source('bookshop', 'books')
    config:
      unique_key: id
      strategy: timestamp
      updated_at: updated_at

With the timestamp strategy, dbt compares each book's updated_at with the version on record. After Saturday's build, dim_book (built from snap_books) holds:

book_key book_id title price valid_from valid_to is_current
2 2 This Earth of Mankind 25 2026-09-01 00:00:00 2026-10-10 08:00:00 false
5 2 This Earth of Mankind 24 2026-10-10 08:00:00 9999-12-31 00:00:00 true

Orders before Saturday keep book_key 2 (25); order 15 uses book_key 5 (24).

💡 Seeds, docs and lineage

⚠️ What makes a dbt project quietly wrong

TL;DR: dbt does the transformation (the T of ELT) inside the warehouse: each model is one SELECT file and dbt creates its table or view. source() points at a source table, ref() at another model, and from the ref() calls dbt builds the DAG and the build order. Materialization: view stores the query and runs it when called, table stores physical data, incremental processes only new rows (Saturday: 2 rows, delete+insert by unique_key, fact_sales becomes 19). Freshness in _sources.yml warns when source data is too old. The tests unique, not_null, accepted_values, relationships go under data_tests:, plus your own SQL tests in tests/; dbt build SKIPS the models downstream of a failing test, so yesterday's report is not overwritten with wrong numbers. Snapshots record history (SCD type 2: This Earth of Mankind book_key 2 and 5), seeds load CSVs, and dbt docs draws the lineage. This project ran with dbt Core 1.12.5 and dbt-duckdb 1.11.0.

⚡ What dbt does

Meaning: dbt does the T of ELT: it transforms data inside the warehouse with organised, testable SQL. Copying data in is the EL step's job, scheduling is an orchestrator's.

⚡ Models: one file, one SELECT

Write:

select
    id as order_id,
    customer_id,
    created_at,
    cast(replace(substr(cast(created_at as varchar), 1, 10), '-', '') as integer) as date_key
from {{ source('bookshop', 'orders') }}

Meaning: No CREATE TABLE, no INSERT. You write only the SELECT; dbt wraps it into a table or view. Other models are named with ref(), and from those dbt builds the DAG and the build order.

⚡ View versus table

Meaning: A view is stored as a query that runs every time it is called. A table is stored as physical data, so it only changes at the next build. Incremental processes only new rows.

⚡ A failing test

Guess first: the dedup in stg_order_items is deleted, then dbt build runs. Next morning, does the owner see This Earth of Mankind 4, yesterday's numbers, or an error?

Yesterday's numbers. The unique test fails and every model downstream is SKIPPED, so weekly_best_sellers isn't overwritten with wrong numbers. Whoever is on duty sees an ERROR.

Meaning: Use dbt build, not dbt run, so a failing test stops the models downstream.

What you will learn