Skip to main content

Simple MongoDB migrations tool.

Project description

Mongo Migrator

License PyPI version

Python 3.8 MongoDB

Coverage Status Build Status

Mongo Migrator is a simple tool to manage MongoDB migrations. It allows you to create, run, and rollback migrations in a MongoDB database.

Features

  • Easily create migration files with a predefined template.
  • Apply migrations to the database.
  • Revert migrations quickly.
  • Validate and ensure no bifurcations in migration history.
  • Keep a clear version history of migrations.

Installation

To install Mongo-Migrator, you can use PyPI:

pip install mongo-migrator

Configuration

Mongo-Migrator uses a configuration file named mongo-migrator.config to connect to your MongoDB instance and manage migrations. Here is an example of a configuration file:

[database]
host = localhost
port = 27017
name = your_database
user = your_user
password = your_password

[migrations]
directory = migrations
collection = version_history

Configuration breakdown

  • database: MongoDB connection details.
    • host: MongoDB host address.
    • port: MongoDB port number.
    • name: The database name where migrations will be applied.
    • user: (Optional) The username for database authentication.
    • password: (Optional) The password for database authentication.
  • migrations: Migration settings.
    • directory: Directory where migration files are stored.
    • collection: Name of the collection that stores migration version information.

Usage

Mongo-Migrator provides several commands to manage your migrations. These can be executed from the command line.

Initialize the package

To initialize the package, run the following command:

mongo_migrator init

This command initializes the migration system by reading the configuration file, creating the migrations directory, and setting up the version collection in the specified MongoDB database.

Create a new migration

To create a new migration, run the following command:

mongo-migrator create --title "Added new field"

This command generates a new migration file with a timestamp and the provided title. The new migration file will be placed in the migrations directory.

Apply migrations

mongo-migrator upgrade

This command applies all pending migrations in the correct order. You can also specify a version to upgrade to. The argument <version> indicates the last version which will be applied.

mongo-migrator upgrade --version <version>

Rollback migrations

mongo-migrator downgrade

This command rolls back the last migration that was applied. You can also specify a version to downgrade to. The argument <version> indicates the version in which the database will be rolled back. Downgrade in migration <version> will not be applied.

mongo-migrator downgrade --version <version>

View history

mongo-migrator history

This command displays the migration history, showing the version number and the migration message.

Other commands

  • mongo-migrator --help: Display the help message.
  • mongo-migrator --version: Display the package version.

Test environment

This repository comes with a folder test-project which can be run to test the mongo-migrator package. It also contains example-project, which is the same small project as test-project already initialized and with some migrations made.

The following dependencies are required to run the example project:

  • Docker
  • Docker Compose
  • Python 3.8 or higher
  • pipenv

Setup of the test environment

  1. First of all, go to the test-project folder and activate the environment:
cd test-project
export PIPENV_VENV_IN_PROJECT=1 # Optional, but recommended
pipenv shell
pipenv install
  1. Use the docker-compose.yml file to start a MongoDB instance:
docker-compose up -d
  1. Run the setup script
python3 setup-test-db.py

The script will create a database called test-db and a collection called test-collection with some sample data.

  1. Now the environment is ready to be tested.

Contributing

By the time, contributions are not being accepted. However, issues will be reviewed and answered.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

mongo_migrator-1.0.0rc1.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

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

mongo_migrator-1.0.0rc1-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file mongo_migrator-1.0.0rc1.tar.gz.

File metadata

  • Download URL: mongo_migrator-1.0.0rc1.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.10.12 Linux/6.8.0-52-generic

File hashes

Hashes for mongo_migrator-1.0.0rc1.tar.gz
Algorithm Hash digest
SHA256 7a89780a8afbf6c1f6b34095878917bff0a4e94ec7fca0d6eb1985bd6ee91430
MD5 92a3a32db1d5e02485a47d0ac3b57ddb
BLAKE2b-256 83f3d6c424aa2aae827466274248283ac820d8771ce86cddafa8789fb12a869e

See more details on using hashes here.

File details

Details for the file mongo_migrator-1.0.0rc1-py3-none-any.whl.

File metadata

  • Download URL: mongo_migrator-1.0.0rc1-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.10.12 Linux/6.8.0-52-generic

File hashes

Hashes for mongo_migrator-1.0.0rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 acb477b41643bbecfcfc4bfd1ec2c98fe87702816988a04fdd4f0cc5ddc30cee
MD5 8182aaa903b32674111488b7f89aff30
BLAKE2b-256 43d9143589a50d059fa3cbe6420eaeb67de83eeb1adc658f33a112dc58e2bb14

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