Skip to main content

Mongrations; a database independent migration and seeding tool for python.

Project description

Mongrations

alt text A database independent migration and seeding tool for python. Compatible with MySQL, PostgreSQL and MongoDB.

Required

  • Python version 3.6 or above
  • pip version 20.3 or above

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 - contents of up() and down() methods are user defined.)

from mongrations import Mongrations, Database

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

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

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


Mongrations(Mongration)

3 . Run migrations

mongrations migrate

Install

pip install --upgrade pip
pip install -U mongrations

or install locally

git clone https://github.com/ableinc/mongrations.git
cd mongrations
python -m pip install -r requirements.txt
python -m pip install .

Use

Mongrations comes with a CLI Tool and an import 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.

CLI

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

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

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

Commands:
  create
  down
  inspect
  migrate
  undo

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')
migrations.migrate()
migrations.down()
migrations.undo()

Run example migration in examples/ folder

Multi-instance

If your API uses multiple databases to write and read data, you can provide multiple database connections. This can be achieved by providing a connection object (connection_obj) to the Mongrations class in your migrations file. For a connection_obj example, please refer to the examples/ folder. You can also do this by prepending the service name to your environment variables.

Supported service names:

  • MONGO_
  • MYSQL_
  • POSTGRES_

Example .env file:

MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=password
MYSQL_DB_NAME=myapp
MYSQL_PORT=3306

Note: MONGO_ service name does NOT accept MONGO_COLLECTION_NAME. You will need to provide the collection name in your migration file. The synchronous and asynchronous instances of MongoDB use admin as the authSource by default. If you do not want to use authSource please use MONGO_AUTH_SOURCE=None.

Issues

Please report all issues to repo.

You MUST have write access to your file system to use this application.

Changelog

January 2023 - Version 1.1.4:

  • Bugfix: CLI tool will now add service name to environment when using --mongrationFile.json

January 2023 - Version 1.1.3:

  • Updated: CLI tool to handle --mongrationFile for rollback and down command
  • psycopg will be downloaded by the library. Installing from source is no longer an option.

January 2023 - Version 1.1.2:

  • Bugfix: postgres connection library fix
  • Bugfix: Database connection would close prematurely

January 2023 - Version 1.1.1:

  • You can now use the mongrationFile.json file to add database connection variables. You can refer to an example of this file here
    • You can specify the environment with --migrationfile (default env is development):
    mongrations migrate --file mongrationFile.json --env development
    
  • The CLI tool can generate the mongrationFile.json file for you. Run this command:
    mongrations file
    

January 2023 - Version 1.1.0:

  • Fixed bug with CLI tool where directory argument wasn't being passed properly to the migrate function.
  • The CLI tool has new arguments with better helper descriptions
  • The database connection class has been updated to provide more enhances connection strings
  • The cache system was rebuilt - The way mongrations caches will change in the future
  • migrations directory will not be created until you create your first migration file
  • Updated error codes and error messages.
  • In the event your PYTHON_PATH is changed and points to a Python version less than 3.6 the CLI tool will prompt you.

January 2023 - Version 1.0.4:

  • The cache system will now keep the cache file in the migrations/ directory at root
  • psycopg[binary,pool] will now be installed during pip installation (pip 20.3 > is required)
  • Removed the default pydotenvs import from the migration file
  • Time (in ms) will be appended to file names instead of UUIDs
  • The library wil be getting a rewrite and released under another name. This will be the last major release to the library under this name. Note: bug fixes will still be published.

January 2022 - Version 1.0.4:

  • Squashed bugs
  • Mongrations can now run on Linux
  • Default: stdout error output if error occurs during caching operation
  • Removed the psycopg2 install step from setup.py
  • Simplified how the database connection strings are initialized
  • Inspect will now pretty print JSON structure and provide file system location
  • Updated examples/ directory

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
  • Setup.py 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.

Source Distribution

mongrations-1.1.4.tar.gz (20.9 kB view details)

Uploaded Source

File details

Details for the file mongrations-1.1.4.tar.gz.

File metadata

  • Download URL: mongrations-1.1.4.tar.gz
  • Upload date:
  • Size: 20.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.7

File hashes

Hashes for mongrations-1.1.4.tar.gz
Algorithm Hash digest
SHA256 a6a9783ee0b32040531651b409627d064e6e4923a29b2390c74a3f132af8e213
MD5 3b5a0079ce3cdcf5298d9d473faeee8e
BLAKE2b-256 b0314f3a7d35055fbd2e7e662c8000c6b3839cdb50b5a525b95d3193ca6fd217

See more details on using hashes here.

Supported by

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