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

Refresh staging with a referentially complete, pseudonymized production subset

Build a staging copy from production with Kestra and DuckDB. Keep foreign keys intact, pseudonymize PII with a keyed hash, verify no raw value leaked.

Categories
DataInfrastructure

Teams want staging data that behaves like production, but copying production leaks personal data, and random sampling breaks foreign keys: orders without customers, items without orders. This flow builds a referentially complete subset and replaces personal data with keyed, deterministic pseudonyms. It refreshes staging only after it has proved three things:

  • No orphan row in any table.
  • No raw personal value anywhere, including inside free text.
  • No pseudonym collisions.

The same email gets the same pseudonym in the customer table and inside ticket bodies, so joins and searches still work in staging.

It runs with no setup and no network. DuckDB generates a shop database of 5,000 customers, 20,000 orders, about 42,000 order items and 3,000 support tickets whose bodies quote the customer's email and phone, which is where pseudonymization usually misses. Run it with scrub_free_text: false to watch the gate block the refresh.

This blueprint was created by zkasuran.

How it works

  1. salt (io.kestra.plugin.core.flow.If) creates a secret salt in the KV store on the first run, or when refresh_salt is set. The salt keeps pseudonyms stable across refreshes and impossible to recompute from source values.
  2. subset_and_verify (io.kestra.plugin.jdbc.duckdb.Queries):
    • builds the source tables (replace them with ATTACH to your database),
    • keeps customers whose keyed SHA-256 bucket is under sample_pct. This is deterministic, unlike random(),
    • follows the foreign keys: their orders, then those orders' items, then their tickets,
    • pseudonymizes emails (c_<hash>@example.invalid), names (Customer <HASH>) and phones (the fictional +1-555-01xx range, so a pseudonym is never a real number),
    • replaces the original email and phone inside ticket bodies with the same pseudonyms,
    • writes each table to Parquet and returns one verification row.
  3. verification (io.kestra.plugin.core.output.OutputValues) and log_verification report the counts on every run.
  4. release_gate (io.kestra.plugin.core.flow.If):
    • Pass: publish_staging (io.kestra.plugin.core.namespace.UploadFiles) replaces staging/shop/*.parquet.
    • Fail: block_refresh (io.kestra.plugin.core.execution.Fail). Staging keeps its previous copy.
  5. weekly (io.kestra.plugin.core.trigger.Schedule) refreshes staging every Sunday.

Inputs

  • sample_pct (INT, default 10): share of customers to keep.
  • scrub_free_text (BOOL, default true).
  • refresh_salt (BOOL, default false): rotate the salt. Every pseudonym, and the sample itself, changes.

Quick start

Run Input Result
1 defaults Published: about 500 customers with their rows (counts depend on the salt). 0 orphans, 0 raw values, 0 collisions. Every ticket mentions the pseudonymized email
2 scrub_free_text: false Blocked: every ticket still contains the real email or phone
3 defaults again Published, byte-identical to run 1
4 refresh_salt: true Published with new pseudonyms and a new sample

Things to know

  • My first version masked phones into the same +1-555-XXXX space as the real numbers, and 260 pseudonyms turned out to be other customers' real phone numbers. The raw-value check caught it. That is why pseudonyms now use the reserved fictional range.
  • The checks only catch values the flow knows about. Free text with personal data that is not in a source column, such as a name typed in a ticket, needs a detector such as Presidio on top.

Secrets

  • SLACK_WEBHOOK_URL: Slack incoming webhook for the errors block when notify_slack is true.

Expected outputs

  • outputs.verification.values: table counts, orphan_rows, raw_in_columns, raw_in_free_text, collisions, linked_tickets.
  • Namespace files staging/shop/customers.parquet, orders.parquet, order_items.parquet, tickets.parquet.
  • KV staging_pseudonym_salt.

Using it with your own database

  • ATTACH 'postgres://...' AS prod (TYPE postgres, READ_ONLY) at the top of the SQL, then select from prod.<table> instead of the generated tables. Credentials go in {{ secret('NAME') }}.
  • Add one CREATE TABLE ... WHERE fk IN (SELECT ...) per child table and one orphan check per foreign key.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.