Skip to content

Instantly share code, notes, and snippets.

@r3m0t
Created April 27, 2017 20:55
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save r3m0t/f4e1388f8412404a990f2c776fb48b44 to your computer and use it in GitHub Desktop.
Save r3m0t/f4e1388f8412404a990f2c776fb48b44 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)
@ianepperson
Copy link

Very useful, but no license. Default copyright in the US means that I can't use this unless you provide some license that says otherwise. Can you? Or at least indicate that this is public domain? Here's a super simple license: https://justinpawela.github.io/default-gist-license/

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment