Schedule icon
Dag icon
Queries icon
Get icon
Log icon
If icon
SlackIncomingWebhook icon
Pause icon
Fail icon
Set icon
Loop icon
OutputValues icon
Assert icon
Delete icon

Versioned database schema migrations with checksum ledger, backup and automatic rollback

Flyway-style schema migrations in Kestra with checksum drift detection, dry run, prod approval, verified backup, automatic rollback and an audit CSV.

Categories
DataInfrastructure

Schema changes are the deployment step that hurts most when it goes wrong: a script edited after it ran on prod, a migration that fails halfway, a rollback nobody has rehearsed. This flow is a small, self-contained migration runner in the style of Flyway. It keeps a schema_history ledger inside the database, refuses to run when an applied script was edited, shows the SQL before anything changes, asks for approval on prod, takes a verified backup, applies each migration in its own transaction and, when something fails, puts the database back where it found it.

The target is a real DuckDB database file kept in Kestra internal storage, with one KV pointer per target (dev, staging, prod). The default migrations are sample data for a small orders schema; replace them with your own.

How it works

  1. checks block the execution before it exists when no migration or no valid verify_checks list is given. The default run is a dry run.
  2. prepare is a Dag: parse_migrations (inline JSON or a bundle of Flyway-named V001__name.sql / U001__name.sql scripts) runs while current_db and read_ledger read the target; plan waits for both. Parsing reports every problem at once: duplicate versions, down scripts without an up script, empty scripts, BEGIN/COMMIT inside a script, ATTACH, scripts that end with a comment.
  3. plan fails fast, before any backup, when an applied migration's SHA-256 checksum changed, when an applied version is missing from the input, or when a new version is older than the head (out of order). Line endings are normalised, so a Windows checkout is not drift.
  4. log_plan prints the pending versions with their up and down SQL. A dry run stops here.
  5. On prod, wait_for_approval pauses (4 hours, behavior: FAIL) with an approve flag and a reason. A rejection ends the run with nothing changed. target_unchanged then checks that the pointer did not move while waiting.
  6. take_backup copies the database and proves the copy opens and has a readable ledger; register_backup stores it under a timestamped KV key with ttl: backup_retention.
  7. apply_migrations (Loop, one at a time) runs the up script and its ledger row in one transaction. A guard statement refuses to apply on top of an unexpected head. Only after the commit does advance_pointer move the KV pointer.
  8. verify_schema runs your assertions plus a ledger-head check. A failed migration or a failed assertion triggers rollback: all down scripts of this run, newest first, in one transaction; if a down script is missing or fails, the pointer goes back to the backup. assert_rollback proves the result matches the backup, then the run fails with a clear message.
  9. finally writes the audit CSV, errors alerts with the failed task and an execution link, and afterExecution reports the outcome, including SLA breaches.

The safety model

Tasks without outputDbFile read a throwaway copy, so they cannot change the target. A change exists only once its transaction committed and the pointer moved. That makes a killed run safe: run it again and the ledger continues where it stopped.

What this blueprint teaches

  • Dag (prepare): explicit dependencies instead of a fixed order; input parsing and ledger reading run side by side.
  • Flow checks (checks): reject an empty migration list before an execution is created.
  • KV ttl (register_backup, remember_failure): backup entries expire after backup_retention; the last failure is kept 30 days and shown by the next run.
  • finally (write_audit): the audit CSV is written after success, dry run, drift, rejection or rollback.
  • sla + afterExecution (max_runtime, notify_outcome): on 2.0.5 an SLA breach fails the run at once and skips finally; afterExecution still reports it (label migration_sla: breached).
  • concurrency (limit: 1, FAIL): a single writer; a second run fails at once instead of queueing behind an approval.
  • Loop transmitFailed: true + allowFailure: true (apply_migrations): stop at the first failure but continue to the rollback instead of jumping to errors. Migrations are ordered, so per-item isolation is deliberately not used.
  • Assert (assert_rollback): a hard invariant; if the rollback cannot be proven, errors asks for manual intervention.

DuckDB, SQLite and Postgres

The SQLite plugin offers the same pattern: swap io.kestra.plugin.jdbc.duckdb.Queries with databaseUri for io.kestra.plugin.jdbc.sqlite.Queries with sqliteFile. For Postgres, drop the pointer and outputDbFile, use io.kestra.plugin.jdbc.postgresql.Queries with a JDBC URL from secrets, back up with pg_dump in a script task and restore with pg_restore. The ledger, checksums, plan, approval, per-migration transactions, down scripts and checks work unchanged, because Postgres DDL is transactional (except CREATE INDEX CONCURRENTLY, which needs its own non-transactional step).

What you get

  • A schema_history ledger: version, checksum, applied_at, execution id and the down script.
  • Fail-fast checksum drift detection, and a nightly prod dry run.
  • Rollback by down scripts, or a backup restore when one is missing or fails.
  • An audit CSV (audit_csv) with one row and status per migration, and a summary output.

Who it's for

  • Platform and data engineers who own an embedded or analytics database.
  • Teams that want reviewed, approved schema changes without a separate migration server.

Why orchestrate this with Kestra

Approvals, backups, retries, timeouts, alerts and the audit are declared next to the SQL, and every run is its own audit trail.

Prerequisites

  • Kestra 2.0 with the DuckDB and Slack plugins. No external database is needed.

Secrets

  • SLACK_WEBHOOK_URL: approval requests, failure alerts and outcomes (optional; everything is also in the logs).

Quick start

  1. Execute with the defaults: a dry run on dev that lists V001 to V003 and their SQL.
  2. Run again with dry_run: false, then once more to see the no-op.
  3. Add a V004 to migrations, or upload a bundle in migrations_file, and run on prod to try the approval.

How to extend

  • Keep the bundle in Git and sync it as a namespace file.
  • Add data checks to verify_checks, such as row counts that must hold.
  • Copy the audit CSV to object storage or your change-management tool.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.