Spec-Diff Database Migrations
This guide explains how to generate, review, and apply deterministic PostgreSQL database migrations from TypeSpec schema diffs using aegil migrate.
Overview
Section titled “Overview”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:
aegil migratecomputes the AST diff between your current specs and the latest committed snapshot inmigrations/.- It generates an incremental, deterministic PostgreSQL migration script (
migration.sql) and a new schema snapshot (snapshot.json). - You review and commit both files to version control alongside your code.
Generating migrations (aegil migrate)
Section titled “Generating migrations (aegil migrate)”Whenever you add, modify, or remove an @entity model or field in your specs:
aegil migrate --name add_ticket_priorityAegil 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.sqlBoth 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:
aegil migrate --name drop_legacy_notes --allow-destructiveSchema drift detection (ERR_SCHEMA_DRIFT)
Section titled “Schema drift detection (ERR_SCHEMA_DRIFT)”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.
Resetting the local database (--reset-db)
Section titled “Resetting the local database (--reset-db)”During early local development, you may want to wipe your local database container and re-apply all migrations from scratch:
aegil run --reset-dbThis safely removes the Docker volume, spins up a fresh container on port 8090, applies all migrations sequentially, and regenerates local development passwords.
What could go wrong?
Section titled “What could go wrong?”| 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). |
Next steps
Section titled “Next steps”- Learn The 3-Step Agent Protocol.
- Learn Running & Debugging Locally.
- Learn 5-Stage Verification Harness.