Query icon
Script icon
Docker icon
If icon
ChatCompletion icon
GoogleGemini icon
Queries icon
Write icon
SlackIncomingWebhook icon
Log icon
Webhook icon
Schedule icon

Automated Data Contract Diff and Schema Migration Generator

Diff data contracts against database tables, generate backward-compatible SQL migrations and dbt models with AI, and validate syntax in Kestra.

Categories
AIData

Data contracts define explicit schema, SLA, and semantic expectations between upstream data producers (application microservices, event streams) and downstream analytics warehouses. As business domains evolve, producers introduce new fields, adjust constraints, or evolve data structures. When schema evolution occurs unannounced, downstream ingestion pipelines crash, dbt models fail to build, and analytics dashboards corrupt.

Traditional remediation requires data engineers to manually inspect database catalogs, write boilerplate ALTER TABLE SQL statements, update corresponding dbt schema.yml model files, and open pull requests by hand. This manual toil causes pipeline downtime and slows down data release velocity.

This blueprint automates the complete data contract enforcement and evolution lifecycle. It ingests declarative contract definitions, discovers live table metadata from PostgreSQL catalogs, computes structured schema diffs, and leverages Kestra's AI plugin with Google Gemini to synthesize safe, backward-compatible SQL migrations and updated dbt model documentation. It automatically verifies generated migration syntax within a transactional dry-run (BEGIN ... ROLLBACK) on PostgreSQL before publishing downloadable artifacts and Slack notifications.

How it works

  1. inspect_target_table_schema (io.kestra.plugin.jdbc.postgresql.Query) queries information_schema.columns on the target PostgreSQL database to inspect existing column names, data types, nullability, and default expressions.
  2. compute_contract_diff (io.kestra.plugin.scripts.python.Script) cross-references the live database schema against the authoritative data_contract specification in Python, identifying newly added fields, type evolutions, and deprecated columns.
  3. evaluate_contract_evolution (io.kestra.plugin.core.flow.If) branches on detected drift:
    • If no differences exist, log_contract_in_sync confirms complete contract alignment.
    • If drift is detected, it proceeds through automated migration generation and validation.
  4. generate_migration_and_dbt_models (io.kestra.plugin.ai.completion.ChatCompletion) prompts Google Gemini with strict JSON schema to generate idempotent, non-locking ALTER TABLE SQL statements and formatted dbt model YAML.
  5. validate_migration_syntax (io.kestra.plugin.jdbc.postgresql.Queries) executes the generated SQL within a transactional dry-run (BEGIN; ... ROLLBACK;) against PostgreSQL, confirming compiler syntax validity without mutating production data.
  6. persist_migration_sql, persist_dbt_schema, and persist_migration_summary (io.kestra.plugin.core.storage.Write) store the generated migration code and documentation in Kestra internal storage.
  7. notify_slack_summary dispatches an operational card to Slack with the migration preview and artifact download links.

What you get

  • Automated discovery of schema discrepancies between data contracts and live database tables.
  • AI-generated, backward-compatible PostgreSQL migration SQL using non-blocking defaults and idempotent syntax.
  • Updated dbt schema.yml model specifications with column descriptions and tests ready for version control.
  • Safe syntax validation via transactional dry-runs against the live PostgreSQL compiler before release.
  • Downloadable SQL and YAML artifacts stored directly in Kestra execution outputs.

Who it's for

Data Engineers, Analytics Engineers, and Platform Architects implementing Data Contracts and Data Mesh architectures.

Why orchestrate this with Kestra

Coordinating data contract enforcement requires bridging relational database catalogs, contract JSON schemas, LLM generation with structured output schemas, transactional compiler validation, and artifact storage. Fragile ad-hoc CLI scripts lack execution tracking, expose database secrets, and cannot provide visual execution audits. Kestra unifies SQL discovery, Python computation, Google Gemini AI tasks, and artifact persistence into an auditable, reusable pipeline.

Pitfalls

  • Destructive Column Drops: Upstream contracts may omit historical columns that are still read by legacy queries. The diff logic flags omitted columns as deprecated rather than generating DROP COLUMN statements, preventing accidental data loss.
  • Exclusive Lock Contention: Running heavy ALTER TABLE operations on multi-gigabyte tables can block concurrent readers and writers. Ensure added columns use PostgreSQL 11+ fast-default additions (ADD COLUMN ... DEFAULT ...) rather than running full-table rewrites.
  • Strict Transaction Rollback: The validation task executes inside BEGIN ... ROLLBACK to ensure zero unintended modifications occur during dry-run validation.

Prerequisites

  • PostgreSQL 12+ database holding target analytical or operational tables.
  • Google Gemini API key with access to Gemini 2.5 Flash.
  • Slack incoming webhook endpoint for operational alerts.

Secrets

  • POSTGRES_PASSWORD: Administrative password for target PostgreSQL database connection.
  • GEMINI_API_KEY: API key for Google Gemini AI completion tasks.
  • SLACK_WEBHOOK_URL: Slack incoming webhook URL for contract notifications.
  • CONTRACT_WEBHOOK_KEY: Secret authentication key for Git-triggered contract webhooks.

Quick start

  1. Configure POSTGRES_PASSWORD, GEMINI_API_KEY, and SLACK_WEBHOOK_URL in your Kestra namespace secrets.
  2. Click Execute in the Kestra UI with default contract inputs to inspect live drift against your orders table.
  3. Download the generated migration SQL and dbt schema artifacts from the Execution Outputs tab.
  4. Connect the contract_webhook_trigger to your Git repository to trigger automated migration generation upon contract updates.

How to extend

  • Add automated Git branch creation and GitHub PR opening using io.kestra.plugin.core.http.Request.
  • Point the schema inspection query to Snowflake, BigQuery, or DuckDB catalogs.
  • Integrate with dbt Cloud or local dbt run tasks to execute models immediately following migration cutover.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.