Migrations¶
Complete migration workflow guide for fastappkit.
Migration System Overview¶
fastappkit provides unified migration management across core, internal apps, and external apps.
Core Migrations¶
- Location:
core/db/migrations/ - Purpose: Core-level schema changes
- Version Table:
alembic_version(shared)
Internal App Migrations¶
- Location:
core/db/migrations/(shared with core) - Purpose: Internal app schema changes
- Version Table:
alembic_version(shared with core) - Autogenerate: Sees all internal app models + core models
External App Migrations¶
- Location:
<app>/<app>/migrations/(isolated per app) - Purpose: External app schema changes
- Version Table:
alembic_version_<appname>(per-app) - Autogenerate: Sees only that app's models
Core Migrations¶
Create Migration¶
IMPORTANT: Must be run from project root.
When to Use: - Core-level schema changes - Shared infrastructure tables (sessions, logs, etc.)
Uses: Core project's DATABASE_URL from .env.
Internal App Migrations¶
Create Migration¶
IMPORTANT: Must be run from project root.
CRITICAL: Shared migration directory with core (core/db/migrations/).
Migration Order:
- From fastappkit.toml apps order, or
- From [tool.fastappkit.migration.order] if specified
Autogenerate Behavior: - Sees all internal app models - Sees all core models - Can detect cross-app relationships
Cannot Use Per-App Commands:
- upgrade/downgrade per-app not supported
- Use unified commands: fastappkit migrate upgrade
External App Migrations¶
Creating Migrations¶
CRITICAL: External apps cannot create migrations from core project. Use Alembic directly:
Uses: External app's .env and DATABASE_URL when developing independently.
Applying from Core¶
Uses: Core project's DATABASE_URL when integrated.
IMPORTANT:
- External apps cannot create migrations from core project
- Must have migrations/env.py with correct version table (alembic_version_<appname>)
Unified Commands¶
Apply All Migrations¶
IMPORTANT: Must be run from project root.
Execution Order:
1. Core migrations (always first)
2. Internal apps (skipped - already included in core migrations)
3. External apps (in fastappkit.toml order)
Recommended: Use this command for normal workflows.
Upgrade Core + Internal¶
IMPORTANT: Must be run from project root.
Applies core and internal app migrations (shared directory).
Downgrade¶
Downgrades core and internal app migrations.
Preview SQL¶
Shows SQL that would be executed without applying.
Migration Workflow Examples¶
Adding a New Model¶
-
Add model to
apps/blog/models.py: -
Create migration:
-
Review migration (optional):
-
Apply migration:
Modifying Existing Model¶
-
Modify model:
-
Create migration:
-
Apply:
Adding Relationships¶
-
Add relationship:
-
Create migration:
-
Apply:
Migration Commands Quick Reference¶
| Command | Purpose | Applies To |
|---|---|---|
fastappkit migrate core -m "msg" |
Create core migration | Core only |
fastappkit migrate app <name> makemigrations -m "msg" |
Create app migration | Internal only |
fastappkit migrate all |
Apply all migrations | All |
fastappkit migrate upgrade |
Apply core + internal | Core + Internal |
fastappkit migrate app <name> upgrade |
Apply app migration | External |
fastappkit migrate preview |
Preview SQL | Core + Internal |
fastappkit migrate downgrade <rev> |
Downgrade | Core + Internal |
Troubleshooting¶
Migration Conflicts¶
Problem: Multiple migrations modify same table
Solution: - Review migration files - Merge changes if needed - Ensure migrations are in correct order
Downgrade Issues¶
Problem: Downgrade fails or causes data loss
Solution:
- Review downgrade SQL: fastappkit migrate preview
- Backup database before downgrading
- Test downgrades in development first
External App Migration Problems¶
Revision Not Found¶
Error: Can't locate revision identified by '...'
Solution:
- Check migration files exist in external app's migrations/versions/
- Verify external app was developed independently
- Ensure migrations are included in package
Wrong Version Table¶
Error: Version table conflicts
Solution:
- Check migrations/env.py has alembic_version_<appname>
- Should NOT use shared alembic_version table
Database Connection Fails¶
Error: Connection errors when applying migrations
Solution:
- Check DATABASE_URL (uses core's when integrated)
- Verify database is running and accessible
- Check connection string format
Migration Directory Not Found¶
Error: Core migrations directory not found
Solution:
- Verify project structure
- Ensure project was created correctly
- Check core/db/migrations/ exists
All Commands Must Run from Project Root¶
CRITICAL: All migration commands must be run from project root (where fastappkit.toml is located).
Error: Configuration file not found
Solution: cd to project root before running commands.
Next Steps¶
- Development Workflow - Day-to-day development
- Migration System - Deep dive into migrations
- CLI Reference - Complete command reference