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 findingEntry 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.00negatives 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:

RuleViolations in 28 daysDecision
`region` null rate at most 5%0block
`amount` at most 50,000.00one, a confirmed 62,000.00 bulk order on Oct 3raise the bound in v1.1.0, then block
at least 300,000 rows a dayone, on Oct 5block, with Oct 5 covered by an `orders_legacy` outage annotation
`order_id` unique3stay 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?

One job, 2 contracts, 2 gates. Each consumer moves from the v1 path to the v2 path on its own dated plan.
  • 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?

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.