The dbt project for data-stack-lab. It turns the raw sources into a Kimball star schema whose account dimension keeps history, built only from what monthly extracts of the application database showed. Results and method: README, MEASUREMENTS.
erDiagram
dim_account ||--o{ fct_invoices : "version billed"
dim_account ||--o{ fct_daily_usage : "version in effect"
dim_date ||--o{ fct_invoices : invoice_date_key
dim_date ||--o{ fct_daily_usage : date_key
dim_plan ||--o{ dim_account : plan_id
dim_account {
string account_key PK "one per version"
bigint account_id "shared by every version"
int version_number
string plan_name
string status
timestamp valid_from
timestamp valid_to "exclusive; 9999-12-31 if current"
boolean is_current
}
fct_invoices {
string invoice_id PK
string account_key FK
int invoice_date_key FK
decimal amount_usd
}
fct_daily_usage {
string account_key FK
date activity_date
int date_key FK
bigint events
bigint active_users
}
| Layer | Schema | What it does |
|---|---|---|
| sources | raw |
as loaded: the current accounts extract, users, plans, invoices, events |
| snapshot | snapshots |
snap_accounts: every version an extract showed, timestamp strategy on updated_at |
| staging | staging |
views: types, e-mail normalised, redelivered events dropped |
| marts | marts |
tables: dim_account (Type 2), dim_plan, dim_date, dim_user (Type 1), fct_invoices, fct_daily_usage, revenue_by_plan |
| Fact (grain) | dim_account | dim_date | dim_plan (through the account version) |
|---|---|---|---|
fct_invoices (one invoice) |
✓ | ✓ | ✓ |
fct_daily_usage (account version × day) |
✓ | ✓ | ✓ |
- Contracts enforced on
dim_account,fct_invoicesandfct_daily_usage: column names and types are checked before the table is built. - Data tests (46): keys unique and not null, relationships from facts to
dimensions, accepted values, one current version per account, validity windows
that neither overlap nor leave gaps (
tests/generic/valid_windows.sql), the usage grain, and two reconciliations: the invoice fact holds exactly the billing export's invoices and money, and daily usage counts every event once. - Unit tests (3): a redelivered event counts once; the first version reaches back to signup and the open one ends at 9999-12-31; an invoice takes the version in effect when it was billed.
From the repository root, after make data:
uv run python scripts/warehouse.py # 13 snapshot loads, then dbt build cd dbt && DUCKDB_PATH=../data/warehouse.duckdb uv run dbt docs generate --profiles-dir .
profiles.yml has two targets: duckdb (the default, and the only one CI builds this
project on) and databricks (databricks/dbt_build.py
builds this project on a Databricks SQL warehouse; see
databricks/README.md). The Snowflake and BigQuery projects
would add their own targets and run this same project.