Skip to main content

A MongoDB migration tool.

Project description

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.


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mongrate-0.0.3.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mongrate-0.0.3-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file mongrate-0.0.3.tar.gz.

File metadata

  • Download URL: mongrate-0.0.3.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for mongrate-0.0.3.tar.gz
Algorithm Hash digest
SHA256 15c5f916b4616ffdcb974d00089e81402ddc75020e8866aad6efd86ce68f4272
MD5 c4fbedfeedd8a2eb0143bec2fd101c24
BLAKE2b-256 7cad4b490cd8e5b1d4459edd7d84b2a997b0b2b2f0bc3aa819f1484b23e97683

See more details on using hashes here.

File details

Details for the file mongrate-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: mongrate-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for mongrate-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 44d822f3d303aff4561cdc5bf1b3ab1e6efd3d27f66e0487c11573587aa7e387
MD5 716a3210349bf5a7a1f522ef16c58e9f
BLAKE2b-256 407a9543011aabb4b8e83ef0c0ff6b07f27fd0d6cabc49210b484e85deff4e1f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page