Skip to main content

Mongrate

A lightweight MongoDB migration tool.


Overview

Mongrate simplifies managing schema changes in MongoDB. It provides a robust migration system to track, apply, and rollback changes to your MongoDB database.


Features

  • Initialize a MongoDB migration system with a single command.
  • Create migration scripts effortlessly.
  • Apply migrations incrementally or all at once.
  • Rollback specific migrations or all migrations.
  • View migration history, including applied and pending migrations.

Installation

Install Mongrate using pip:

pip install mongrate

Getting Started

1. Initialize the System

Set up Mongrate in your project:

mongrate init

This command creates a configuration file (mongrate_config.yaml) and a migrations directory.

2. Configuration

Update the mongrate_config.yaml file with your database details:

db_url: "mongodb://localhost:27017"
db_name: "my_database"
migrations_dir: "migrations"

3. Create a Migration

Generate a new migration file:

mongrate create add_users_collection

This creates a new script in the migrations directory.

4. Define Migration

Edit the generated migration script to specify your database changes:

# Example: 20241228230118_add_users_collection.py

def upgrade(db):
    db.create_collection("users")
    db["users"].insert_one({"name": "admin", "role": "superuser"})

def downgrade(db):
    db.drop_collection("users")

5. Apply Migrations

Apply all pending migrations:

mongrate upgrade all

Apply a specific migration:

mongrate upgrade 20241228230118_add_users_collection.py

6. Rollback Migrations

Rollback all migrations:

mongrate downgrade all

Rollback a specific migration:

mongrate downgrade 20241228230118_add_users_collection.py

7. View Migration History

Check applied and pending migrations:

mongrate history

Example output:

Applied Migrations:
  - 20241228230118_add_users_collection.py

Pending Migrations:
  - 20241228233045_add_orders_collection.py

Commands Overview

Command Description
mongrate init Initializes the migration system.
mongrate create <name> Creates a new migration script.
mongrate upgrade all Applies all pending migrations.
mongrate upgrade <file> Applies a specific migration.
mongrate downgrade all Rolls back all applied migrations.
mongrate downgrade <file> Rolls back a specific migration.
mongrate history Displays applied and pending migrations.

Example Workflow

Initialize Mongrate:

mongrate init

Create a migration:

mongrate create add_users_collection

Edit the migration file:

def upgrade(db):
    db.create_collection("users")

Apply migrations:

mongrate upgrade all

View migration history:

mongrate history

Rollback a migration:

mongrate downgrade add_users_collection.py

Why Use Mongrate?

  • Simplifies MongoDB migration management.
  • Tracks applied and pending migrations automatically.
  • Provides an easy-to-use CLI.
  • Designed specifically for MongoDB workflows.

License

Mongrate is licensed under the MIT License.


Metadata

Release files for mongrate 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mongrate 0.0.5
File Size Uploaded
mongrate-0.0.5.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mongrate 0.0.5
File Interpreter ABI Platform
mongrate-0.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 14.4 kB

Release files / mongrate-0.0.5.tar.gz

Download URL mongrate-0.0.5.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f61b081d6ae5a0a2d98c91d05a98692bb7a36ce9f214fdd67f0f5be0f60c2b70
BLAKE2b-256 checksum
How to use checksums
63f7ccf7a65ff8c2ec8cd9e6f2742e6feddd1f1cd009621aafa04558dcdeda5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.3

Release files / mongrate-0.0.5-py3-none-any.whl

Download URL mongrate-0.0.5-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
66e824fe2fdc2429fd84c236e20ddd7ccc3f679e252ac6297073e7944c8ca078
BLAKE2b-256 checksum
How to use checksums
f69ca9432e728de350df9a12977175178c971b057682357922b517547620f701
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.0.5 This release

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page