Skip to content
Tech Atlas, home
Search
Esc
  • All roadmapsBrowse every learning path
  • ExploreFind a technology area
  • ProjectsGuided projects and builds
  • My AtlasYour learning dashboard
Lesson 2 of 6 · Lab · 25 min · revision 2

Lab: Enforcing a Contract and Shipping a Breaking Change

Write an Open Data Contract Standard (ODCS) v3.2.0 contract for an orders table, test it with the Data Contract CLI in the producer's CI, watch CI block a rename that would break consumers, then ship the rename as version 2 next to a deprecated version 1.

Contentsand lesson path

What you will learn

  • Write a data contract covering schema, semantics, quality expectations, freshness SLA, owner and version, enforce it at the producer boundary, and roll out a breaking change with a deprecation window.

01. Goal and setup

The checkout team publishes an orders table that finance and analytics read every morning. In this lab you are the checkout team. You will write the promise you make to those consumers as a data contract, check that promise automatically every time your job changes, and then make a change that would break consumers in the safe way: as a new major version, published next to the old one for a deprecation window.

You will use two open tools. The Open Data Contract Standard (ODCS) is a YAML format for contracts, maintained by the Bitol project of the Linux Foundation; version 3.2.0 is current. The Data Contract CLI reads an ODCS file, connects to the data and runs the checks the contract implies. Here the data lives in a DuckDB file so that everything runs on your laptop; the same contract can point at PostgreSQL, Snowflake, BigQuery or files in object storage.

You need Python 3.10 or later. The lab was tested with Python 3.12, datacontract-cli 1.2.10 and DuckDB 1.5.6.

Terminal
mkdir contract-lab && cd contract-lab && mkdir data
python3 -m venv .venv && source .venv/bin/activate
pip install "datacontract-cli[duckdb]==1.2.10" "duckdb==1.5.6"
Folder layout at the end of the lab
contract-lab/
├── export_orders.py     step 1, replaced in step 6: the producer's job
├── orders.odcs.yaml     step 2, deprecated in step 6: contract version 1
├── orders-v2.odcs.yaml  step 5: contract version 2
├── ci.sh                step 3: the producer's CI check
└── data/shop.duckdb     the published tables

02. Step 1: Run the producer job

The job publishes 2,000 orders, generated from the row number so that everyone gets the same values. loaded_at records when the row was published, in UTC; the contract's freshness rule will read it.

Python · export_orders.py
"""The checkout team's job: publish yesterday's orders for other teams."""
import duckdb

con = duckdb.connect("data/shop.duckdb")
con.execute("""
    create or replace table orders as
    select 'ORD-' || lpad(i::text, 5, '0') as order_id,
           1 + (i * 37) % 400 as customer_id,
           timestamp '2026-11-15 00:00:00' + to_seconds(i * 41) as ordered_at,
           (5 + (i * 53) % 4000 / 20)::decimal(10, 2) as amount,
           ['paid', 'refunded'][1 + (i % 9 = 0)::int] as status,
           (current_timestamp at time zone 'UTC') as loaded_at
    from range(1, 2001) t(i)
""")
print(con.sql("select count(*) as orders, sum(amount) as amount from orders"))
Terminal
python export_orders.py
Expected output
┌────────┬───────────────┐
│ orders │    amount     │
│ int64  │ decimal(38,2) │
├────────┼───────────────┤
│   2000 │     208050.00 │
└────────┴───────────────┘

03. Step 2: Write the contract

Read the contract from top to bottom. Each part answers a question a consumer would otherwise have to ask in a chat channel.

  • Who and what version. id, version and status identify the promise. The version follows semantic versioning: a major number for changes that can break consumers, a minor number for compatible additions, a patch number for fixes that change nothing consumers rely on. team names the owner, the person or team accountable when something is wrong.
  • What it means. description gives purpose, usage and limitations, and each property has a description. Semantics matter as much as types: "amount, including VAT, before any refund" stops each consumer from guessing.
  • Where it lives. servers tells the CLI how to reach the data: a DuckDB file here.
  • What shape and quality. schema lists the table's properties with types, required and unique. quality adds rules: a SQL check that no amount is negative, a library metric that status holds only known values, and a row count above zero.
  • How fresh. slaProperties holds service levels. The freshness rule says the newest loaded_at must be less than 6 hours old when checked.
YAML · orders.odcs.yaml
apiVersion: v3.2.0
kind: DataContract
id: urn:shop:checkout:orders
name: orders
version: 1.0.0
status: active
domain: checkout
description:
  purpose: One row per order placed in the web shop, for finance and analytics.
  usage: Read the orders table after 06:00 UTC. Join to customers on customer_id.
  limitations: Refunds are flagged in status; refunded amounts are not subtracted.
team:
  name: checkout
  members:
    - username: [email protected]
      role: owner
servers:
  - server: shop
    type: duckdb
    database: ./data/shop.duckdb
    schema: main
schema:
  - name: orders
    physicalName: orders
    physicalType: table
    properties:
      - name: order_id
        logicalType: string
        required: true
        unique: true
        description: Order number, ORD- followed by five digits.
      - name: customer_id
        logicalType: integer
        required: true
      - name: ordered_at
        logicalType: timestamp
        required: true
        description: When the customer placed the order, in UTC.
      - name: amount
        logicalType: number
        required: true
        description: Order value in euros, including VAT, before any refund.
        quality:
          - type: sql
            description: No negative amounts.
            query: SELECT COUNT(*) FROM {table} WHERE {column} < 0
            mustBe: 0
      - name: status
        logicalType: string
        required: true
        quality:
          - metric: invalidValues
            arguments:
              validValues: [paid, refunded]
            mustBe: 0
      - name: loaded_at
        logicalType: timestamp
        required: true
        description: When this row was published, in UTC.
    quality:
      - metric: rowCount
        mustBeGreaterThan: 0
slaProperties:
  - property: freshness
    element: orders.loaded_at
    value: 6
    unit: h

lint checks the file against the ODCS JSON schema. test connects to the data and runs every check. The CLI prints a summary line with the run time, which differs on every run, so sed removes it. Each command takes several seconds to start. The result table is as wide as your terminal, so on a narrower window the same lines wrap at different places.

Terminal
datacontract lint orders.odcs.yaml | tail -n 1 | sed -E 's/ Took .*//'
datacontract test orders.odcs.yaml | sed -E 's/ Took .*//'
Expected output (in a terminal about 88 columns wide; line wrapping follows your width)
🟢 Data contract is valid. Ran 1 checks.
Testing orders.odcs.yaml
Server: shop (type=duckdb, database=./data/shop.duckdb, schema=main)
╭────────┬─────────────────────────────────────────────────────┬─────────────┬─────────╮
│ Result │ Check                                               │ Field       │ Details │
├────────┼─────────────────────────────────────────────────────┼─────────────┼─────────┤
│ passed │ Check that model orders has row_count > 0           │             │         │
│ passed │ Check that field 'amount' is present                │ amount      │         │
│ passed │ Check that field amount has type number             │ amount      │         │
│ passed │ Check that field amount has no missing values       │ amount      │         │
│ passed │ No negative amounts.                                │ amount      │         │
│ passed │ Check that field 'customer_id' is present           │ customer_id │         │
│ passed │ Check that field customer_id has type integer       │ customer_id │         │
│ passed │ Check that field customer_id has no missing values  │ customer_id │         │
│ passed │ Check that field 'loaded_at' is present             │ loaded_at   │         │
│ passed │ Check that field loaded_at has type timestamp       │ loaded_at   │         │
│ passed │ Check that field loaded_at has no missing values    │ loaded_at   │         │
│ passed │ Freshness of orders.loaded_at < 6h                  │ loaded_at   │         │
│ passed │ Check that field 'order_id' is present              │ order_id    │         │
│ passed │ Check that field order_id has type string           │ order_id    │         │
│ passed │ Check that field order_id has no missing values     │ order_id    │         │
│ passed │ Check that unique field order_id has no duplicate   │ order_id    │         │
│        │ values                                              │             │         │
│ passed │ Check that field 'ordered_at' is present            │ ordered_at  │         │
│ passed │ Check that field ordered_at has type timestamp      │ ordered_at  │         │
│ passed │ Check that field ordered_at has no missing values   │ ordered_at  │         │
│ passed │ Check that field 'status' is present                │ status      │         │
│ passed │ Check that field status has type string             │ status      │         │
│ passed │ Check that field status has no missing values       │ status      │         │
│ passed │ Check that field status has invalid_count = 0       │ status      │         │
╰────────┴─────────────────────────────────────────────────────┴─────────────┴─────────╯
🟢 Data contract is valid. Ran 23 checks.

You wrote five properties and three rules, and the CLI ran 23 checks. It derives most of them from the schema: every property must exist, have the declared type and, when required, have no missing values; a unique property must have no duplicates.

04. Step 3: Enforce it at the producer boundary

A contract that nobody runs is documentation. Enforce it where it can stop damage: in the producer's own CI, before new data or new code is published. ci.sh rebuilds the data with the job's current code, then lints and tests every contract in the folder. set -euo pipefail makes the script fail as soon as any command fails, and a failing script fails the CI job, which blocks the merge. In a CI service you would run this on every pull request against a disposable copy of the data. The CLI also has a ci command that adds GitHub Actions annotations.

Bash · ci.sh
#!/usr/bin/env bash
# The producer's CI job: build the data, then test every contract against it.
set -euo pipefail
export COLUMNS=88         # CI has no terminal: wrap the CLI's table at a fixed width
mkdir -p data
python export_orders.py > /dev/null
for contract in *.odcs.yaml; do
  datacontract lint "$contract" > /dev/null
  datacontract test "$contract" | sed -E 's/ Took .*//' | grep -E '^(🟢|🔴|[0-9]+\))'
done
Terminal
chmod +x ci.sh
./ci.sh; echo "exit code: $?"
Expected output
🟢 Data contract is valid. Ran 23 checks.
exit code: 0

05. Step 4: Watch CI block a breaking change

A developer on the checkout team decides that amount is ambiguous and renames it to amount_gross in the job. The rename is a good idea, but every consumer query that selects amount would fail tomorrow morning. Make the change and run CI. (The -i.bak form of sed works on both Linux and macOS and keeps a backup.)

Terminal
sed -i.bak -e 's/ as amount,$/ as amount_gross,/' \
  -e 's/sum(amount)/sum(amount_gross)/' export_orders.py
./ci.sh; echo "exit code: $?"
Expected output
🔴 Data contract is invalid, found the following errors:
1) amount Check that field 'amount' is present: Required column 'amount' is missing
2) amount Check that field amount has type number: Column 'amount' is missing
3) amount Check that field amount has no missing values: Column 'amount' not found
4) amount No negative amounts.: Error evaluating check: Binder Error: Referenced column
exit code: 1

Long reasons are cut at the table width, as in line 4; datacontract test orders.odcs.yaml prints the whole message. CI fails, so the change never reaches consumers. The producer learns about the problem on their own pull request, while the change is still cheap to rethink. Put the original job back before you continue.

Terminal
mv export_orders.py.bak export_orders.py

06. Step 5: Classify the change before you ship it

The rename is still wanted, together with a new amount_net column. Write it as contract version 2.0.0: the same table name in the contract, published under the physical name orders_v2. Then let the CLI compare the two versions. breaking reports each difference with a severity from the consumer's point of view, and exits with code 1 if any change is breaking.

YAML · orders-v2.odcs.yaml
apiVersion: v3.2.0
kind: DataContract
id: urn:shop:checkout:orders
name: orders
version: 2.0.0
status: active
domain: checkout
description:
  purpose: One row per order placed in the web shop, for finance and analytics.
  usage: Read the orders_v2 table after 06:00 UTC. Join to customers on customer_id.
team:
  name: checkout
  members:
    - username: [email protected]
      role: owner
servers:
  - server: shop
    type: duckdb
    database: ./data/shop.duckdb
    schema: main
schema:
  - name: orders
    physicalName: orders_v2
    physicalType: table
    properties:
      - name: order_id
        logicalType: string
        required: true
        unique: true
        description: Order number, ORD- followed by five digits.
      - name: customer_id
        logicalType: integer
        required: true
      - name: ordered_at
        logicalType: timestamp
        required: true
        description: When the customer placed the order, in UTC.
      - name: amount_gross
        logicalType: number
        required: true
        description: Order value in euros, including VAT, before any refund.
        quality:
          - type: sql
            description: No negative amounts.
            query: SELECT COUNT(*) FROM {table} WHERE {column} < 0
            mustBe: 0
      - name: amount_net
        logicalType: number
        required: true
        description: Order value in euros after refunds; 0 for a refunded order.
      - name: status
        logicalType: string
        required: true
        quality:
          - metric: invalidValues
            arguments:
              validValues: [paid, refunded]
            mustBe: 0
      - name: loaded_at
        logicalType: timestamp
        required: true
        description: When this row was published, in UTC.
    quality:
      - metric: rowCount
        mustBeGreaterThan: 0
slaProperties:
  - property: freshness
    element: orders.loaded_at
    value: 6
    unit: h
Terminal
datacontract breaking orders.odcs.yaml orders-v2.odcs.yaml | sed '/^Details/,$d'
echo "exit code: ${PIPESTATUS[0]}"
Expected output
Perspective: consumer
Summary
[ 2 Error ]  [ 4 Info ]
╭──────────┬─────────┬───────────────────────────────────────╮
│ Severity │ Change  │ Field                                 │
├──────────┼─────────┼───────────────────────────────────────┤
│ INFO     │ Updated │ description                           │
│ ERROR    │ Updated │ schema.orders                         │
│ ERROR    │ Removed │ schema.orders.properties.amount       │
│ INFO     │ Added   │ schema.orders.properties.amount_gross │
│ INFO     │ Added   │ schema.orders.properties.amount_net   │
│ INFO     │ Updated │ version                               │
╰──────────┴─────────┴───────────────────────────────────────╯

exit code: 1

Two changes are errors: the removed amount, and the changed schema.orders, because the data moved to another table. The new properties and the new version number are only information. This is why 2.0.0 is a major version, and why version 1 must stay available while consumers move.

07. Step 6: Publish version 2 next to a deprecated version 1

Replace the job with the version below. It builds orders_v2, then rebuilds orders from it with exactly the old columns and meaning. Then mark version 1 as deprecated in its contract: set status: deprecated, tell consumers what to use instead, and add an endOfLife service level with the date after which version 1 stops being published. The time until that date is the deprecation window. Announce it to the consumers listed for the table.

Python · export_orders.py (version 2)
"""The checkout team's job, version 2: publish orders v2, and v1 until it retires."""
import duckdb

con = duckdb.connect("data/shop.duckdb")
con.execute("""
    create or replace table orders_v2 as
    select order_id, customer_id, ordered_at, amount_gross,
           case when status = 'refunded' then 0 else amount_gross end as amount_net,
           status, (current_timestamp at time zone 'UTC') as loaded_at
    from (
        select 'ORD-' || lpad(i::text, 5, '0') as order_id,
               1 + (i * 37) % 400 as customer_id,
               timestamp '2026-11-15 00:00:00' + to_seconds(i * 41) as ordered_at,
               (5 + (i * 53) % 4000 / 20)::decimal(10, 2) as amount_gross,
               ['paid', 'refunded'][1 + (i % 9 = 0)::int] as status
        from range(1, 2001) t(i)
    )
""")
# Version 1 keeps its exact shape and meaning until its end of life.
con.execute("""
    create or replace table orders as
    select order_id, customer_id, ordered_at, amount_gross as amount, status, loaded_at
    from orders_v2
""")
print(con.sql("select count(*) as orders, sum(amount_gross) as gross,"
              " sum(amount_net) as net from orders_v2"))
Changes to orders.odcs.yaml
--- orders.odcs.yaml
+++ orders.odcs.yaml
@@ -5,3 +5,3 @@
 version: 1.0.0
-status: active
+status: deprecated
 domain: checkout
@@ -10,3 +10,6 @@
   usage: Read the orders table after 06:00 UTC. Join to customers on customer_id.
-  limitations: Refunds are flagged in status; refunded amounts are not subtracted.
+  limitations: >-
+    Deprecated. Use version 2.0.0 (table orders_v2), which renames amount to
+    amount_gross and adds amount_net. This version stops being published after
+    its end of life.
 team:
@@ -63,2 +66,4 @@
 slaProperties:
+  - property: endOfLife
+    value: "2027-01-31"
   - property: freshness
Terminal
python export_orders.py
./ci.sh; echo "exit code: $?"
Expected output
┌────────┬───────────────┬───────────────┐
│ orders │     gross     │      net      │
│ int64  │ decimal(38,2) │ decimal(38,2) │
├────────┼───────────────┼───────────────┤
│   2000 │     208050.00 │     184780.95 │
└────────┴───────────────┴───────────────┘

🟢 Data contract is valid. Ran 23 checks.
🟢 Data contract is valid. Ran 26 checks.
exit code: 0

Both contracts pass. Consumers can now check that the two versions agree before they switch.

Terminal
python -c 'import duckdb; db = duckdb.connect("data/shop.duckdb", read_only=True)
print(db.sql("select (select sum(amount) from orders) as v1_amount,"
             " (select sum(amount_gross) from orders_v2) as v2_amount_gross"))'
Expected output
┌───────────────┬─────────────────┐
│   v1_amount   │ v2_amount_gross │
│ decimal(38,2) │  decimal(38,2)  │
├───────────────┼─────────────────┤
│     208050.00 │       208050.00 │
└───────────────┴─────────────────┘

08. Step 7: Retire version 1 after the window

When the end-of-life date passes, check who still reads orders before you stop publishing it. Lineage events (the next lesson) and the warehouse's access logs both show the jobs and people that read a table. Contact the remaining readers, then remove the step that builds orders, set the old contract's status to retired and drop the table. Keep the retired contract in version control: it documents what the data meant while it existed.

09. Check your understanding

Practice check · 3 questions

Contracts in CI

Each question changes one detail of the lab. Pick the best answer.

Q1. In step 5, `breaking` reports `ERROR Updated schema.orders`. Which difference between the two contracts causes that line?
Q2. A teammate writes `loaded_at` as `current_timestamp::timestamp` on a server set to UTC+7. Data loaded 10 hours ago is tested against the 6-hour rule. What happens?
Q3. Consumers ask for a new optional column, `channel`, in version 1. How should the checkout team release it?
Practice only: not graded, not saved, not evidence.

10. Apply it

This is the contract part of the skill practice. For the main output of a pipeline you built:

  • Write an ODCS contract with owner, descriptions for every property, at least three quality rules and a freshness service level, and make the pipeline's CI run datacontract test against a disposable copy.
  • Add a CI step that compares a changed contract with the version on your main branch using datacontract breaking, and fails unless the major version was raised.
  • Write a short deprecation notice for a breaking change: what changes, the new version, the end-of-life date, and how consumers can check their migration.

11. Review prompt

Without looking back, explain: what each part of an ODCS contract promises and why semantics belong in it; why the contract is tested in the producer's CI rather than by consumers; how datacontract breaking decides that a change needs a new major version; and the steps of a breaking change, from the new version to retiring the old one.

References

Your progress

Reading state

Sign in to save that you finished reading this lesson.

Sign in to mark reading complete

Prove this skill

10 questions · about 8 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.
Take the skill check