Queries icon
OutputValues icon
Log icon
If icon
Set icon
UploadFiles icon
Fail icon
SlackIncomingWebhook icon
Schedule icon

Prove a migrated table matches its source, row by row, before cutover

Compare a migrated table with its source in Kestra and DuckDB. Bucket checksums narrow the diff, then every differing row and column is named.

Categories
DataInfrastructure

"The row counts match" is how broken migrations get approved. A count says nothing about a rounded amount, a timestamp shifted by a timezone or one row missing and another one extra. This flow compares a source and a target table the way data-diff tools do, so it stays fast on large tables:

  1. Checksum per bucket. Rows are split into bucket_count buckets by key. For each side the flow computes the row count and an XOR of row hashes over every column.
  2. Row diff only where needed. Only buckets whose checksums differ are compared row by row.
  3. Name the difference. Each differing row is MISSING_IN_TARGET, EXTRA_IN_TARGET or CHANGED, with the changed columns and their old and new values.

The cutover is approved only when the number of differing rows is at most max_diff_rows.

It runs with no setup and no network. DuckDB generates 200,000 orders and a migrated copy with the drift real migrations produce: three missing rows, one extra row, an amount rounded to whole units and a timestamp shifted by two hours.

This blueprint was created by zkasuran.

How it works

  1. diff (io.kestra.plugin.jdbc.duckdb.Queries) builds both tables, computes bucket_sums with hash() and bit_xor(), diffs the differing buckets with a FULL OUTER JOIN on the key, writes differences.csv and returns one summary row.
  2. result and log_result report rows on each side, buckets compared, rows scanned and every difference.
  3. cutover_gate (io.kestra.plugin.core.flow.If):
    • Pass: record_approval (io.kestra.plugin.core.kv.Set) stores the approval in KV.
    • Fail: publish_differences (io.kestra.plugin.core.namespace.UploadFiles) writes migration/orders-differences.csv, then block_cutover (io.kestra.plugin.core.execution.Fail).
  4. alert_flow_failure (io.kestra.plugin.slack.notifications.SlackIncomingWebhook, in errors) sends a Slack alert when the run fails and notify_slack is true, which includes a blocked cutover.
  5. hourly_during_migration (io.kestra.plugin.core.trigger.Schedule), disabled, re-checks while both systems run in parallel.

Quick start

Run Input Result
1 defaults Blocked. 6 of 64 buckets differ, so 18,750 of 200,000 rows were compared. Found 3 missing (40011, 40012, 151009), 1 extra (200007), 87654 amount 203.51 to 204.0, 123456 created_at shifted by 2 hours
2 inject_drift: false Approved. 0 of 64 buckets differ, so 0 rows needed a row-level comparison

Inputs

  • bucket_count (INT, default 64): more buckets mean fewer rows compared per differing bucket.
  • inject_drift (BOOL, default true): demo only.
  • max_diff_rows (INT, default 0).

Secrets

  • SLACK_WEBHOOK_URL: Slack incoming webhook used by the errors block when notify_slack is true. In Kestra OSS, provide it as the base64-encoded environment variable SECRET_SLACK_WEBHOOK_URL.

Expected outputs

  • outputs.result.values.row: source_rows, target_rows, buckets, differing_buckets, rows_compared, differing_rows, missing, extra, changed, sample.
  • Namespace file migration/orders-differences.csv when blocked.
  • KV orders_migration_cutover when approved.

Using it with your databases

  • ATTACH both databases with DuckDB extensions, for example ATTACH 'postgres://...' AS old (TYPE postgres, READ_ONLY) and the same for the new one, then replace source and target with old.public.orders and new.public.orders.
  • When the engines differ, cast values to a common type in the hash, for example round(amount, 2) or created_at AT TIME ZONE 'UTC', so formatting differences are not reported as data differences.
  • For tables too large to pull through DuckDB, compute the bucket checksums inside each database and diff only the differing buckets.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.