Ok, it was easier I thought with much thanks to the great __repr__() strings
that SQLAlchemy uses... made life so much easier

Here is the script I used, just pass the class object to the function and it
returns a string in table_name = Table(...) form.

Since this is just being used for sqlalchemy-migrate there is no need for
default or onupdate definitions, which contain function pointers in the
repr() anyways so they didn't work 100%

------------------------------------------------------------------------

import re

def convert_model_to_table(model):
    out = "%s = Table('%s', meta,\n" % (model.__tablename__,
model.__tablename__)

    columns = model._sa_class_manager.mapper.local_table.c

    for k in columns.keys():
        c = columns.get(k)

        declr = c.__repr__()

        declr = declr.replace('table=<%s>' % model.__tablename__, '')

        declr = re.sub('default=.*?\)', '', declr)
        declr = re.sub('onupdate=.*?\)', '', declr)
        declr = declr.replace(' ,', '')

        out += '    %s,\n' % declr

    out += ')'

    return out

def get_tables(models):
    print ""
    print "#", "-" * 79
    print ""
    for m in models:
        print convert_model_to_table(m)
        print ""
        print "#", "-" * 79
        print ""

--
Thadeus




On Fri, Dec 3, 2010 at 1:02 PM, Thadeus Burgess <thade...@thadeusb.com>wrote:

> I'm about to use sqlalchemy-migrate, however all of my tables are already
> in a declarative format.
>
> Is there a script out there that will convert a declarative model syntax to
> table model? I'm looking to just paste my model in declarative and it spit
> out a tablename = Table(...) format for me to paste into the migration
> versioning files.
>
> --
> Thadeus
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to