Skip to main content

Migrations for MongoEngine ODM inspired by Django

Project description

Mongoengine-migrate

Work in progress

Schema migrations for Mongoengine ODM. Inspired by Django migrations system.

How it works

Let's suppose that we already have the following Document declaration:

from mongoengine import Document, fields
    
class Books(Document):
    name = fields.StringField()
    year = fields.StringField(max_length=4)
    isbn = fields.StringField()

Then we changed couple of things:

# Add Author Document
class Author(Document):
    name = fields.StringField(required=True)

class Books(Document):
    caption = fields.StringField(required=True)  # Make required and rename
    year = fields.fields.IntField()  # Change type to IntField
    # Removed field isbn
    author = fields.ReferenceField(Author)  # Add field

Such changes should be reflected in database during upgrading. To detect changes run the command:

mongoengine-migrate makemigrations -m myproject.db 

New migration file will be created:

from mongoengine_migrate.actions import *


dependencies = [
    'my_migration'
]

forward = [
    CreateDocument('Author', collection='author'),
    CreateField('Author', 'name', choices=None, db_field='name', default=None, max_length=None,
        min_length=None, null=False, primary_key=False, regex=None, required=True,
        sparse=False, type_key='StringField', unique=False, unique_with=None),
    RenameField('Books', 'name', new_name='caption'),
    AlterField('Books', 'caption', required=True, db_field='caption'),
    AlterField('Books', 'year', type_key='IntField', min_value=None, max_value=None),
    DropField('Books', 'isbn'),
    CreateField('Books', 'author', choices=None, db_field='author', dbref=False, default=None,
        target_doctype='Author', null=False, primary_key=False, required=False, sparse=False,
        type_key='ReferenceField', unique=False, unique_with=None),
]

Now in order to migrate database to the last migration, just run the command:

mongoengine-migrate migrate

Or to migrate to the certain migration:

mongoengine-migrate migrate my_migration

...to be continued

Roadmap

  • Migrations graph utilities and core code
  • Basic collection actions
  • Basic field actions
  • User-defined code action
  • Basic data type fields support (string, integer, float, etc.)
  • Dictionary, list fields support
  • Reference fields support
  • Embedded documents support + actions
  • Geo fields support
  • GridFS fields support
  • Altering fields in embedded documents
  • Document inheritance support
  • Dynamic documents support
  • Index support
  • Generic* fields support
  • Interactive mode
  • Schema repair tools
  • Alpha release

Author

Igor Derkach, gosha753951@gmail.com

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

mongoengine-migrate-0.0.1a1.dev2.tar.gz (56.3 kB view hashes)

Uploaded Source

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