MongoDB migration tool
Project description
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>
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 starsol_mongo_migrate-0.2.0.tar.gz.
File metadata
- Download URL: starsol_mongo_migrate-0.2.0.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8932f29bea9e314c9076a00bf4d6a4c6eefaa9749ce1e446f9541d4f2fb45af9
|
|
| MD5 |
8464934217b014b351dca5488fb6eca7
|
|
| BLAKE2b-256 |
c0a1f61d0b0db9ab189937a7102187820187848f9684d4ee39e5d185ddae0a0c
|
File details
Details for the file starsol_mongo_migrate-0.2.0-py3-none-any.whl.
File metadata
- Download URL: starsol_mongo_migrate-0.2.0-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
26d5cf89c9bd4ebf24dd02c9b863916a6175a56e7d6741f02d44571ae7575551
|
|
| MD5 |
2ff7809647606dfbd78a3374dddac54b
|
|
| BLAKE2b-256 |
e9c0bd4d943e21519fc586659c97f9bccd3be3dd9a6e239bcbb182be38531c84
|