Created
February 12, 2013 16:30
-
-
Save DoggettCK/4771124 to your computer and use it in GitHub Desktop.
Creates DDL from generated Elixir Models
This file contains 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 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