|
1 |
| -# django-replace-migrations |
| 1 | +# django-replace-migrations |
| 2 | + |
| 3 | +This package is an extension to djangos `makemigrations.py`. |
| 4 | +It can be used to get rid of old migrations as alternative to djangos `squashmigration` command. |
| 5 | + |
| 6 | +## Reasoning |
| 7 | + |
| 8 | +In big django projects, migration files easily pile up and get a increasing problem. |
| 9 | +Django comes with the squashmigration command - however, it is hard to handle because of multiple reasons. |
| 10 | +Especially, it can not handle circular dependencies - they must be resolved [manually and with great care](https://stackoverflow.com/questions/37711402/circular-dependency-when-squashing-django-migrations). |
| 11 | + |
| 12 | +One possible solution is to: |
| 13 | + |
| 14 | +* Delete all existing migrations |
| 15 | +* Run `./manage.py makemigrations`, so that it creates new initial migrations |
| 16 | +* Run `./manage.py migrate --fake [new migrations]` or `./manage.py migrate --fake-initail` on all servers. |
| 17 | + |
| 18 | +This workflow might work fine, if you have only few (production) servers - however, it becomes hard, when you have many environments with different versions of your application.\ |
| 19 | + |
| 20 | +With django-replace-migrations also creates new initial migrations, but also, additionally, adds the already existing migrations to the `replace` list of the new migration |
| 21 | +(That list is used by `squashmigratiions` as well). By doing that, faking migrations is not needed anymore. |
| 22 | + |
| 23 | +## Warning |
| 24 | + |
| 25 | +The new replacing migrations will not consider any `RunPython` or `RunSQL` operations. |
| 26 | +That might be acceptable depending on your use of those operations and if you need those to prepare a fresh database. |
| 27 | + |
| 28 | + |
| 29 | +## Installation |
| 30 | + |
| 31 | +Run |
| 32 | + |
| 33 | +``` |
| 34 | +pip install django-replace-migrations |
| 35 | +``` |
| 36 | + |
| 37 | +and add `django_migration_linter` to your list of installed apps. |
| 38 | + |
| 39 | + |
| 40 | +## Simple Workflow |
| 41 | + |
| 42 | +If your apps are not depending on each other, you can use django-replace-migrations like this: |
| 43 | + |
| 44 | +``` |
| 45 | +./manage.py makemigratons --replace-all --name replace [app1, app2, ...] |
| 46 | +``` |
| 47 | +Note, that you will need to list all of your apps explicitly - otherwise django will also try to replace migrations from dependencies. |
| 48 | +While `--name` could be omited, it is highly recommended to use it so that you can easlily recognize the new migrations. |
| 49 | + |
| 50 | +If for any of your apps there are not one but two or more migrations created, your apps are depending on each other (see below). |
| 51 | + |
| 52 | +You can leave your old migrations in the codebase. Old versons will continue to use the old migrations, while fresh installations will use the newly created replace migration instead. |
| 53 | + |
| 54 | +If you remove the old migrations later, you will need to update the dependencies in your other migrations and repalce there all ocurances of the old migration with the new replace migration. You can easily do that with try-and-error (`migrate` will fail and tell you which dependency is missing) |
| 55 | + |
| 56 | + |
| 57 | +## Workflow for depending apps |
| 58 | + |
| 59 | +Due to the way django resolves the `replace` list, it can not handle circular dependencies within apps. To prevent an error during migration, you must delete the old migrations that you replaced. |
| 60 | + |
| 61 | +If you have your application deployed on multiple servers, you must define down to which version, you will support upgrading and only replace those migratons. |
| 62 | + |
| 63 | +Let’s assume that our current version of the application is 3.0 and we want to get rid of all migrations prior to 2.0. |
| 64 | + |
| 65 | +The workflow for this would be: |
| 66 | + |
| 67 | +* `git checkout 2.0` |
| 68 | +* create a new branch `git checkout -b 2-0-delete-migrations` |
| 69 | +* delete all existing migrations in your apps |
| 70 | +* commit and note the commit hash |
| 71 | +* `git checkout 2.0` |
| 72 | +* create a new branch `git checkout 2-0-replace-migrations` |
| 73 | +* run `./manage.py makemigrations --replace-all --name replace_2_0 [app1, app2, ...] |
| 74 | +* commit and note the commit hash |
| 75 | +* `git checkout [your main branch]` |
| 76 | +* `git cherry-pick [commithash from 2-0-delete-migrations] |
| 77 | +* `git cherry-pick [commithash from 2-0-replace-migrations]` |
| 78 | + |
| 79 | +Now you have all migrations prior to 2.0 removed and replaced with new migrations. |
| 80 | + |
| 81 | +That means that: |
| 82 | + |
| 83 | +* Server database is prior 2.0 -> Migrations will not work |
| 84 | +* Server database is after 2.0 -> Newly created replacement migrations will not run because all migrations they replace are already applied |
| 85 | +* Server database is fresh -> Newly created replacement migrations will run. |
| 86 | + |
| 87 | +## `makemigration.py` compatebility |
| 88 | + |
| 89 | +This pagage requires deep integration into `makemigrations.py` so that I needed to copy the whole `makemigrations.py` here. Currently the version of `makemigrations.py` is copied from Django 2.1, however it is also tested with Django 3.0 and works there as well. If you encounter problems, please write what version of Django you are using. |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
0 commit comments