I want to migrate from one Model to another model with extra field. I tried with both iterative and free fall - but none of those work. Whenever I run beanie migrate -uri <connection-string> -db <db-name> -p <path/to/*_migrate.py> --distance 1 --forward
, it just shows:
Building migration list
The following code is similar to my actual code:
from beanie import Document, free_fall_migration
class OldModel(Document):
name: str
class NewModel(Document):
name: str
new_field: str
class Forward:
@free_fall_migration(document_models=[OldModel, NewModel])
async def add_new_field(self, session):
async for old_data in OldModel.find_all():
new_data = NewModel(
**old_data .dict(),
new_field='yay'
)
await new_data.replace(session=session)
class Backward:
@free_fall_migration(document_models=[OldModel, NewModel])
async def remove_new_field(self, session):
async for new_data in NewModel.find_all():
new_data_dict = new_data.dict()
new_data_dict.pop('new_field')
old_data = OldModel(
**new_data_dict
)
await old_data.replace(session=session)
Am I missing something?
I found the solution with the help from beanie package owner.
Apparently, we must point the folder which contains the migration script. Not the migration code itself. So, the right command is:
beanie migrate -uri <connection-string> -db <db-name> -p <path/to/migration/folder> --distance 1 --forward
Also, it seems it is best to maintain a folder structure as follows:
.
|....
|....
├── _migration
| |── _model
| |── *_001_migration.py
| |── *_002_migration.py
This allows us to keep the migration logic for each model in its own folder.
Hope this helps.