Skip to content

Instantly share code, notes, and snippets.

@h4
Created May 28, 2020 14:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save h4/963e03b944e9dd72f38a0640a55329b7 to your computer and use it in GitHub Desktop.
Save h4/963e03b944e9dd72f38a0640a55329b7 to your computer and use it in GitHub Desktop.
Rename alembic revisions with new format
#!/usr/bin/env python3
import subprocess
import time
from os import walk, path, chdir
import re
regex = re.compile(r'([0-9a-f]+)_(\w+)\.py')
date_re = re.compile(r'(\d+)-(\d+)-(\d+) (\d+):(\d+)')
chdir('/alembic_/versions')
for root, dirs, files in walk('/alembic_/versions'):
for fname in files:
if fname.endswith('.py'):
if regex.match(fname):
full_path = path.join(root, fname)
with open(full_path) as fp:
for i, line in enumerate(fp):
if i == 4:
prefix = ''.join(date_re.search(line).groups())
if i > 5:
break
newname = prefix + '_' + regex.sub(r'\2_\1.py', fname)
subprocess.run(["git", "mv", fname, newname])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment