tj-django/django-migration-fixer

Resolve multiple leaf nodes error running django migrations.

django
django-makemigrations
django-migration-fixer
django-migrations
django-models
django-project
github-actions
makemigrations
migrate
migration
migration-tool
migrations
resolves-migration-conflicts

logo

Codacy Badge codecov Codacy Badge Test Upload Python Package

PyPI PyPI - Python Version PyPI - Django Version Downloads Public workflows that use this action. Code style: black

django-migration-fixer

Resolve django makemigrations multiple leaf nodes in the migration graph by ensuring that migration files and dependencies are always ordered regardless of remote changes, without having to run python manage.py makemigrations --merge.

Table of Contents

Features

  • Resolve migration conflicts on Pull Request branches
  • Resolve migration conflicts on the default branch (NOT RECOMMENDED)

Installation

$ pip install django-migration-fixer

Add migration_fixer to your INSTALLED_APPS

INSTALLED_APPS = [
    ...,
    "migration_fixer",
    ...,
]

Usage

Merge the changes from the default branch or the target branch of the pull request.

$ git checkout main # OR: develop/another parent feature branch
$ git pull
$ git checkout feature/xxxx
$ git merge main

Fix the migration conflicts

$ python manage.py makemigrations --fix

By default this uses main as the default branch

Example

After merging the default branch

Screen Shot 2021-07-06 at 2 21 46 PM

After running django-migration-fixer

Screen Shot 2021-07-06 at 2 22 31 PM

Assumptions

The final migration on the default branch would be used as the base for all subsequent migrations.

Specifying a different default branch

Run:

$ python manage.py makemigrations -b master --fix

Setup using Github Actions

NOTE: :warning:

  • To get this action to work you'll need to install django-migration-fixer and update your INSTALLED_APPS setting.

Inputs

Input type required default description
managepy-path string true ./manage.py The location of manage.py.
default-branch string false ${{ github.base_ref }} The default branch or
target branch of a Pull request.
force-update string false Force update the target branch
locally when git fetch fails.
skip-default-branch-update string false Skip pulling the latest
changes from the default branch.
name: Fix django migrations

on:
  pull_request:
    branches:
      - main

jobs:
  fix-migrations:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: '3.6.x'

      - name: Upgrade pip
        run: |
          pip install -U pip

      - name: Install project dependencies
        run: |
          make install

      - name: Run django-migration-fixer
        uses: tj-django/[email protected]
        with:
          managepy-path: /path/to/manage.py

      - name: Verify Changed files
        uses: tj-actions/[email protected]
        id: verify-changed-files
        with:
          files: |
             /path/to/migrations

      - name: Commit migration changes
        if: steps.verify-changed-files.outputs.files_changed == 'true'
        run: |
          git config --local user.email "github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git add /path/to/migrations
          git commit -m "Updated migrations"

      - name: Push migration changes
        if: steps.verify-changed-files.outputs.files_changed == 'true'
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }}

See: here for a working example.

Test Platforms

Found a Bug?

To file a bug or submit a patch, please head over to django-migration-fixer on github.

If you feel generous and want to show some extra appreciation:

Support me with a :star:

Buy me a coffee

Stars
29
100.00% more than last month
Forks
4
Open Issues
34