Created
January 6, 2014 19:53
-
-
Save isterin/8288794 to your computer and use it in GitHub Desktop.
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
import networkx as nx | |
from sqlalchemy import create_engine, MetaData | |
import sqlalchemy | |
engine = create_engine('mysql+mysqldb://........', echo=True) | |
meta = MetaData() | |
meta.reflect(bind=engine) | |
try: | |
# Topological sort, which will yield an exception if there is a cyclical dependency | |
meta.sorted_tables | |
except sqlalchemy.exc.CircularDependencyError as e: | |
G=nx.DiGraph() | |
cycle_tables = set([t.name for t in e.cycles]) | |
for t in e.cycles: | |
for fk in t.foreign_keys: | |
table, col = fk.target_fullname.split('.') | |
if (table in cycle_tables): # Skip any foreign keys that aren't a part of the cycle tables | |
G.add_edge(t.name, table) | |
agraph = nx.to_agraph(G) | |
agraph.draw('graph.png', format='png', prog='dot') | |
else: | |
print("There are no cyclical depenencies in the database") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment