A Django management commands to help on migrations deployment rollbacks
This is a fork of Jc2k's work, where I've made small fixes, build and distribuite as a pypi package
The idea here is that if you know which migrations were in version 2.0.3 of your project and which were in version 2.0.4 then setA - setB gives you the list of migrations you need to undo. See Design Implementation document.
- Python >= 3.7
- Django >= 2.2
pip install django-migrations-mgmt
- Add to django
settings.py
INSTALLED_APPS = [
...
'migrations_mgmt_cmds',
...
]
...
# Inform the class' path that handle storage's backend
MIGRATIONS_RELEASES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
# Inform the function's path that handle storage's backend sorting method.
# This is optional and should be a called for parameter `key` of `sorted` function.
MIGRATIONS_RELEASES_SORTING = 'migrations_mgmt_cmds.management.commands.release_management.default_sort_date'
...
- Create a migration release
python manage.py migrations_release --release v0.1
- Check the release JSON file in your storage backend
ls
v0.1.json
- Create a new migration
python manage.py makemigrations
Migrations for 'sample_app':
sample_app/migrations/0002_choice.py
- Create model Choice
- Execute pending migrations
python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sample_app, sessions
Running migrations:
Applying sample_app.0002_choice... OK
- Rollback the last applied migration
python manage.py migrations_rollback --release v0.1
Operations to perform:
Revert sample_app 0002_choice
Running migrations:
Rendering model states... DONE
Unapplying sample_app.0002_choice... OK
- Check applied migrations
python manage.py showmigrations
The storage's backend to be used can be informed passing a class' path to the setting MIGRATIONS_RELEASES_STORAGE
, in case it isn't informed, the value set-up at Django's setting DEFAULT_FILE_STORAGE
will be used instead.
You should inform the path as python package, e.g. 'path.Classname'
This extension introduce a command to allow for displaying, disabling or deleting the release files available in the storage's backend:
# Display list of ordered release files with extension .json
python manage.py release_management --display-list
# Display last created file with extension .json
python manage.py release_management --display-last
# Delete a file informed by path from storage
python manage.py release_management --delete-release=<file's path in storage>
# Rename a release file informed by path from storage, appending `.disabled` to its filename.
python manage.py release_management --disable-release=<release file's path in storage>
It's quite common to change the database level constraints - for example to suddenly allow NULL data - which you'll almost certainly end up with data that doesn't satisfy this constraint, and consequenctly any attempt to revert will fail. Similar problems existing when changing the size of a field.
So relying on this as a strategy for backing our changes is risky! More testing should be added.
To avoid the rollback issues mentioned above please check the Django Migrations Good Practices documentation
Contributions, issues and feature requests are welcome!
- Feel free to check issues page.
Copyright © 2021 Italo Santos.
This project is GNU General Public License v2.0 licensed.
Give a ⭐️ if this project helped you!
👤 Italo Santos
- Website: http://italosantos.com.br
- Twitter: @italux
- Github: @italux
- LinkedIn: @italosantos
This README was generated with by readme-md-generator