Lightweight MongoDB migration module with named migrations and dependency resolution
Project description
quaestor
In ancient Rome, a quaestor was the official in charge of the treasury — here, it manages your MongoDB schema and data, your real treasure.
Lightweight MongoDB migration module with named migrations and dependency resolution.
Install
pip install mongo-quaestor
Quick Start
import asyncio
from motor.motor_asyncio import AsyncIOMotorClient
from quaestor import MigrationRegistry
registry = MigrationRegistry()
@registry.register("create_users_index")
async def create_users_index(db):
await db.users.create_index("email", unique=True)
@registry.register("add_status_field", depends_on=["create_users_index"])
async def add_status_field(db):
await db.users.update_many(
{"status": {"$exists": False}},
{"$set": {"status": "active"}},
)
async def main():
client = AsyncIOMotorClient("mongodb://localhost:27017")
db = client["myapp"]
applied = await registry.run(db, "myapp")
print(f"Applied migrations: {applied}")
asyncio.run(main())
API Reference
| Symbol | Signature | Description |
|---|---|---|
MigrationRegistry() |
MigrationRegistry() |
Create a new registry. No arguments. |
.register() |
register(name: str, depends_on: list[str] | None = None) -> Callable |
Decorator. Registers an async migration function. |
.run() |
async run(db: AsyncIOMotorDatabase, prefix: str) -> list[str] |
Execute all unapplied migrations. Returns names of applied migrations. |
.migrations |
migrations -> dict[str, MigrationDefinition] |
Read-only property. All registered migrations. |
How It Works
- Applied migrations are tracked in a MongoDB collection named
{prefix}_migrations. - Each record stores
name(str) andappliedAt(UTC datetime). - Dependencies are resolved via topological sort (Kahn's algorithm). Circular or missing dependencies raise
ValueError. - Failed migrations are not recorded, so they retry automatically on the next
run()call. - Independent migrations (no dependency relationship between them) execute in alphabetical order for deterministic behavior.
- Multiple registries can safely share one database by using different prefixes.
- Progress and errors are logged to the
quaestorlogger.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mongo_quaestor-0.1.0.tar.gz.
File metadata
- Download URL: mongo_quaestor-0.1.0.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3514d52e0c198e1fee2c7a61bbdb16ca0dc80f1dd1792285b07a20154e7272a4
|
|
| MD5 |
b01a2f9b4a16a25acbee2c8d18c70333
|
|
| BLAKE2b-256 |
b31c05019520c1a4efa8a91bcb26490a97c4243ae7ff28936211a518f28ff508
|
File details
Details for the file mongo_quaestor-0.1.0-py3-none-any.whl.
File metadata
- Download URL: mongo_quaestor-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
be15a4bda0ce2ead642a6c22127ce9ecfe49ed894074d8bc2f26a83637281c61
|
|
| MD5 |
4041b4630422e13fca52b918d3425fe2
|
|
| BLAKE2b-256 |
9a8c94f1cdb64b2725eb9929649ec8ae913bcbbeecba8e118942945f5989440a
|