Last active
September 16, 2015 16:48
-
-
Save adewes/dea76a0cc7c56c705d74 to your computer and use it in GitHub Desktop.
Finding circular dependencies in your SQLAlchemy data scheme
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
from collections import defaultdict | |
dependencies = defaultdict(set) | |
#use your metadata object here | |
for t in metadata.sorted_tables: | |
for fkey in t.foreign_key_constraints: | |
dependencies[t.name].add(fkey.referred_table.name) | |
def find_circular_dependencies(dependencies,verbose = False): | |
def walk_graph(name,path = None,tabs = 0): | |
if path is None: | |
path = [] | |
for dependency in dependencies[name]: | |
if verbose: | |
print " "*tabs,dependency | |
if dependency in path: | |
print "Circular dependency:","->".join(path+[dependency]) | |
continue | |
walk_graph(dependency,path+[dependency],tabs = tabs+1) | |
for name in dependencies.keys(): | |
walk_graph(name) | |
#will print your circular dependencies | |
find_circular_dependencies(dependencies) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment