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

Schema Evolution and Migrations

This guide explains how to evolve your application database schemas safely over time, generate deterministic PostgreSQL migrations from TypeSpec changes, and protect against unintended data loss in production.

  • An existing Aegil project with committed migrations in migrations/.
  • Familiarity with modifying TypeSpec domain models in specs/<feature>/*.tsp.

In traditional frameworks, migrations are often generated by inspecting a local development database or writing fragile hand-crafted SQL scripts.

Aegil uses deterministic AST spec-diffing:

  1. The compiler reads your declarative specifications in specs/<feature>/*.tsp.
  2. It compares the desired schema against the history of committed migrations in migrations/.
  3. It emits a new, timestamped SQL migration file capturing the exact difference.

Whenever you add, modify, or remove an entity field in specs/:

specs/notes/notes.tsp
@entity({ table: "notes" })
model Note {
@pk
id: uuid;
title: string;
content: string;
// Newly added field
isArchived: boolean = false;
createdAt: utcDateTime;
}

2. Generate the Migration with aegil migrate

Section titled “2. Generate the Migration with aegil migrate”

Run the aegil migrate command:

Terminal window
aegil migrate --name add_is_archived_to_notes

Aegil analyzes the schema difference and creates a new committed migration under migrations/:

migrations/
20261123120000_init.sql
20261123134500_add_is_archived_to_notes.sql

Run aegil build to update the Serverpod ORM models and Flutter BLoC definitions, followed by aegil verify:

Terminal window
aegil build
aegil verify

aegil verify spins up a throwaway PostgreSQL container, applies all migrations in sequence, and verifies that the database schema precisely matches the compiled models.


Aegil classifies all schema changes into safe and destructive categories:

  • Adding a new @entity table.
  • Adding a nullable column (field?: string).
  • Adding a column with a default value (isArchived: boolean = false).
  • Adding an @index on an existing field.

Destructive Changes (Requires --allow-destructive)

Section titled “Destructive Changes (Requires --allow-destructive)”
  • Dropping a column or entity.
  • Changing an existing column type (e.g. int to string).
  • Adding a non-nullable column without a default value to a table with existing data.

If a schema modification would destroy existing data, aegil migrate halts with ERR_DESTRUCTIVE_MIGRATION unless you explicitly confirm the operation:

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

Best Practices for Zero-Downtime Evolution

Section titled “Best Practices for Zero-Downtime Evolution”
  1. Commit migrations to source control: Files in migrations/ are Zone 2 (hand-written/committed). Always commit them alongside your updated specs/.
  2. Never edit generated files in .aegil/server/migrations/: Only commit files in the root migrations/ directory.
  3. Use Expand and Contract: When renaming a field, add the new field first, migrate data, and drop the old field in a subsequent release.
  4. Always verify before merging: Run aegil verify in CI to ensure all migrations apply cleanly on a fresh PostgreSQL instance.