Skip to content

Instantly share code, notes, and snippets.

@anudeepsamaiya
Forked from r3m0t/makemigrations.py
Created January 16, 2021 09:08
Show Gist options
  • Save anudeepsamaiya/ca23c058239b371613c2c21a358e0e1f to your computer and use it in GitHub Desktop.
Save anudeepsamaiya/ca23c058239b371613c2c21a358e0e1f to your computer and use it in GitHub Desktop.
Migration conflict file
# coding: utf-8
"""Cause git to detect a merge conflict when two branches have migrations."""
# myapp/management/commands/makemigrations.py
# you'll need myapp/management/commands/__init__.py and myapp/management/__init__.py in PY2, see Django docs
from __future__ import absolute_import, unicode_literals
import io
import os
import six
from django.conf import settings
from django.db.migrations.loader import MigrationLoader
from django.core.management.commands.makemigrations import Command as MakeMigrationsCommand
# note: if you use django-migrate-sql, you will want to use:
#from migrate_sql.management.commands.makemigrations import Command as MakeMigrationsCommand
# note: for this to work, 'myapp' must be *above* 'migrate-sql' in settings.INSTALLED_APPS
class Command(MakeMigrationsCommand):
"""Cause git to detect a merge conflict when two branches have migrations."""
def handle(self, *app_labels, **options):
super(Command, self).handle(*app_labels, **options)
loader = MigrationLoader(None, ignore_no_migrations=True)
latest_migration_by_app = {}
for migration in six.itervalues(loader.disk_migrations):
name = migration.name
app_label = migration.app_label
latest_migration_by_app[app_label] = max(latest_migration_by_app.get(app_label, ''), name)
result = '# File generated by myapp.management.commands.makemigrations.Command#handle\n'
result += '\n'.join(
'{}: {}'.format(app_label, name)
for app_label, name in sorted(latest_migration_by_app.items())
)
with io.open(os.path.join(settings.BASE_DIR, 'latest_migrations.txt'), 'w') as f:
f.write(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment