Skip to main content

Migrations for MongoEngine ODM inspired by Django

Project description

Mongoengine-migrate

version pyversions travis license

Framework-agnostic schema migrations for Mongoengine ODM. Inspired by Django migrations system.

WARNING: this is an unstable version of software. Please backup your data before migrating

Installation

pip3 install mongoengine-migrate

Features

  • Documents
    • Creating, dropping, renaming
    • Renaming a collection
    • Creating, dropping, renaming of fields
    • Converting to/from a DynamicDocument
    • Inheritance
  • Embedded documents
    • Recursive creating, dropping
    • Renaming
    • Recursive creating, dropping, renaming of fields
    • Converting to/from a DynamicEmbeddedDocument
    • Inheritance
  • Altering fields in document and embedded documents
    • Changing of init parameters such as db_field, required, etc.
    • Convertion between field types (if possible)
  • Automatic select a query or a python loop to perform an update depending on MongoDB version
  • Two policies of how to work with existing data which does not meet to mongoengine schema

All mongoengine field types are supported, including simple types, lists, dicts, references, GridFS, geo types, generic types.

Example

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

from mongoengine import Document, fields
    
class Book(Document):
    name = fields.StringField(default='?')
    year = fields.StringField(max_length=4)
    isbn = fields.StringField()

Then we make some changes:

from mongoengine import Document, fields

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

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

Such changes should be reflected in database. The following command creates migration file (myproject.db is a python module with mongoengine document declarations):

mongoengine-migrate makemigrations -m myproject.db 

New migration file will be created:

from mongoengine_migrate.actions import *

# Existing data processing policy
# Possible values are: strict, relaxed
policy = "strict"

# Names of migrations which the current one is dependent by
dependencies = [
    'previous_migration'
]

# Action chain
actions = [
    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=False,
        sparse=False, type_key='StringField', unique=False, unique_with=None),
    RenameField('Book', 'name', new_name='caption'),
    AlterField('Book', 'caption', required=True, db_field='caption'),
    AlterField('Book', 'year', type_key='IntField', min_value=None, max_value=None),
    DropField('Book', 'isbn'),
    CreateField('Book', '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),
]

Next, upgrade the database to the latest version:

mongoengine-migrate migrate

Or to the certain migration:

mongoengine-migrate migrate previous_migration

Actual db changes

During the running forward the migration created above the following changes will be made:

  • "author" collection
    1. Nothing to do
  • "book" collection
    1. Existing fields "name" will be renamed to "caption"
    2. All unset "caption" fields will be set to default value '?' (because this field was defined as "required")
    3. Existing fields "year" with string values will be casted to integer value
    4. "isbn" field will be dropped

On backward direction the following changes will be made:

  • "book" collection
    1. "author" field will be dropped
    2. All integer values in "year" field will be casted back to string
    3. Existing "caption" fields will be renamed back to "name"
  • "author" collection
    1. "name" field will be dropped
    2. whole "author" collection will be dropped

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.1a3.tar.gz (56.9 kB view details)

Uploaded Source

File details

Details for the file mongoengine-migrate-0.0.1a3.tar.gz.

File metadata

  • Download URL: mongoengine-migrate-0.0.1a3.tar.gz
  • Upload date:
  • Size: 56.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.6.7

File hashes

Hashes for mongoengine-migrate-0.0.1a3.tar.gz
Algorithm Hash digest
SHA256 e5e56e5d7e8f48d3e5770ff81286b39ed7a439b786def0b5434471d4b663c58a
MD5 6ad0e06fc5a6c7edb9354d7947618d8d
BLAKE2b-256 25d170064f2ab22a2d58f3b7fe357868863d9de3eec06b5748e464fb401fdd46

See more details on using hashes here.

Supported by

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