Skip to content

Instantly share code, notes, and snippets.

@DoggettCK
Created February 12, 2013 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DoggettCK/4771124 to your computer and use it in GitHub Desktop.
Save DoggettCK/4771124 to your computer and use it in GitHub Desktop.
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