Insights · Data engineering

Proving a pharmacy feed migration without exporting patient data

The usual way to prove a migration is to put the old and new outputs side by side. With patient data, you often can't: the rows aren't allowed to leave the secure environment.

A US specialty pharmacy sent a partner hub patient status and dispense updates several times a day. The file came from four SQL Server stored procedures with thousands of lines of status mapping, and it had to move to Azure Databricks without a single row or value changing. Here is how we rebuilt and proved it.

Prove the business rule before writing the pipeline

The feed's population came down to one rule: which patients are enrolled and consented. Before building anything, we tested that rule against tens of thousands of historical legacy rows. If the rule is wrong, every later check is measuring the wrong thing.

Build in pieces a reviewer can check

Instead of one giant query, the new feed is a set of views, one per subject: population, dispense, prescription, prescriber, insurance and claims. The final output assembles 165 columns in the order the partner's specification requires. A reviewer can check one subject at a time.

Level 1: reconcile by key

Every legacy record ID is matched to the new output, and three numbers are counted, not eyeballed: rows missing from the new feed, rows that shouldn't be there, and rows whose key fields differ.

Level 2: fingerprint every column

Key-level checks don't prove the other 160-odd columns. To compare those without moving values, each column is reduced to a hash on both sides, in the same deterministic order:

SELECT sha2(concat_ws('|', transform(
         array_sort(collect_list(struct(record_id, CAST(status_code AS STRING) AS v))),
         x -> coalesce(x.v, '<null>'))), 256) AS status_code_fingerprint
FROM feed

Sorting by record ID inside the expression matters: Spark doesn't keep row order through a subquery, so an unsorted list could hash differently on each run. Compute the equivalent fingerprint over the legacy output, and only the fingerprints leave each side. Equal hashes mean the column matches exactly, nulls included. A different hash points straight at the column to investigate, without anyone seeing patient values. (For very large tables, fingerprint per partition or per day rather than the whole column.)

The trap: a test environment that lies

Early comparisons showed missing data for some patients. It looked like a pipeline bug. It was the test environment, which simply lacked those patients' records. Reconciling in production, read-only, separated real differences from gaps in the test data.

The result

In production, all 163 rows matched legacy on record ID and status codes, with no rows wrongly added or dropped, across 165 columns rebuilt in specification order.

Migrating stored procedures to Databricks? See what a plain translation gets wrong, or talk to us about legacy migration. Or book a free 30-minute call.