Skip to content
VerificationWould you notice if your agent drifted in production?Explore the 5-stage harness›
Framework Architecture

Spec-Diff Database Migrations

This guide explains how to generate, review, and apply deterministic PostgreSQL database migrations from TypeSpec schema diffs using aegil migrate.

In Aegil, your TypeSpec domain models (specs/<feature>/*.tsp) are the single source of truth for the database schema. When you change entities or fields in your specs:

  1. aegil migrate computes the AST diff between your current specs and the latest committed snapshot in migrations/.
  2. It generates an incremental, deterministic PostgreSQL migration script (migration.sql) and a new schema snapshot (snapshot.json).
  3. You review and commit both files to version control alongside your code.

Whenever you add, modify, or remove an @entity model or field in your specs:

Terminal window
aegil migrate --name add_ticket_priority

Aegil calculates the schema diff and commits a new migration directory under Zone 2:

migrations/
0001_initial/
snapshot.json # JSON AST of the schema at this version
migration.sql # PostgreSQL DDL statements
0002_add_ticket_priority/
snapshot.json
migration.sql

Both snapshot.json and migration.sql are committed to your git repository alongside your code.


Guarding against destructive changes (--allow-destructive)

Section titled “Guarding against destructive changes (--allow-destructive)”

If a spec edit would result in data loss (such as dropping a table, removing a column, or altering a column type incompatibly), Aegil halts with ERR_DESTRUCTIVE_MIGRATION:

ERR_DESTRUCTIVE_MIGRATION: Refusing to generate a destructive migration without --allow-destructive:
- Dropping column 'legacy_notes' from table 'tickets'

To proceed intentionally, pass --allow-destructive:

Terminal window
aegil migrate --name drop_legacy_notes --allow-destructive

If you edit models in specs/<feature>/*.tsp and attempt to build or verify without committing a migration, Aegil detects that the committed migration history is out of date:

ERR_SCHEMA_DRIFT: Specifications have drifted from committed migrations.
Remediation: Run `aegil migrate --name <label>` to commit a migration for the spec changes.

This guarantees that database migrations are never forgotten before code reaches CI/CD or production.


During early local development, you may want to wipe your local database container and re-apply all migrations from scratch:

Terminal window
aegil run --reset-db

This safely removes the Docker volume, spins up a fresh container on port 8090, applies all migrations sequentially, and regenerates local development passwords.


What you see Why What to do
ERR_DESTRUCTIVE_MIGRATION A schema diff drops a table or column. Pass --allow-destructive if the data deletion is intentional.
ERR_SCHEMA_DRIFT Specs were modified without creating a migration. Run aegil migrate --name <label> to commit the migration.
ERR_MIGRATION_NAME_INVALID The migration name contains spaces or capital letters. Use lowercase snake_case (e.g. add_priority).