This package is an extension to djangos makemigrations.py. It can be used to get rid of old migrations as an alternative to djangos squashmigration command.
Project description
# django-replace-migrations
This package is an extension to djangos makemigrations.py. It can be used to get rid of old migrations as an alternative to djangos squashmigration command.
## Reasoning
In big django projects, migration files easily pile up and get an increasing problem. Django comes with the squashmigration command - however, it is hard to handle because of multiple reasons. 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).
One possible solution is to:
Delete all existing migrations
Run ./manage.py makemigrations, so that it creates new initial migrations
Run ./manage.py migrate –fake [new migrations] or ./manage.py migrate –fake-initail on all servers.
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.
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 (That list is used by squashmigrations as well). By doing that, faking migrations is not needed anymore.
## Warning
The new replacing migrations will not consider any RunPython or RunSQL operations. That might be acceptable depending on your use of those operations and if you need those to prepare a fresh database.
## Installation
Run
` pip install django-replace-migrations `
and add django_migration_linter to your list of installed apps.
## Simple Workflow
If your apps are not depending on each other, you can use django-replace-migrations like this:
` ./manage.py makemigratons --replace-all --name replace [app1, app2, ...] ` Note, that you will need to list all of your apps explicitly - otherwise django will also try to replace migrations from dependencies. While –name could be omitted, it is highly recommended to use it so that you can easily recognize the new migrations.
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).
You can leave your old migrations in the codebase. Old versions will continue to use the old migrations, while fresh installations will use the newly created replace migration instead.
If you remove the old migrations later, you will need to update the dependencies in your other migrations and replace all occurrences 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)
## Workflow for depending apps
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.
If you have your application deployed on multiple servers, you must define down to which version, you will support upgrading and only replace those migrations.
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.
The workflow for this would be:
git checkout 2.0
create a new branch git checkout -b 2-0-delete-migrations
delete all existing migrations in your apps
commit and note the commit hash
git checkout 2.0
create a new branch git checkout 2-0-replace-migrations
run ./manage.py makemigrations –replace-all –name replace_2_0 [app1, app2, …]
commit and note the commit hash
git checkout [your main branch]
git cherry-pick [commit-hash from 2-0-delete-migrations]
git cherry-pick [commit-hash from 2-0-replace-migrations]
Now you have all migrations prior to 2.0 removed and replaced with new migrations.
That means that:
Server database is prior 2.0 -> Migrations will not work
Server database is after 2.0 -> Newly created replacement migrations will not run because all migrations they replace are already applied
Server database is fresh -> Newly created replacement migrations will run.
## makemigration.py compatibility
This package 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-replace-migrations-0.0.1.tar.gz
.
File metadata
- Download URL: django-replace-migrations-0.0.1.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57a667ec048f303c3432e974370d3fd769e0bf11156f06db1f5afef5e9c0fc98 |
|
MD5 | 4116b9a95e9e6412d2088e8fca355a48 |
|
BLAKE2b-256 | 3bc624003271489a95ca47141085296c77eaeb379683f62e2020a238151ff0f1 |
File details
Details for the file django_replace_migrations-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: django_replace_migrations-0.0.1-py3-none-any.whl
- Upload date:
- Size: 7.3 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/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5f317aca6a27b47127a520c07529a39eab649389a977269e2a1aac5e710f816 |
|
MD5 | e6d6a42c973d2601c60ca097c91cd2b2 |
|
BLAKE2b-256 | 6028a8d168b54619d161708a73cafcb59fb32914a262fdd735a804e1f53395e1 |