MongoDB Migrations
Overview
This project includes a CLI tool for managing MongoDB database migrations.
Migration scripts should utilize the pymongo library to interact with the database.
Migrations are performed in a transactional manner, meaning that if an error occurs during the execution of a migration, the migration will be rolled back and the database will be left in the state it was in before the migration was attempted. To support this functionality MongoDB requires a replica set to be configured.
Installation
pip install starsol-mongo-migrate
Usage
Python API
You can use the migration system programmatically in your Python scripts:
from starsol_mongo_migrate import MigrationManager
# Create a migration manager
with MigrationManager('mongodb://localhost:27017/mydb', 'versions') as manager:
# Initialize migration system
manager.init()
# Generate new migration
revision = manager.generate('add_user_collection')
# List migrations
migrations = manager.list_migrations()
for rev, name in migrations:
print(f'{rev}: {name}')
# Check current revision
current = manager.current_revision()
print(f'Current: {current}')
# Upgrade to latest
manager.upgrade()
# Upgrade to specific revision
manager.upgrade(target_revision='20250216000109')
# Downgrade
manager.downgrade(target_revision='20250216000109')
# Use without transactions (for non-replica set)
manager.upgrade(use_transactions=False)
CLI Commands
This project also offers CLI commands to manage MongoDB migrations.
Initialize
Initialize the migration directory and the database version collection:
python3 -m starsol_mongo_migrate.cli --dir=versions init <your_mongo_uri>
Generate Migrations
Generate a new migration:
python3 -m starsol_mongo_migrate.cli --dir=versions generate migration_name
List Migrations
List all migrations:
python3 -m starsol_mongo_migrate.cli --dir=versions list
Upgrade Database
Upgrade the database to a specific revision:
python3 -m starsol_mongo_migrate.cli --dir=versions upgrade <your_mongo_uri> <target_revision>
Upgrade the database to the latest revision:
python3 -m starsol_mongo_migrate.cli --dir=versions upgrade <your_mongo_uri>
Upgrade without using transactions:
python3 -m starsol_mongo_migrate.cli --dir=versions upgrade --no-transaction <your_mongo_uri>
Downgrade Database
Downgrade the database to a specific revision:
python3 -m starsol_mongo_migrate.cli --dir=versions downgrade <your_mongo_uri> <target_revision>
Downgrade without using transactions:
python3 -m starsol_mongo_migrate.cli --dir=versions downgrade --no-transaction <your_mongo_uri> <target_revision>
Show Current Revision
Display the current database revision:
python3 -m starsol_mongo_migrate.cli --dir=versions current <your_mongo_uri>
Metadata
Release files for starsol-mongo-migrate 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| starsol_mongo_migrate-0.2.0.tar.gz | 8.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| starsol_mongo_migrate-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.9 kB
Release files / starsol_mongo_migrate-0.2.0.tar.gz
| Download URL | starsol_mongo_migrate-0.2.0.tar.gz |
|---|---|
| Size | 8.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8932f29bea9e314c9076a00bf4d6a4c6eefaa9749ce1e446f9541d4f2fb45af9
|
|
BLAKE2b-256 checksum How to use checksums |
c0a1f61d0b0db9ab189937a7102187820187848f9684d4ee39e5d185ddae0a0c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.11
|
Release files / starsol_mongo_migrate-0.2.0-py3-none-any.whl
| Download URL | starsol_mongo_migrate-0.2.0-py3-none-any.whl |
|---|---|
| Size | 9.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
26d5cf89c9bd4ebf24dd02c9b863916a6175a56e7d6741f02d44571ae7575551
|
|
BLAKE2b-256 checksum How to use checksums |
e9c0bd4d943e21519fc586659c97f9bccd3be3dd9a6e239bcbb182be38531c84
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.11
|