Queries icon
Parallel icon
Script icon
Process icon
If icon
Log icon
Pause icon
Query icon
Schedule icon

Database Migration Safety Gate with DuckDB

Inspect SQL migrations in parallel, calculate risk scores, and gate high-risk DDL with human approval before applying to DuckDB.

Categories
DataInfrastructure

Accidental table drops, index creation, and unvetted schema modifications remain among the leading causes of production database outages. This blueprint establishes a shift-left safety gate in front of your database migrations: it inspects DDL payloads concurrently across multiple risk dimensions, computes a transparent risk score, automatically applies safe changes, and halts high-risk operations for explicit human sign-off via an interactive Pause task.

How it works

  1. prepare_demo_database (io.kestra.plugin.jdbc.duckdb.Queries) initializes an embedded DuckDB database (/tmp/kestra-migration-safety-gate.duckdb) containing representative customers and orders tables.
  2. migration_checks (io.kestra.plugin.core.flow.Parallel) concurrently executes three independent static analysis checks using Python on the Process runner:
    • destructive_change_check: Scans the SQL payload for destructive DDL commands (DROP TABLE, DROP COLUMN, TRUNCATE, and DELETE FROM).
    • compatibility_check: Identifies breaking contract changes and schema-change risks (ALTER COLUMN TYPE, SET NOT NULL, ADD CONSTRAINT, and CREATE INDEX).
    • rollback_check: Validates whether a corresponding rollback SQL statement was supplied.
  3. risk_assessment (io.kestra.plugin.scripts.python.Script) consumes the parallel findings, computes a deterministic weighted score, and classifies the migration into LOW, MEDIUM, or HIGH risk.
  4. route_migration (io.kestra.plugin.core.flow.If) branches on the risk classification:
    • Low and Medium Risk: Automatically executes the DDL via apply_low_risk_migration and validates schema modifications with verify_schema_after_auto_apply (DESCRIBE customers;).
    • High Risk: Logs an alert and pauses execution at human_review (io.kestra.plugin.core.flow.Pause) waiting for reviewer authorization.
  5. check_approval_decision (io.kestra.plugin.core.flow.If) inspects the reviewer's verdict from outputs.human_review.onResume.approved:
    • If approved: Applies the migration to DuckDB (apply_approved_migration), verifies schema updates (verify_schema_post_approval), and logs the reviewer's approval notes.
    • If rejected: Logs rejection details without executing the migration SQL.

Risk model

The migration receives a weighted risk score:

  • Destructive operation: +4
  • Compatibility risk: +2
  • Missing rollback SQL: +1

By default:

Score Risk level Behaviour
0-1 LOW Automatically apply
2-3 MEDIUM Automatically apply
4+ HIGH Require human approval

The HIGH-risk threshold can be customized with the min_high_risk_score input.

What you get

  • Automated multi-vector static analysis running in parallel before any SQL reaches a database engine.
  • A deterministic risk-scoring model that separates routine additive changes from breaking DDL.
  • Self-contained execution using embedded DuckDB without requiring an external database or service credentials.
  • Server-side human approval workflow via Pause with typed onResume inputs capturing the approval decision and reviewer comments.
  • Verified schema introspection (DESCRIBE) proving whether the migration executed successfully.

Who it's for

  • Platform and DevOps engineers designing CI/CD deployment pipelines that include database schema migrations.
  • Database Administrators (DBAs) seeking an auditable, automated gate to review risky DDL before production rollout.
  • Backend engineering teams wanting fast, automated clearance for safe migrations while requiring explicit human approval for destructive schema changes.

Why orchestrate this with Kestra

A simple shell script or CI step cannot easily coordinate parallel inspection jobs, retain structured state across workflow branches, or hold an execution in a durable, pause-and-resume approval state without consuming worker compute. Kestra provides native parallel orchestration, server-side pause gates with typed interactive resume schemas, centralized execution logs for compliance auditing, and easy portability to production database engines like PostgreSQL, MySQL, or Snowflake.

Prerequisites

  • A running Kestra instance with the DuckDB JDBC plugin (io.kestra.plugin.jdbc.duckdb) available on the worker.
  • Python 3 installed in the worker's environment for the Process runner.

Secrets

No secrets are required. The workflow runs against an embedded DuckDB database file with local Python analysis, requiring zero external service credentials.

Quick start

  1. Add this blueprint to your Kestra instance.
  2. Run with default inputs (ALTER TABLE customers ADD COLUMN phone VARCHAR;) to observe a low-risk migration automatically apply and verify.
  3. Run with a high-risk change (e.g. ALTER TABLE customers DROP COLUMN email; or DROP TABLE customers;).
  4. Observe the execution pause at the human_review task, click Resume in the Kestra UI, supply your approval decision and comments, and inspect the final schema.

How to extend

  • Replace the embedded DuckDB tasks with the appropriate database plugin and connection configuration for your target database.
  • Inject production credentials through Kestra secrets rather than hardcoding them in the flow.
  • Add notification tasks (Slack, Discord, PagerDuty, or Email) before human_review to alert on-call reviewers when high-risk DDL pauses execution.
  • Trigger this flow automatically from GitHub Actions or GitLab CI webhooks whenever a pull request modifies SQL migration directories.
  • Customize risk weighting and add engine-specific heuristics, such as PostgreSQL CONCURRENTLY index enforcement or MySQL lock-timeout rules.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.