Skip to main content

Mongrations; a database migration tool for Python 3.6 and above.

Project description


alt text A migrations tool for Python 3.6+. Mongrations started as a MongoDB migrations tool but has introduced MySQL & Postgres as compatible databases for the Mongrations tool.

Getting Started

1 . Generate a migration file

mongrations create insert-into-members

2 . Contents of the generated migration file (import and class definition are autogenerated for migration file - contents of up() and down() are user defined.)

from mongrations import Mongrations, Database
from pydotenvs import load_env

# This is a MongoDB example. Go to /examples directory
# for more examples

class Mongration(Database):
    def __init__(self):
        super(Database, self).__init__()

    def up(self):
        collection = self.db['members']
        data = {
            'accountId': 1,
            'username': 'admin',
            'email': '',
            'firstName': 'Site',
            'lastName': 'Owner'

    def down(self):
        collection = self.db['members']
        collection.delete_one({'username': 'admin'})

Mongrations(Mongration, 'sync')

3 . Run migrations

mongrations migrate


pip install --upgrade mongrations

or install locally

python install


Mongrations comes with a CLI Tool as well as a class for a pythonic migration approach. PyMongo, PyMySQL & Psycopg2 are used under the hood, so follow PyMongo's, PyMySQL's, or Psycopg2's documentation for instructions on how to create your migrations. For the environment variable tool used in this application, follow this repo (its also installed with this package).

Refer to Mongrations documentation for more information.


Usage: mongrations [OPTIONS] COMMAND [ARGS]...

  Mongrations; a database migration tool for Python 3.6 and above.

  --version  Show the version and exit.
  --help     Show this message and exit.


CLI Examples

mongrations create [name]  # create new migration (w/ name)
mongrations migrate  # run migrations
mongrations down  # tear down migrations
mongrations undo  # undo last migration

Mongrations Class

from mongrations import MongrationsCli

migrations = MongrationsCli()

migrations.create(directory='migrations', name='file_name')

Run example migration in examples/ folder


Please report all issues to repo.


You can install psycopg2 from source via; python develop. Follow prompts. You will need root access to development machine to install this tool.


August 2020:

  • Introduced the official version 1.0.0 of Mongrations!
  • Rewrote command line tool; much easier and intuiative
  • Extracted classes into their own files; reducing clutter
  • Added a raw sql function that allows for much more flexibility
  • File name rewrites (if you encounter an upgrade error run the option: --no-cache, with pip)
  • psycopg2 is now installed optionally (refer to Notes)
  • Super fast writing to the system
  • has been cleaned up. An occasional bug occured when installing
  • Added/Updated examples (refer to Github)

Project details

Download files

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

Files for mongrations, version 1.0.0
Filename, size File type Python version Upload date Hashes
Filename, size mongrations-1.0.0.tar.gz (8.5 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page