Resolve migration errors
Project description
django-migration-fixer
Maintain a consistent migration history when conflicts occur as a result of changes made using different versions of the default branch.
Features
- Resolves migration conflicts on feature/PR branches
- Resolves migration conflicts on the default branch (NOT RECOMMENDED)
- Supports numbered migration modules i.e (
0001_....py
) - Supports named migration modules i.e (
custom_migration.py
) - Re-index all migrations using the last migration on the default branch i.e
main
ordevelop
Example
Branch: main
├── app
│ ├── migrations
│ ├── 0001_initial.py
│ ├── 0002_auto_20210521_2328.py
Branch: feature/test-a
├── app
│ ├── migrations
│ ├── 0001_initial.py
│ ├── 0002_auto_20210521_2328.py
│ ├── 0003_auto_20210522_1128.py
Branch:feature/test-b
├── app
│ ├── migrations
│ ├── 0001_initial.py
│ ├── 0002_auto_20210521_2328.py
│ ├── 0003_auto_20210522_1228.py
Both feature/test-a
and feature/test-b
share the last migration on main
(0002_auto_20210521_2328.py
)
Once feature/test-a
is merged into main
you run into the problem of resolving migrations on feature/test-b
which was dependent on 0002_auto_20210521_2328.py
Branch: main
├── app
│ ├── migrations
│ ├── 0001_initial.py
│ ├── 0002_auto_20210521_2328.py
│ ├── 0003_auto_20210522_1128.py
Branch: feature/test-b
├── app
│ ├── migrations
│ ├── 0001_initial.py
│ ├── 0002_auto_20210521_2328.py
│ ├── 0003_auto_20210522_1128.py \___________________ Both dependent on 0002_auto_20210521_2328.py
│ ├── 0003_auto_20210522_1228.py /
Running makemigrations
fails with the following error:
CommandError: Conflicting migrations detected; multiple leaf nodes in the migration graph: (0003_auto_20210522_1128, 0003_auto_20210522_1228 in app).
To fix them run 'python manage.py makemigrations --merge'
Using the --merge
option creates a new migration file which might not be desired.
Solution
django-migration-fixer
identifies changes between the default branch main
, and the feature branch feature/test-b
and maintains a consistent dependency history as shown below:
Branch: feature/test-b
├── app
│ ├── migrations
│ ├── 0001_initial.py
│ ├── 0002_auto_20210521_2328.py
│ ├── 0003_auto_20210522_1128.py
│ ├── 0004_auto_20210522_1228.py # Renames: '0003_auto_20210522_1228.py' → '0004_auto_20210522_1228.py'
0004_auto_20210522_1228.py
...
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('app', '0003_auto_20210522_1128'), # Replaced '0002_auto_20210521_2328' → '0003_auto_20210522_1128'
]
operations = [
...
]
NOTE: :warning:
This also works when there are conflicts detected on the default branch.
i.e You can run
python manage.py makemigrations --fix
on themain
branch which relies on primitively picking the first migration file.e.g
(0003_auto_20210522_1128, 0003_auto_20210522_1228 in app)
would result in0003_auto_20210522_1128.py
being picked as the base migration which might not be accurate in every case and is not recommended.
Assumptions
The final migration on the default branch would be used as the base for all subsequent migrations.
Installation
$ pip install django-migration-fixer
Add migration_fixer
to your INSTALLED_APPS
INSTALLED_APPS = [
...,
"migration_fixer",
...,
]
Usage
$ python manage.py makemigrations --fix
Specifying a different default branch name
Use:
$ python manage.py makemigrations -b [ master | develop ] --fix
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
Hashes for django-migration-fixer-0.0.6.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8262951ea99a06db19e88d1f18c2ea90bf46adfa30001b2a96798f4eaff943b9 |
|
MD5 | a5c6127d14de3a07fb858e8f05852827 |
|
BLAKE2b-256 | 10dad6dc18fc9cfa57e37b7c08e397e6d75ea974231ffdfd6ced12b97a511a9e |
Hashes for django_migration_fixer-0.0.6-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36790d4e37c8f0d3e729612e008fbc33aba91c573cfe447c57623360af1dd103 |
|
MD5 | 91660452270ff98182c0b5811c2bee79 |
|
BLAKE2b-256 | da4c65e576b4cd4a11bd5767d7ba1f3932ceaf7a3e486b981da7137f3bdee419 |