django-migrant
django-migrant is a tool that allows developers to automatically migrate their development database when switching from one git branch to another. A common use case is when asked to run a colleague's branch. With django-migrant you no longer need to figure out which migrations need to be rolled back in order to then apply another branch's migrations.
[!IMPORTANT] The tool relies on proper reverse migrations having been written!
Requirements
- A django project, version controlled using git, with database migrations.
How it works
django-migrant will create a post-checkout and a pre-rebase hook in a repository's "hooks" directory.
When you checkout a branch the hook will determine which django migrations need to be rolled back, go to the previous branch and roll back, then return to your target branch and migrate forwards.
The tool deliberately does not perform migration operations during a git rebase however, assuming the rebase goes well (🤞), it should just be a case of running ./manage.py migrate, right? 🙂
Example
The example repository can be found at https://github.com/powlo/django-migrant-example
Installation
-
Install the python package.
pip install django-migrant -
Add django_migrant to settings.py
#settings.py INSTALLED_APPS = [ # ... "django_migrant", # ... ] -
Install the post-checkout hook:
./manage.py migrant install <destination>Eg,
./manage.py migrant install ....will attempt to install the hook in the current directory.
-
Review and adapt the hooks if necessary.
The hooks are installed assuming that the application has been added to settings.py and that the appropriate python interpreter is available when executing the hook. This should work for most users.
However, instead of adding django_migrant to your INSTALLED_APPS, you may wish to change the post-checkout script to invoke the python module directly. For example:
#.git/hooks/post-checkout
DJANGO_SETTINGS_MODULE=myproject.settings python -m django_migrant migrate
Similarly, you may wish to specify the full path to the python interpreter:
#.git/hooks/post-checkout
/home/myname/virtualenvs/myproject/bin/python ./manage.py migrant migrate
Check the hook invocation matches the nuances of your own environment.
IMPORTANT! Change the permissions on the hooks to allow them to be invoked.
Eg,
chmod +x ./.git/hooks/post-checkout
chmod +x ./.git/hooks/pre-rebase
Release files for django-migrant 0.0.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_migrant-0.0.9.tar.gz | 21.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_migrant-0.0.9-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 40.4 kB
Release files / django_migrant-0.0.9.tar.gz
| Download URL | django_migrant-0.0.9.tar.gz |
|---|---|
| Size | 21.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
45dfdad1b692fcee0e9705352249a12627f581b337698974640347c4b4178bf6
|
|
BLAKE2b-256 checksum How to use checksums |
de0629f7bcfee9aca94a59a55dcdf73a1ee0620f5bdd16691abf703ecc7ecebe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 12, 2025.
Transparency logRelease files / django_migrant-0.0.9-py3-none-any.whl
| Download URL | django_migrant-0.0.9-py3-none-any.whl |
|---|---|
| Size | 18.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
92c23caaa296a5328b099db91fb714b640d4022ee1b42dd341dc2bb69a702d20
|
|
BLAKE2b-256 checksum How to use checksums |
6760a697ad0790fc1003b12603204d55a3c798f79070060e36a623c643d9291b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 12, 2025.
Transparency log