Performance: Alembic migrations
Alembic migrations affect backend database schema changes and can indirectly impact frontend load times if migrations block API responses.
Jump into concepts and practice - no test required
from alembic import command from alembic.config import Config alembic_cfg = Config("alembic.ini") # Run migrations separately before starting the app # or run migrations asynchronously in a background task import asyncio async def run_migrations_async(): loop = asyncio.get_running_loop() await loop.run_in_executor(None, lambda: command.upgrade(alembic_cfg, "head")) # Call run_migrations_async() before serving requests
from alembic import command from alembic.config import Config alembic_cfg = Config("alembic.ini") command.upgrade(alembic_cfg, "head") # runs migrations synchronously on app start
| Pattern | Backend Blocking | API Response Delay | User Impact | Verdict |
|---|---|---|---|---|
| Synchronous migrations during app start | High | High | Delays page load and interaction | [X] Bad |
| Asynchronous or pre-run migrations | None during requests | Minimal | Smooth user experience | [OK] Good |
revision with a message describing the change.alembic revision -m "message" creates a new migration file with the given message.alembic revision -m "add users table"alembic upgrade headalembic revision -m "add users table" creates a new migration script file describing the addition of the users table.alembic upgrade head applies all migrations up to the latest, updating the database schema accordingly.alembic upgrade head but get an error about missing dependencies in your migration script. What is the best way to fix this?alembic upgrade head applies the new migration safely without deleting existing data.