Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Creates DDL from generated Elixir Models
from inspect import getmembers, isclass
from sqlalchemy.schema import CreateTable
from elixir import metadata, setup_all, Entity, EntityMeta
from sys import modules
def generate_ddl(module, tablenames=[]):
prev_bind = metadata.bind
metadata.bind = "sqlite://"
setup_all()
classes = dict(getmembers(module, isclass))
ddls = []
for table in tablenames:
cls = classes.get(table)
if cls and cls.__module__ == module.__name__ and isinstance(cls, EntityMeta):
ddls.append(str(CreateTable(cls.table)))
metadata.bind = prev_bind
return ddls
dependencies = ['Director', 'Genre', 'Movie']
module = __import__('model')
print "\n\n".join(ddl.strip() for ddl in generate_ddl(module, dependencies))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment