Skip to content

Instantly share code, notes, and snippets.

@lad1337
Last active July 16, 2020 07:34
Show Gist options
  • Save lad1337/be839576d44df07997d7d0804e6c5dd0 to your computer and use it in GitHub Desktop.
Save lad1337/be839576d44df07997d7d0804e6c5dd0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
from graphviz import Digraph
dot = Digraph(comment='alembic')
def split_line(line):
bases, _, name_and_target = line.partition('->')
id_and_stuff, _, desc = name_and_target.strip().partition(',')
desc = desc.strip()
id_, _, type_ = id_and_stuff.partition(' ')
id_ = id_.replace('<', '').replace('>', '')
type_ = type_[1:-1]
lowers = []
dependencies = []
for base in bases.split(' '):
base = base.strip().replace(',', '')
if not base:
continue
if base.startswith('(') and base.endswith(')'):
dependencies.append(base.replace('(', '').replace(')', ''))
else:
lowers.append(base)
return id_, lowers, dependencies, desc, type_
for line in sys.stdin.readlines():
id_, lowers, dependencies, desc, type_ = split_line(line)
if not id_:
continue
dot.node(id_, id_ + ":\n" + desc)
for lower in lowers:
dot.edge(lower, id_)
for dep in dependencies:
dot.edge(id_, dep, label='depends on', _attributes=dict(style='dotted'))
dot.render('/tmp/alembic.gv', view=True)
@lad1337
Copy link
Author

lad1337 commented Nov 7, 2016

use: alembic history | alembic-graph
needs graphviz pip module and dot executable. you get a pdf

@javaes
Copy link

javaes commented Nov 7, 2016

funny i wrote a similar script on the weekend which uses alembic walk_revisions function and a config instead of the pipe. i'll post it tomorrow.

i think depends_on parents aren't displayed correctly here (in my version too)

@lad1337
Copy link
Author

lad1337 commented Nov 8, 2016

yeah i didn't know what (<revision_id>) in the output was, but then remembered the depends on thing ... fixed it
and i used the pipe on purpose because i didnt want to fiddle around with configs or how alembic does it.

this is just a small hacky script but it worked for me ;)

@weaming
Copy link

weaming commented Jul 16, 2020

Nice!

@weaming
Copy link

weaming commented Jul 16, 2020

You should insert it after line 24 to avoid invalid dot node id <base> which comes from alembic history.

        if base.startswith('<') and base.endswith('>'):
            dependencies.append(base.replace('<', '').replace('>', ''))

FYI: https://gist.github.com/weaming/2463b0ec3f6ee79f5d38dcfb8b7a0b94

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