Skip to main content

The un-migration migration tool

Project description

Build Status

The Concept

“migrations should happen naturally”

monarch is a mongo utility belt that helps developers and admins deal with common admin usecases. The main usecase that this library was built for was _migrations_ but it does a bunch or other useful things like makes it easy to backup, restore, and copy environments between one another.

It has been very helpful for our teams – and hopefuly you guys can find it useful as well.

The Interface

generate <migration_name>

Generates a new migration template. In this template you write the necessary code to perform your migration

list_migrations <env_name>

Lists all of the migrations and there current status

migrate <env_name>

Runs all pending migration on the given environment. Normally you will use copy_db to move the production environment locally and test the migrations locally first before doing on production

backup <env name>

Backs ups a given database. Currently it only supports backing up locally. But S3 support is coming soon. Make sure you have BACKUPS configured in your migrations/settings.py file It will dump your database and compress it and give it a unquie name

restore <backup_name>:<env_name>

Restore a backup into the provided environment. It will trucacte the database before the import

list_backups

Lists the available backups

copy_db <from_env>:<to_env>

Copies one database into another database

It will make an archive of the “From” database and then truncate the “To” database and restore that archive into the “To” database

This is most useful for copying the production database locally to test migrations before doing it for reals

init

Initializes monarch for your project

The Installation

pip install monarch

You need to configure monarch for each project. Simply run monarch init in the root of your project. Then go into migrations.settings.py to configure your environments and backups

Migrations

One of the core design principals behind monarch is that it does not provide a DSL or DDL for database specific migrations (like South and alembic)

You write your migrations in pure python using whatever libraries you like.

When we develop a feature we implement the following:

  • the feature

  • the tests

  • and the necessary migrations that move the production data to where it needs to be to rock the new feature

So now with monarch we can implement a Pull Request(PR) with the feature, test and migration. And once your Continuous Integration says that your tests are cool then you can deploy and run the pending migrations needed for your feature.

Example Use Case

  1. Generate a Migration

monarch create add_indexes_to_user_table

That will create a template migration that looks something like this

# in ./migrations/_201405290038_add_indexes_to_user_table_migration.py

from monarch import MongoBackedMigration

class AddIndexesToUserTableMigration(MongoBackedMigration):

    def run(self):
        """Write the code here that will migrate the database from one state to the next
        No Need to handle exceptions -- we will take care of that for you
        """
        raise NotImplementedError
  1. Implement the Migration

Do whatever you want in that run method. I mean anything! Go crazy wild man.

  1. Test the Migration

# copy the production db locally
monarch copy_db production:development

# check the status of the pending migraitons
monarch list_migrations development

# try running the migrations
monarch migrate development

# everything cool?

# just to be sure -- lets make a backup
monarch backup production

# time to rock
monarch migrate production

# not cool?
# fix your migration and try again
monarch copy_db production:development

# and so on ....

RoadMap

  • S3 backup support

  • Support for PostgreSQL and the like

  • Use only pymongo (not mongoengine)

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

monarch-0.0.6.tar.gz (9.2 kB view details)

Uploaded Source

File details

Details for the file monarch-0.0.6.tar.gz.

File metadata

  • Download URL: monarch-0.0.6.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for monarch-0.0.6.tar.gz
Algorithm Hash digest
SHA256 cc6197cc8656b1c915a82eed2eb4c4d4fe2cb713a409a70ca96920c9cae152ed
MD5 ba70eaa6134fa9f1d5b3eb4f67a7a94e
BLAKE2b-256 b0a9bfe9d9a2bde6272db9232561563b2437c3c82c1e1b2c06825cdd301559f5

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