Created
May 28, 2020 14:53
-
-
Save h4/963e03b944e9dd72f38a0640a55329b7 to your computer and use it in GitHub Desktop.
Rename alembic revisions with new format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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