Discovering Invariants & Reconciling Dual-Run Migrations
Using profile, approve, and reconcile to document implied database rules and catch quiet parity drift before cutover.
Core database migrations rarely fail loudly on row counts. A legacy payment service and a rewritten Rust or Go replacement can both finish migrating 50,000 accounts with zero SQL errors, yet subtle rounding differences, dropped status updates, or missed reversals leave hundreds of balances off by pennies weeks after cutover.
Writing every validation contract by hand also stalls teams working with older schemas where half the implicit constraints live in application code and never appear in database CHECK declarations. CausalFoundry closes both gaps with three connected commands: profile to discover candidate rules from existing databases, approve to review and promote them into active enforcement, and reconcile to check source and target outputs row by row in DuckDB.
Step 1: Infer Candidates with profile and Promote via approve
Running causalfoundry profile inspects table distributions and logs candidate invariants carrying confidence scores, observed row counts, and counterexample counts. Nothing is enforced silently behind your back until an engineer reviews candidates through causalfoundry approve (interactively or via CI thresholds).
# Introspect schema and draft candidate config rules causalfoundry profile \ --url "postgres://readonly@prod-replica:5432/billing" \ --schema public \ --output-schema schema.json \ --output-manifest manifest.yaml # Review or auto-promote rules meeting strict confidence bounds causalfoundry approve \ --config manifest.yaml \ --min-confidence 0.98 \ --max-counterexamples 0 \ --non-interactive
Rules promoted to Approved become mandatory checks inside generate, shadow, verify, and sandbox. Candidate rules still awaiting human sign-off stay quarantined from breaking builds unless you explicitly test them with --include-candidates.
Step 2: Catch Row & Rounding Drift with reconcile
When teams run a legacy service alongside a replacement target over identical inputs (seeded via generate or mirrored via shadow), causalfoundry reconcile loads both CSV outputs into an in-memory DuckDB engine, aligns rows on your chosen primary key, checks missing records in either direction, and compares numeric and string columns within an explicit tolerance.
causalfoundry reconcile \ --source ./legacy_output \ --target ./v2_output \ --table accounts \ --key account_id \ --columns balance,account_status \ --tolerance 0.001 \ --output reconcile_report.json
The console summary reports Source Rows, Target Rows, missing IDs in either dataset, mismatching row count, an overall Parity Score, and up to five sample discrepancy records so engineers can inspect failing account IDs immediately.
The Adversarial Stress-Test: Five Critical Inquiries
1. What is the strongest argument against this approach?
Comparing exported snapshots at the end of a test window checks destination state convergence, yet it does not automatically surface transient mid-flight lock contention or latency spikes inside the migration pipeline itself.
2. Under what circumstances would this fail?
Automated profiling on skewed or partial staging tables can emit false-positive candidate rules (for example, inferring a column is always positive simply because a test slice lacked refunds). Promoting candidates blindly via --all without inspecting counterexamples will reject legitimate production transactions later.
3. What would a skeptical engineer or migration lead object to?
Large enterprise tables spanning tens of millions of rows require dumping or partitioning datasets before local DuckDB comparison runs. Teams migrating multi-terabyte tables typically run reconcile over deterministic high-risk cohorts or daily cutover shards first instead of unsharded full-table exports.
4. Which alternative deserves more consideration?
Streaming shadow proxies that compare synchronous HTTP responses or asynchronous consumer outputs continuously in production catch latency regressions alongside state differences, especially during blue-green traffic splits.
5. What am I assuming that may not be true?
Row-by-row parity checks assume a stable join key links legacy and replacement schemas cleanly. Schema rewrites that merge tables or mint brand-new UUIDs on insert need a mapping column exported before calling reconcile.
One-Line Synthesis
Safe database cutovers require explicit governance over discovered rules and deterministic key-by-key parity checks, because row counts alone never surface rounding drift or silent state divergence.
Tags: #CausalFoundry #Migrations #Reconciliation #DuckDB #DataGovernance
Audit Your Next Migration
Combine candidate profiling, human-in-the-loop approval, and DuckDB parity checks before cutover.
Explore CausalFoundry