Skip to content

Instantly share code, notes, and snippets.

@maquinuz
Forked from theskumar/app.py
Created November 9, 2017 01:02
Show Gist options
  • Select an option

  • Save maquinuz/96321aded415aca3055334b1c3c0344d to your computer and use it in GitHub Desktop.

Select an option

Save maquinuz/96321aded415aca3055334b1c3c0344d to your computer and use it in GitHub Desktop.
Database diagram using sqlalchemy
# -*- coding: utf-8 -*-
''' Generates database schema graph from a relational database.
Usages:
Add database configuation in this file and then
python app.py
Note: You must have your latest database schema in the database
engine you are running against.
'''
from __future__ import unicode_literals, absolute_import
from sqlalchemy import MetaData
from sqlalchemy_schemadisplay import create_schema_graph
# Change here
db_name = 'your_db_name_here'
db_type = 'postgres' # postgres, mysql
DATABASE_CONFIG = {
'db_name': db_name,
'db_type': db_type,
'db_host': '127.0.0.1',
}
database_url_template = "%(db_type)s://%(db_host)s/%(db_name)s"
# database_url = 'mysql://root:password@127.0.0.1/your_db_name'
# database_url = 'postgres://127.0.0.1/onydo'
database_url = database_url_template % DATABASE_CONFIG
graph = create_schema_graph(metadata=MetaData(database_url),
show_datatypes=False,
show_indexes=False,
rankdir='TB',
concentrate=True,)
graph.write_png('%s_dbschema.png' % db_name)
graph.write_svg('%s_dbschema.svg' % db_name)
graph.write_pdf('%s_dbschema.pdf' % db_name)
https://pypi.python.org/packages/source/p/pyparsing/pyparsing-1.5.7.tar.gz\#md5\=9be0fcdcc595199c646ab317c1d9a709
pydot
sqlalchemy
sqlalchemy_schemadisplay
graphviz
# Database drivers (uncomment whichever you need)
# ---------------------------------
# mysql-python
# psycopg2
@maquinuz
Copy link
Author

maquinuz commented Nov 9, 2017

looks very cool, will try it

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