Skip to content

Instantly share code, notes, and snippets.

@ronakmutha
Created November 30, 2020 06:42
Show Gist options
  • Save ronakmutha/f3d8b270e60ecb1f9e47d75870490506 to your computer and use it in GitHub Desktop.
Save ronakmutha/f3d8b270e60ecb1f9e47d75870490506 to your computer and use it in GitHub Desktop.
Fix Django migration conflicts by Migration Manifest
from django.core.management.commands.makemigrations import Command as BaseCommand
from django.db.migrations.loader import MigrationLoader
class Command(BaseCommand):
def handle(self, *app_labels, **options):
# Creates a manifest file with stores the last migrations
# for each of the Django app.
super(Command, self).handle(*app_labels, **options)
loader = MigrationLoader(None, ignore_no_migrations=True)
apps = sorted(loader.migrated_apps)
graph = loader.graph
with open("latest_migrations.manifest", "w") as file:
for app_name in apps:
leaf_nodes = graph.leaf_nodes(app_name)
if len(leaf_nodes) != 1:
raise Exception(
"App {} has multiple leaf migrations!".format(app_name)
)
file.write("{}: {}\n".format(app_name, leaf_nodes[0][1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment