Migrations for MongoEngine ODM inspired by Django
Project description
Mongoengine-migrate
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)
- Changing of init parameters such as
- 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
- Nothing to do
- "book" collection
- Existing fields "name" will be renamed to "caption"
- All unset "caption" fields will be set to default value
'?'
(because this field was defined as "required") - Existing fields "year" with string values will be casted to integer value
- "isbn" field will be dropped
On backward direction the following changes will be made:
- "book" collection
- "author" field will be dropped
- All integer values in "year" field will be casted back to string
- Existing "caption" fields will be renamed back to "name"
- "author" collection
- "name" field will be dropped
- whole "author" collection will be dropped
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for mongoengine-migrate-0.0.1a3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5e56e5d7e8f48d3e5770ff81286b39ed7a439b786def0b5434471d4b663c58a |
|
MD5 | 6ad0e06fc5a6c7edb9354d7947618d8d |
|
BLAKE2b-256 | 25d170064f2ab22a2d58f3b7fe357868863d9de3eec06b5748e464fb401fdd46 |