Contracts on a Legacy Pipeline
On a new dataset the contract comes first and the data is built to match. A legacy table reverses that order. orders_legacy has 23 columns and has loaded nightly for 6 years. It has no written owner, and nobody has listed its readers. A clean contract enforced on day one would block tonight's load. The rollout runs in 4 steps, and each produces something the next one needs.
Step 1: Write down what the table is today
Contract v1.0.0 describes the table as it behaves, quirks included, so it passes on its first night and gives every later step a baseline. Profile 90 days to find the facts.
SELECT
count(*) AS row_count,
count_if(region IS NULL) * 100.0 / count(*) AS region_null_pct,
min(amount) AS min_amount,
max(amount) AS max_amount,
array_agg(DISTINCT status) AS status_values
FROM legacy.orders_legacy
WHERE order_date BETWEEN DATE '2026-06-28' AND DATE '2026-09-25'| Profile finding | Entry in contract v1.0.0 |
|---|---|
| `region` null in 4.1% of rows | `region` optional, null rate at most 5% |
| `status` has 7 values, with both `CANCELLED` and `canceled` | all 7 values allowed, as written |
| `amount` is a `double` in dollars, minimum -310.00 | negatives allowed; meaning: refunds are stored as negative orders |
| `amount` maximum 48,200.00 | `amount` at most 50,000.00 |
Step 2: Inventory every consumer
A breaking change cannot be planned without knowing whom it breaks. Query history lists who read the table and which columns they touched. A 90-day search found 13 readers; the 14th, an annual audit export that reads once a year, appeared only when the search covered 13 months. Of the 14, 9 are scheduled jobs and 3 are dashboards; the other 2 are analysts' notebooks. Of those, 5 filter on lowercase canceled only, so normalizing status in place would drop cancellations from their counts without any error. Record an owner and a contact for each consumer; step 4 needs them.
Step 3: Observe before enforcing
Run every v1 rule in warn mode for 28 days and record each violation. A rule gets blocking authority once it has run clean or every violation has an explanation. The window from Sep 26 to Oct 23 produced this:
| Rule | Violations in 28 days | Decision |
|---|---|---|
| `region` null rate at most 5% | 0 | block |
| `amount` at most 50,000.00 | one, a confirmed 62,000.00 bulk order on Oct 3 | raise the bound in v1.1.0, then block |
| at least 300,000 rows a day | one, on Oct 5 | block, with Oct 5 covered by an `orders_legacy` outage annotation |
| `order_id` unique | 3 | stay warn until the upstream retry replaces its partition instead of appending, then block after a clean window |
Step 4: Migrate behind a new major version
The cleanups go into contract v2.0.0 on a new table, orders_v2, under the same evolution policy as checkout's contract. There amount_cents becomes a bigint and status is normalized to 6 values. region is now required and filled from the delivery address. The legacy job writes both tables from the same input, each behind its own gate. Version 1 is frozen and receives fixes only. Each consumer from the inventory gets a dated move to v2, and orders_legacy is retired once every inventoried consumer has moved and the 30-day notice has passed.
Contract v2.0.0 maps both CANCELLED and canceled to CANCELLED. Of the 14 consumers, 5 filter on lowercase canceled. What keeps their cancellation counts right through the migration?
- Normalize the values in orders_legacy tonight, since the change is already documented in v2.0.0.
- Add a rule on the v2 gate that blocks any row with lowercase canceled, so the 5 consumers see a failure.
- Leave orders_legacy as it is, and have each of the 5 change its filter to CANCELLED during its dated move to orders_v2.
- Keep both spellings in v2 so no consumer has to change anything, and retire v1 when the notice ends.
- Nightly legacy job (transform, orchestrator)
- Gate v1 (quality gate, contract v1.1.0); on error: quarantine batch
- Gate v2 (quality gate, contract v2.0.0); on error: quarantine batch
- orders_legacy (storage, frozen, retiring)
- orders_v2 (silver, new table)
- Consumers on v1 (consumer, not yet moved)
- Consumers on v2 (consumer, moved on schedule)
- Nightly legacy job -> Gate v1
- Gate v1 -> orders_legacy
- orders_legacy -> Consumers on v1
- Nightly legacy job -> Gate v2
- Gate v2 -> orders_v2
- orders_v2 -> Consumers on v2
At week 6 of the move, 8 of the 14 consumers read orders_v2. A producer pull request renames region to market and raises the contract from 2.0.0 to 2.1.0. The volume check on orders_v2 has fired 6 times in those 6 weeks, against a budget of about 5 per 90 days. Which plan follows this lesson?
- Let CI fail the rename and ship market as an added optional field in 2.1.0; backtest the volume check with annotations and retune k to the budget.
- Approve the rename, since 2.1.0 is a new version; then raise the volume check to k = 4.0 so its alerts drop under the budget.
- Let CI fail the rename; then add retries and concurrency to the volume check's task so its 6 alerts resolve faster.
- Approve the rename after the 6 remaining consumers move to v2, and return the volume check to warn mode for 28 days.
About this lesson
This section is part of the Data Quality: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.