Cosmos DB schema migrations
Cosmos DB schema migrations with DBLift are open-source Python files for the NoSQL API. DBLift does not copy data between databases. There is no licence. Install with pip install "dblift[cli,cosmosdb]" and set the engine type to cosmosdb. The driver is the azure-cosmos SDK for Azure Cosmos DB, not the MongoDB API or vCore. MongoDB is a separate engine.
Cosmos DB schema migrations with DBLift
Install
pip install "dblift[cli,cosmosdb]"Connection URL
https://account.documents.azure.com:443/Set account_endpoint (or url) and database_name (or database). Authenticate with account_key (or password), or set use_managed_identity to true to use Azure DefaultAzureCredential. A localhost endpoint is auto-detected as the emulator.
dblift.yaml
database:
type: cosmosdb
account_endpoint: "https://account.documents.azure.com:443/"
database_name: "app"
use_managed_identity: trueCosmos DB schema migrations example
from azure.cosmos import PartitionKey
def migrate(context):
if context.dry_run:
context.log.info("[DRY-RUN] would create container 'users'"); return
context.db.create_container(id="users", partition_key=PartitionKey(path="/tenant_id"), offer_throughput=400)
Engine settings
account_endpoint—Account endpoint. url is a synonym. Env: DBLIFT_DB_ACCOUNT_ENDPOINT. A localhost endpoint is auto-detected as the emulator.account_key—Account key. password is a synonym. Env: DBLIFT_DB_ACCOUNT_KEY.database_name—Database name. database is a synonym. Env: DBLIFT_DB_DATABASE_NAME.use_managed_identityfalseWhen true, authenticate with Azure DefaultAzureCredential. Env: DBLIFT_DB_USE_MANAGED_IDENTITY.Behaviour notes
Names are V<version>__<desc>.py, optional undo U<version>__<desc>.py, and repeatable R__<desc>.py. Each file defines migrate(context). context.db is the azure-cosmos DatabaseProxy. SQL write statements are rejected.
Anything the azure-cosmos SDK can do goes in a script: containers with a partition key, throughput or autoscale, indexing policy or TTL via replace_container, and documents. DBLift has no operation list of its own.
Applied Cosmos DB schema migrations are recorded in the dblift_schema_history container.
The lock document in dblift_migration_lock uses a 5-minute lease. migrate waits up to 60 seconds, then fails.
A script that fails partway leaves its completed operations in place, so write idempotent migrations. 429 throttling retries come from the azure-cosmos SDK defaults.
A localhost endpoint is auto-detected as the emulator.
It still connects and runs scripts with context.dry_run=True, so scripts must check that flag before writing.
migrate, info, validate, undo (via U files), baseline, repair, and clean. clean drops every container in the database.
diff and drift detection are Pro.