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.
Prerequisites
Section titled “Prerequisites”- An existing Aegil project with committed migrations in
migrations/. - Familiarity with modifying TypeSpec domain models in
specs/<feature>/*.tsp.
The Spec-Diff Philosophy
Section titled “The Spec-Diff Philosophy”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:
- The compiler reads your declarative specifications in
specs/<feature>/*.tsp. - It compares the desired schema against the history of committed migrations in
migrations/. - It emits a new, timestamped SQL migration file capturing the exact difference.
The Migration Workflow
Section titled “The Migration Workflow”Whenever you add, modify, or remove an entity field in specs/:
1. Update the Specification
Section titled “1. Update the Specification”@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:
aegil migrate --name add_is_archived_to_notesAegil analyzes the schema difference and creates a new committed migration under migrations/:
migrations/ 20261123120000_init.sql 20261123134500_add_is_archived_to_notes.sql3. Rebuild and Verify
Section titled “3. Rebuild and Verify”Run aegil build to update the Serverpod ORM models and Flutter BLoC definitions, followed by aegil verify:
aegil buildaegil verifyaegil verify spins up a throwaway PostgreSQL container, applies all migrations in sequence, and verifies that the database schema precisely matches the compiled models.
Safe vs. Destructive Changes
Section titled “Safe vs. Destructive Changes”Aegil classifies all schema changes into safe and destructive categories:
Safe Changes (Permitted by default)
Section titled “Safe Changes (Permitted by default)”- Adding a new
@entitytable. - Adding a nullable column (
field?: string). - Adding a column with a default value (
isArchived: boolean = false). - Adding an
@indexon 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.
inttostring). - 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:
aegil migrate --name drop_legacy_field --allow-destructiveBest Practices for Zero-Downtime Evolution
Section titled “Best Practices for Zero-Downtime Evolution”- Commit migrations to source control: Files in
migrations/are Zone 2 (hand-written/committed). Always commit them alongside your updatedspecs/. - Never edit generated files in
.aegil/server/migrations/: Only commit files in the rootmigrations/directory. - Use Expand and Contract: When renaming a field, add the new field first, migrate data, and drop the old field in a subsequent release.
- Always verify before merging: Run
aegil verifyin CI to ensure all migrations apply cleanly on a fresh PostgreSQL instance.
Next steps
Section titled “Next steps”- Review Full-Stack Runtime Architecture.
- Explore The 5-Stage Verification Pipeline.