guide
guide8 min read

How to Enforce Data Contracts in CI Before a Renamed Column Breaks a Dashboard

A five-step setup that fails the pull request, not the dashboard

To enforce data contracts in CI, declare the contract next to the model, build the changed models against a production baseline on every pull request, and fail the check when a column name, type or constraint differs from what consumers were promised. dbt supports this natively through model contracts, which fail a build when the returned columns do not match the declared ones. The rest of this guide covers what that catches, what it misses, and how to close the gap.

Key Takeaways

  • •A data contract enforced only at deploy time is documentation; enforced in CI, it is a merge gate.
  • •Structural enforcement (names, types, constraints) catches a renamed column. It does not catch a column whose meaning changed but whose name and type stayed the same.
  • •CI should compare the pull request against a production baseline, not against the previous commit, so it sees exactly what consumers will see.
  • •A contract check is only useful if it also names the downstream consumers it would break; otherwise reviewers cannot judge severity.
  • •Start with the five to ten tables that feed executive dashboards, not the whole warehouse.

How to enforce data contracts in CI: the failure we are preventing

The scenario is common enough that practitioners keep describing it. An engineer renames customer_id to account_id in a staging model. The SQL is clean, the model builds, the reviewer approves because the diff is small and correct. Nothing in the pull request touches the dashboard that reads customer_id. It breaks the next morning, and the first person to notice is a finance lead.

Code review fails here because the reviewer sees the diff, not the consumers. A contract moves that knowledge out of people's heads and into a check that runs on every pull request. The steps below build that check up in layers, from the cheapest structural gate to the semantic checks that need more context.

Step 1: Declare the contract next to the model

A contract has to live in the same repository and the same pull request as the code it constrains. If it sits in a wiki or a separate registry, it drifts. In dbt, add contract: {enforced: true} under the model's config in its YAML file, and list every column with a name and a data_type. With enforcement on, dbt validates the columns the model returns against that list when it builds.

Add constraints where the warehouse supports them, such as not_null, and keep the list of columns explicit. The contract should describe the interface you promise consumers, so it is fine, and often better, for it to be smaller than the full table. Expose only what downstream users are allowed to depend on.

If you are not on dbt, the open-source Data Contract CLI reads a YAML contract that follows the Data Contract Specification and tests it against the live table. The same pattern applies: the contract is a file in the repo, and CI runs a command against it.

Step 2: Build changed models against a production baseline

A contract check that runs against a fresh empty schema proves little. The useful question is whether this pull request changes what production consumers see. In dbt, save the production run's manifest as an artifact, then in CI run dbt build --select state:modified+ --state ./prod-artifacts. The state:modified selector picks up models that differ from the baseline, and the + includes everything downstream of them.

dbt also provides a narrower selector, state:modified.contract, which picks up models whose contract definition changed. Use it as a separate, loud check: any pull request that edits a contract needs a deliberate reviewer sign-off, because a contract edit is the moment a promise is being broken or renegotiated.

Step 3: Wire the check into the pull request

Run the build from your CI system on every pull request that touches model or contract files. In GitHub Actions that is a workflow triggered on pull_request with a path filter, a step that downloads the production manifest, and a step that runs the build command above. Mark the job as a required status check in branch protection. If it is optional, it will be skipped the first time someone is in a hurry.

  • •Trigger on changes to model SQL, YAML contracts and seeds, not on every commit to the repo.
  • •Fetch the baseline manifest from the last successful production run, not from the pull request's base branch build.
  • •Run against a CI schema with representative data volumes, so type and null constraints are exercised.
  • •Post the failure reason as a pull request comment, with the old and new column list, so the author does not have to read logs.

Step 4: Show the blast radius, not just the failure

A red check that says "contract violation on stg_orders" tells the author what broke, not how much it matters. Reviewers need the second half: which models, dashboards and notebooks read the column. Lineage from the dbt manifest gives you the downstream models. It usually does not give you the BI layer or ad hoc queries, which is where the renamed column actually hurts.

This is where our Schema Agent fits. It detects column-level schema changes and projects downstream impact, so the pull request comment can say "this rename affects 3 models and 2 dashboards" instead of only "contract failed." Because every Data Workers agent is an MCP server, the same question is available from the terminal in Claude Code or Cursor: "What's the blast radius if I drop this column?" We covered how the agents share context across governance and quality in separate posts, and the short version is that impact analysis works best when it reads the same catalog the rest of the platform uses.

Step 5: Catch the changes structural checks miss

Everything so far is structural. It catches a rename, a dropped column, a type change from integer to string. It does not catch a column that keeps its name and type while its meaning changes: revenue that used to include refunds and now excludes them, a timestamp that moves from local time to UTC, a status column that gains a new value. The contract passes, the build is green, the dashboard number is silently wrong.

Three additions help, and they are our opinion on where to spend effort rather than a standard:

  • •Accepted-value and range tests on columns where meaning is encoded in values, so a new status or a sign flip fails CI.
  • •Distribution checks against the production baseline for key metrics, with a tolerance, so a 40% shift in a sum is flagged on the pull request rather than overnight.
  • •A required description change: if a column's SQL expression changes, the contract's description field must change in the same pull request, and a reviewer must read both.

None of these is perfect. The description rule in particular depends on human discipline, which is why we like having an agent compare the old and new expression and flag semantic edits for the reviewer. Our guardrail agents are designed to do this kind of check in the loop, with a per-domain autonomy dial deciding whether they only comment or can block a merge.

Rollout: start small and make failures cheap

Teams that enforce contracts on every model on day one tend to turn the check off within a month, because the failure rate is high and the fixes are unclear. A more durable order is below.

PhaseScopeGate behavior
Week 1Five to ten tables behind executive dashboardsContracts declared, CI runs in warn-only mode
Weeks 2-3Same tablesRequired check; contract edits need a named reviewer
Month 2All models exposed to other teamsRequired check plus blast-radius comment
Month 3Key metricsDistribution and accepted-value checks added

Track two numbers: how many pull requests the check blocked, and how many of those were real breaks rather than noise. If most blocks are noise, narrow the contracts; if none are real, check whether the contracts list the columns consumers actually use.

Frequently Asked Questions

What is the difference between a data contract and a data test? A test asserts something about the data after it is built, such as no nulls in a key. A contract declares the interface a producer promises to consumers, such as column names, types and constraints, and fails the build when the output does not match. Contracts are checked against shape; tests are checked against content, and you want both.

Do I need dbt to enforce data contracts in CI? No. dbt's model contracts are the lowest-effort route if you already use it. Otherwise a YAML contract plus the Data Contract CLI, or a short script that compares the pull request's table schema against the declared columns, achieves the same gate. What matters is that the check is required and runs against a production baseline.

Will a contract catch a renamed column that keeps the same type? Yes, if the column is listed in the contract by name. The build fails because the declared column is missing from the output. It will not catch a change in meaning where name and type stay the same; that needs the value-level checks in step 5.

How do I avoid blocking legitimate breaking changes? Provide an explicit path: versioned models, where the new interface ships beside the old one with a deprecation date, or a contract-edit approval from the owning team. The goal is not to forbid change but to make sure consumers hear about it before the merge.

Can an AI coding agent write the contracts? It can draft them quickly from existing models and the warehouse's information schema, and Claude Code or Cursor can do this from the repo. A human should still decide which columns are promises to consumers, since that is a product decision rather than a code one. Data Workers agents can then keep the contract and the catalog in sync as the schema evolves.