3. Sources & Seeds
Defining and Referencing Raw Sources
In dbt, a source is a way to define raw data existing in your warehouse that you intend to transform. By defining these in YAML files, dbt gains awareness of the provenance of your data. This abstraction allows you to reference tables using the `source()` function rather than hard-coding schema names, which significantly improves portability across different environments (e.g., development, staging, production).
By centralizing source definitions, you enable dbt to build a robust lineage graph. This visibility is critical for impact analysis, as it shows exactly which downstream models are affected if a raw source schema changes. Furthermore, the `source()` function creates an implicit dependency, ensuring that dbt executes operations in the correct order based on the directed acyclic graph (DAG).
version: 2
sources:
- name: raw_stripe
database: raw_db
schema: stripe
tables:
- name: payments
freshness:
warn_after: {count: 12, period: hour}
error_after: {count: 24, period: hour}
loaded_at_field: updated_at
Configuring Source Freshness Checks
Data quality begins at ingestion. dbt's source freshness functionality allows you to validate that your pipelines are running on schedule. By defining `warn_after` and `error_after` thresholds, you can enforce Service Level Agreements (SLAs) directly within your transformation layer. dbt calculates the delta between the current timestamp and the maximum value in your `loaded_at_field`.
If the data lag $t_{lag} = T_{now} - T_{max}$ exceeds the thresholds, dbt will return a warning or error status. This proactive monitoring allows engineers to catch upstream ingestion failures before they affect business-critical reporting models. Executing `dbt source freshness` should be a standard component of your orchestration pipeline, ideally scheduled immediately after data ingestion completes.
# Execute source freshness checks from the CLI dbt source freshness --select source:raw_stripe.payments # Run all freshness tests for the entire project dbt source freshness
Managing Static Data with dbt Seeds
dbt Seeds provide a mechanism to load CSV files into your warehouse. This is ideal for static or slowly changing lookup tables—such as mapping codes, geography lookups, or categorization rules—that are not managed by your primary ingestion pipeline. By keeping these files within your dbt repository, you ensure that your reference data is version-controlled and deployed via the same CI/CD process as your SQL logic.
When you execute `dbt seed`, dbt parses the CSV, infers the data types, and creates the corresponding table in your warehouse. Because seeds are treated like tables, they can be referenced in your models using the `ref()` function. This creates an elegant bridge between static business context and dynamic transformation logic, maintaining a single source of truth for your master data mapping.
-- Reference a seed in your dbt model SELECT p.payment_id, l.region_name FROM {{ source('raw_stripe', 'payments') }} p LEFT JOIN {{ ref('country_codes') }} l ON p.country_code = l.code
Knowledge Check
Ready to test your understanding of 3. Sources & Seeds?