Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Test data during development

Verify data contracts on every pull request with GitHub Actions, so a change that breaks the data is caught before it merges.

This guide shows how to verify Soda data contracts in a GitHub Actions workflow that runs on every pull request. The workflow builds the dbt models in a CI schema, verifies their contracts, and posts the results to the pull request. When a check fails, the job fails, so the change is caught before it reaches production.

Use the example as a starting point and adapt the data source, models, and contracts to your own repository.

Example repository

A team keeps its dbt models and their Soda contracts in the same repository. One model, fact_product_category, joins products to their subcategories and categories. Its contract requires every product to appear exactly once.

A pull request that changes the model triggers the workflow, which:

  1. Builds the models with dbt in a CI schema, separate from production

  2. Verifies every contract against the CI schema

  3. Writes the results to the job summary and posts them as a comment on the pull request

  4. Fails the job if any check fails

The repository has this layout:

.github/
└── workflows/
    └── verify-data-contracts.yml
dbt/
├── dbt_project.yml
├── profiles.yml
└── models/
    ├── sources.yml
    └── fact_product_category.sql
soda/
├── ds_config.yml
└── contracts/
    └── fact_product_category.yml

Prerequisites

  • A GitHub repository with GitHub Actions enabled

  • A PostgreSQL data source that GitHub-hosted runners can reach, with a schema the workflow can write to for CI builds

  • A dbt project that builds models in that data source

Configure the data source

Create a data source configuration file in the soda folder. The values in ${env.…} are read from environment variables, which the workflow sets from the repository's secrets and variables.

dbt reads the same environment variables. The CI target builds models in the schema named by CI_SCHEMA, and the sources read from SOURCE_SCHEMA:

For the configuration keys of each data source, see the Data source reference for Soda Core.

Write the contract

The contract uses two variables, DATABASE and SCHEMA, in the dataset name. The workflow sets them to the CI schema, so the same contract file can verify the production table when you pass the production schema instead.

A product without a category isn't an error in this model, so that check warns instead of failing.

To learn more about variables, see Make contracts dynamic with variables.

Create the workflow

Add the workflow file to .github/workflows:

A few details in the workflow:

  • soda contract verify exits with 2 when checks only warn. The verify step treats 2 as a pass, and 1 or any other code as a failure. See Exit codes in the CLI reference.

  • The verify step runs every contract before it reports, so one failing contract doesn't hide the results of the others. The last step fails the job.

  • pull-requests: write lets the workflow post the comment with the built-in GITHUB_TOKEN.

  • concurrency runs one workflow at a time, because every pull request builds in the same CI schema.

Add the secrets and variables

In the repository, open Settings > Secrets and variables > Actions, and add the values the workflow reads:

Name
Kind
Value

POSTGRES_HOST

Secret

Host name of the PostgreSQL server

POSTGRES_USER

Secret

User that builds the CI schema and runs the checks

POSTGRES_PASSWORD

Secret

Password of that user

POSTGRES_DATABASE

Variable

Database name

SOURCE_SCHEMA

Variable

Schema of the source tables

CI_SCHEMA

Variable

Schema where the workflow builds the models

Open a pull request

Open a pull request that changes a model. The workflow runs, and its results appear in three places: the check on the pull request, a comment on the pull request, and the job summary.

When a check fails, the job fails. For example, a change that joins fact_internet_sales into fact_product_category without aggregating it repeats each product once per sale. dbt builds the model without an error, but the contract's Each product appears once check fails, and the pull request shows a failed check.

The checks box of a pull request in GitHub, with the Verify data contracts check failed
The failed check on the pull request
The comment the workflow posts on the pull request, with the contract results table and the failed check Each product appears once
The comment the workflow posts, with the contract results

To block merging while the check fails, make the verify job a required status check in a branch protection rule. See About protected branches in the GitHub documentation.

Next steps

Last updated

Was this helpful?