Skip to content

Instantly share code, notes, and snippets.

@codeinthehole
Created January 8, 2014 21:22
Show Gist options
  • Select an option

  • Save codeinthehole/8324905 to your computer and use it in GitHub Desktop.

Select an option

Save codeinthehole/8324905 to your computer and use it in GitHub Desktop.
Git post-merge hook that looks for South migrations that have the same numbers. Duplicate migration numbers can be introduced when two different branches add migrations for the same app. This is easy to miss and can cause pesky out-of-order issues when the migrations are applied.
#!/usr/bin/env python
"""
Search for migrations that share the same number.
"""
import subprocess
# You will probably want to replace 'oscar' with the appropriate folder for
# your project. I tried using '.' but it finds lots of migrations in .tox which
# I don't want to consider.
find_args = ['find', 'oscar', '-name', '*.py']
output = subprocess.check_output(find_args)
data = {}
for line in output.split("\n"):
if '/migrations/' not in line:
continue
parts = line.split("/")
# The key is a tuple of app label and migration number
key = (
parts[parts.index('migrations') - 1],
parts.pop().split('_')[0])
if key in data:
print "Found clashing migrations:\n %s\n %s\n" % (
data[key], line)
else:
data[key] = line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment