Skip to content
migration.tools

data · database · verification

How to prove a data migration worked: counts, checksums and diffs

Prove a data migration worked: row counts, aggregates, row hashes and key-based diffs — and how to normalise data so you compare values, not formatting.

By

Updated · first published · 4 min read

Two tables side by side, source and target, with matching row counts, check marks on identical rows and one changed row highlighted.

Key points

  • Reconcile in layers: row counts per table, aggregates on important columns, then a full diff by primary key on critical tables.
  • Compare rows by key, never by position. The same data can come out of two databases in different orders.
  • Normalise before comparing: number formats, booleans, date and time zone formats, NULL versus empty string, and whitespace.
  • When hashing rows, mark NULLs explicitly. Functions like CONCAT_WS skip NULLs, so different rows can produce the same hash.
  • Run the full reconciliation on every rehearsal, not just on cutover day, and keep the source read-only until it passes.

Most data migrations are declared successful when the import finishes without errors. That only proves the target accepted the data. It doesn't prove the data is the same.

Reconciliation is how you find out. It's not complicated, but it has to be deliberate: you decide what "the same" means, measure it on both sides, and explain every difference. This guide describes a method that scales from a spreadsheet import to a multi-terabyte database move.

Decide what "the same" means

Before comparing anything, write down the rules. Data rarely arrives byte-for-byte identical, and it isn't supposed to:

  • A TINYINT(1) value of 1 in MySQL is true in PostgreSQL.
  • 12.50 may come out as 12.5.
  • A timestamp may be stored in UTC on one side and local time on the other.
  • One system writes an empty string where the other writes NULL.
  • The target may add columns (created_at, migrated_from) that the source never had.

Each of these is a formatting difference, not a data difference — but only if you've decided so in advance. Write the normalisation rules down, apply them to both sides, and anything left over is a real finding.

Layer 1: row counts

Count rows in every table on both sides. It takes seconds and catches the largest failures: a table that didn't load, a batch that was silently skipped, a filter applied by mistake.

SELECT 'customers' AS table_name, COUNT(*) FROM customers
UNION ALL
SELECT 'orders', COUNT(*) FROM orders;

Run the same query on source and target and put the results side by side. Any mismatch is a stop-everything finding. Matching counts are necessary, not sufficient: a migration that duplicates one row and drops another has the right count.

Layer 2: aggregates

For each important table, compute a few aggregates that would change if values changed:

  • Sums of money and quantity columns
  • Minimum and maximum of dates and IDs
  • Counts of distinct values in key and foreign key columns
  • Counts of NULLs in columns that matter
SELECT COUNT(*), COUNT(DISTINCT customer_id), SUM(total_cents),
       MIN(created_at), MAX(created_at), COUNT(*) - COUNT(shipped_at)
FROM orders;

Aggregates catch truncated decimals, shifted time zones (every MIN and MAX moves by the same few hours), and broken relationships. Use integer cents or exact decimals for money sums; summing floating-point values can differ in the last digit between engines.

Layer 3: row hashes

To check every row without moving the whole table, hash each row on both sides and compare the hashes by primary key. In PostgreSQL:

SELECT id, md5(concat_ws('|', id, coalesce(email, '∅'), coalesce(total_cents::text, '∅')))
FROM orders ORDER BY id;

Two details make or break this approach:

  1. NULL handling. CONCAT_WS skips NULL arguments in both MySQL and PostgreSQL, so ('a', NULL, 'b') and ('a', 'b', NULL) produce the same string — and the same hash. Replace NULLs with an explicit marker, as above.
  2. Identical text on both sides. The hash is only comparable if each column is formatted the same way before hashing: same date format, same decimal places, same boolean representation. This is where your normalisation rules from the first step become code.

When the hashes for a key differ, pull that row from both sides and compare it field by field.

Layer 4: a full diff by key

For the tables that matter most — customers, orders, balances, anything with legal or financial weight — compare every field of every row.

Export the table from both systems to CSV with the same columns. The SQL to CSV converter turns INSERT statements from a dump into CSV, the JSON to CSV converter flattens API exports, and the Excel to CSV converter handles spreadsheets. Run both files through the CSV cleaner with the same settings to apply the whitespace and placeholder rules.

Then compare them with the CSV compare tool, giving it the primary key column. Rows are matched by key, not by position, so the files can be in different orders. Every difference is classified:

  • Missing in target: the migration dropped the row.
  • Only in target: the migration created it — a duplicate, or test data that leaked in.
  • Changed: the row exists on both sides, and the report lists each field as old → new.

Columns present only in the source are listed separately, because a column that silently didn't migrate is a finding too.

Explaining the differences

A reconciliation report is finished when every difference has an explanation. For each one, record:

  • What differs (table, key, field)
  • Why (a normalisation rule you missed, a known transformation, a defect)
  • Decision (accepted, fixed in the migration script, fixed in the data)

If a difference is a normalisation issue, add the rule and run the comparison again. If it's a defect, fix the migration — not the target data — so the next run doesn't reintroduce it.

When to reconcile

Not just on cutover day. A good migration is rehearsed several times against fresh copies of production data, and reconciliation runs on every rehearsal. By the final run, the report should contain only differences you've already explained.

On cutover day:

  1. Freeze writes to the source, or capture changes made during the migration window.
  2. Run the migration.
  3. Run all four layers of reconciliation.
  4. Switch over only if the report is clean or fully explained.
  5. Keep the source read-only and available for a defined period, so rolling back is a decision rather than a rebuild.

If you're moving between database engines, the MySQL to PostgreSQL guide covers the conversion side; this method is how you check its result.

Summary

Counts catch missing tables. Aggregates catch damaged columns. Hashes catch changed rows. Diffs tell you exactly what changed. Normalise first, compare by key, and explain every difference before you call the migration done.

About the author

Abdessamad Bettal, full-stack engineer and the developer of migration.tools — free, private, browser-based tools for moving databases, data, websites and content between systems.

LinkedIn GitHub