Skip to content

Instantly share code, notes, and snippets.

@cypreess
Created November 29, 2018 14:15
Show Gist options
  • Save cypreess/bc2ab180e2e97c8b898a6cc9f714daea to your computer and use it in GitHub Desktop.
Save cypreess/bc2ab180e2e97c8b898a6cc9f714daea to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Author: Kris Dorosz
# Enter the root directory of your project and type `squash`
# This should squash all not tracked by git migrations and remove the old files
import delegator
from collections import defaultdict
import sys
from subprocess import call
import os
docker_service_name = "web"
if len(sys.argv) == 2:
docker_service_name = sys.argv[1]
c = delegator.run(
r'git status -z | tr "\0" "\n" | grep "^??" | cut -b4- | grep "/migrations/"'
)
apps = defaultdict(list)
migration_files = []
for line in c.out.split("\n"):
if line:
migration_files.append(line)
app, migration = line.split("/migrations/")
app = app.split("/")[-1]
apps[app].append(migration[0:4])
for k in apps:
apps[k].sort()
cmd = "docker-compose exec %s ./manage.py squashmigrations --no-input %s %s %s" % (
docker_service_name,
k,
apps[k][0],
apps[k][-1],
)
print("$", cmd)
call(cmd.split())
cmd = "docker-compose exec %s ./manage.py migrate" % docker_service_name
print("$", cmd)
call(cmd.split())
for f in migration_files:
os.remove(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment