A database independent migration and seeding tool for python. Compatible with MySQL, PostgreSQL and MongoDB.
GPL-2.0 License
A database independent migration and seeding tool for python. Compatible with MySQL, PostgreSQL and MongoDB.
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': '[email protected]',
'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
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 .
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
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
.
Please report all issues to repo.
You MUST have write access to your file system to use this application.
January 2023 - Version 1.1.4:
--mongrationFile.json
January 2023 - Version 1.1.3:
--mongrationFile
for rollback and down commandJanuary 2023 - Version 1.1.2:
January 2023 - Version 1.1.1:
mongrationFile.json
file to add database connection variables. You can refer to an example of this file here
--migrationfile
(default env is development):mongrations migrate --file mongrationFile.json --env development
mongrationFile.json
file for you. Run this command:
mongrations file
January 2023 - Version 1.1.0:
migrations
directory will not be created until you create your first migration fileJanuary 2023 - Version 1.0.4:
migrations/
directory at rootpydotenvs
import from the migration fileJanuary 2022 - Version 1.0.4:
examples/
directoryAugust 2020: