Skip to main content

Smart reversion of Django migrations based on Git diff

Project description

Packaging: poetry Code style: black Join the chat at https://gitter.im/django-unmigrate Build status Code coverage PyPi version Downloads

If you are in a complex Django project, sometimes you will find yourself switching between multiple branches, some of which can add a number of database migrations. Before switching back to master you will have to unapply all migrations that are specific to the current branch. In order to unapply these, you will have to enter the migration that comes right before the first migration of the current branch. If two or more apps are involved, you will have to do that for each one of them.

If you leave your migration names unchanged, inferring the name of the right migration to target is not too difficult, because they are prefixed by default with a sequential number. Django also helps, being smart enough to let you use an unambiguous prefix of any migration name. Add a merge migration and the numbers will no longer be so obvious. Or if you have renamed your migration files as this article recommends, you will have to do the search manually.

With django-unmigrate you can speed up the process.

Usage

Add django-unmigrate to your INSTALLED_APPS. This is required to make the unmigrate management command available.

Then, while standing on any branch, you will be able to use:

python manage.py unmigrate master

Or if it’s going to be master anyways, this will suffice:

python manage.py unmigrate

And that’s it!

A little deeper

Ok, you can do more than that.

Do you need to unapply your migrations from the same branch, a few commits behind? Here’s how:

python manage.py unmigrate HEAD~12
python manage.py unmigrate b13553d
python manage.py unmigrate v1.33.7

Or if you only want to see the target migrations, do:

python manage.py unmigrate --dry-run

Finally, if you just want to play with the app with no actual modifications in the database, go ahead and unapply your migrations with fake. Just don’t forget to apply them again at the end:

python manage.py unmigrate --fake
python manage.py migrate --fake

Do you see potential?

This app started as a quick-n-dirty hack to speed up my team’s development in multiple Django projects. However, with your help, it can become more than that:

  • Do you find the migration diff detection code hackish? We agree, help us make it more robust and aligned with the Django internals.

  • Do you use mercurial instead of git? Help us with a mercurial adapter. Maybe another VCS? Please, help us as well!

  • Do you think this app can be used as a tool to generate automatic rollback scripts for automatic Django deployments? We’re thinking the same. Help us shape the logic and give us a hand with the code!

  • Is there any other direction where you see potential here? We’re open to hear your ideas.

Contributing

  • Join the discussion at https://gitter.im/django-unmigrate/community.

  • PRs are welcome! If you have questions or comments, please use the link above.

  • To run the test suite run make or make coverage. The tests for this project live inside a small django project called dunm_sandbox. Beware! This package uses Git to function, therefore, the tests expect a number of commit hashes inside this repository to be present and remain stable in order to function. See this meta file for further details.

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

django-unmigrate-0.1.4.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

django_unmigrate-0.1.4-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file django-unmigrate-0.1.4.tar.gz.

File metadata

  • Download URL: django-unmigrate-0.1.4.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for django-unmigrate-0.1.4.tar.gz
Algorithm Hash digest
SHA256 e968c0de75048a33a24db24f41b095462ba916148d1901ea6621aaade9d2a273
MD5 59eadcec4c9880febffde46dd6affc0f
BLAKE2b-256 6994c34030157a35d57c55a069796bc55e3f65471c1164db3f3f24247a9615f0

See more details on using hashes here.

File details

Details for the file django_unmigrate-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: django_unmigrate-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for django_unmigrate-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6c34295a000a31e84014914e30f7f12bd05772ffeb1d3eb6ace4587250c83ffb
MD5 c635aa57be925d20ea8a4e792c0eb4e0
BLAKE2b-256 fda2e87a4b39b8880a6c041dd14005571d18d42d9f54e925adff6eee1178bc37

See more details on using hashes here.

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