migration_context¶
Page Moved and Expanded
This page has been moved to a comprehensive tutorial with detailed examples.
📍 New Location¶
The new page includes: - All parameters documented with examples - Check vs Migrate mode comparison - Environment-specific patterns - Production best practices - Complete troubleshooting guide
For quick reference:
Quick Reference¶
migration_context is a FastAPI lifespan helper for startup migration or read-only schema checks.
Related direct helpers:
check_schema_on_startup(...)migrate_on_startup(...)
Modes¶
mode="migrate": run migration workflow at startupmode="check": validate connectivity/schema state without mutation
Usage in lifespan¶
from contextlib import asynccontextmanager
from fastapi import FastAPI
from dbwarden.fastapi import migration_context
@asynccontextmanager
async def lifespan(app: FastAPI):
async with migration_context(mode="check", all_databases=True, fail_fast=True):
yield
app = FastAPI(lifespan=lifespan)
Important options¶
database,all_databasesdev,strict_translationwith_backup,backup_dirallow_in_productionfail_fastonly_dev
only_dev behavior¶
If only_dev=True, helper logic runs only when ENVIRONMENT is one of:
devdevelopmentlocaltesttesting
Otherwise startup helper logic is skipped.
Production guidance¶
- use
mode="check"in app startup by default - run
mode="migrate"in controlled rollout jobs unless intentionally allowed in production
Navigation¶
- Previous: get_session
- Next: DBWardenHealthRouter