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 migrations, you have to enter the migration that comes right before the first migration of the current branch. If more than one of your 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 - Django is smart enough to let you use an unambiguous prefix of any migration name. But if you have renamed your migrations as this article recommends, then you need to do the searching manually.

With django-unmigrate you can speed up the process. While standing on any branch, you can 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

If you only want to see the target migrations:

python manage.py unmigrate --dry-run

And if you just want to play with this, go ahead and unapply your migrations with fake. Just don’t forget to apply them again:

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

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.3.tar.gz (5.9 kB view hashes)

Uploaded Source

Built Distribution

django_unmigrate-0.1.3-py3-none-any.whl (6.4 kB view hashes)

Uploaded Python 3

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