Migrations¶
TenantMigrationManager ¶
TenantMigrationManager(
config: TenancyConfig,
store: TenantStore[Any],
alembic_cfg_path: str | Path = "alembic.ini",
executor: Any | None = None,
)
Alembic migration manager for multi-tenant databases.
Supports SCHEMA and DATABASE isolation strategies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
TenancyConfig
|
Tenancy configuration. |
required |
store
|
TenantStore[Any]
|
Tenant store used by |
required |
alembic_cfg_path
|
str | Path
|
Path to |
'alembic.ini'
|
executor
|
Any | None
|
Optional With Text Only
|
None
|
Example::
migrator = TenantMigrationManager(config, store)
# Migrate a single tenant
await migrator.upgrade_tenant(tenant, revision="head")
# Migrate all tenants with 20 concurrent workers
results = await migrator.upgrade_all(revision="head", concurrency=20)
failed = [r for r in results if not r["success"]]
Source code in src/fastapi_tenancy/migrations/manager.py
upgrade_tenant
async
¶
upgrade_tenant(
tenant: Tenant, revision: str = "head"
) -> None
Run Alembic upgrade for tenant.
Executes the migration in a thread pool executor to avoid blocking the asyncio event loop with synchronous Alembic I/O.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tenant
|
Tenant
|
Target tenant. |
required |
revision
|
str
|
Alembic revision target (default: |
'head'
|
Raises:
| Type | Description |
|---|---|
MigrationError
|
When the migration fails. |
Source code in src/fastapi_tenancy/migrations/manager.py
downgrade_tenant
async
¶
downgrade_tenant(
tenant: Tenant, revision: str = "-1"
) -> None
Run Alembic downgrade for tenant.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tenant
|
Tenant
|
Target tenant. |
required |
revision
|
str
|
Alembic revision to downgrade to (default: |
'-1'
|
Raises:
| Type | Description |
|---|---|
MigrationError
|
When the migration fails. |
Source code in src/fastapi_tenancy/migrations/manager.py
get_current_revision
async
¶
get_current_revision(tenant: Tenant) -> str | None
Return the current Alembic revision for tenant.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tenant
|
Tenant
|
Target tenant. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The current revision string (e.g. |
str | None
|
when no migrations have been applied. |
Source code in src/fastapi_tenancy/migrations/manager.py
upgrade_all
async
¶
upgrade_all(
revision: str = "head",
concurrency: int = 10,
page_size: int = 100,
) -> list[dict[str, Any]]
Upgrade all active tenants to revision with bounded concurrency.
Loads tenants in pages from the store to avoid loading the entire
fleet into memory at once. Uses asyncio.Semaphore(concurrency)
to cap parallel database connections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
revision
|
str
|
Alembic revision target. |
'head'
|
concurrency
|
int
|
Maximum concurrent migration workers. |
10
|
page_size
|
int
|
Number of tenants to fetch per page from the store. |
100
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of result dicts, one per tenant:: [ {"tenant_id": "...", "success": True, "revision": "head"}, {"tenant_id": "...", "success": False, "error": "..."}, ] |
Example::
results = await migrator.upgrade_all(revision="head", concurrency=20)
failed = [r for r in results if not r["success"]]
print(f"{len(failed)} of {len(results)} tenants failed migration")
Source code in src/fastapi_tenancy/migrations/manager.py
downgrade_all
async
¶
downgrade_all(
revision: str = "-1",
concurrency: int = 10,
page_size: int = 100,
) -> list[dict[str, Any]]
Downgrade all active tenants with bounded concurrency.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
revision
|
str
|
Alembic revision target. |
'-1'
|
concurrency
|
int
|
Maximum concurrent workers. |
10
|
page_size
|
int
|
Page size for store pagination. |
100
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of result dicts (same format as :meth: |