On Friday, January 31, 2014 7:45:07 AM UTC-7, Michael Bayer wrote:
>
>
> On Jan 30, 2014, at 9:52 PM, Lucas Taylor <ltaylo...@gmail.com<javascript:>> 
> wrote:
>
> Any notion of how one might instruct SQLAlchemy to (conditionally) create 
> tables using UNLOGGED?
>
> I'd like to be able to modify the DDL for all CREATE TABLE statements 
> under certain conditions (dialect=postgresql & testing=True)
> If not under test, then there would be no need to modify CREATE TABLE.
>
> I'm thinking that it might involve some kind of before_create event and 
> modifying the DDL, e.g. something that might start like:
>
> event.listen(
>     metadata,
>     'before_create',
>     DDL('CREATE UNLOGGED TABLE').execute_if(dialect='postgresql', 
> callable_=isUnderTest, state=TESTING)
>
> Does this seem like the right direction?
>
>
> sorta, though you might also look into using @compiles on top of 
> sqlalchemy.schema.CreateTable, see 
> http://docs.sqlalchemy.org/en/rel_0_9/core/compiler.html.
>
>
Thanks! I took a stab at it using @compiles and came up with this:

@compiles(CreateTable, 'postgresql')
def compile_unlogged(create, compiler, **kwargs):
    if unittests and 'UNLOGGED' not in create.element._prefixes:
        create.element._prefixes.append('UNLOGGED')
    return compiler.visit_create_table(create)

This subsection of the compiler docs was helpful:
http://docs.sqlalchemy.org/en/rel_0_9/core/compiler.html#changing-the-default-compilation-of-existing-constructs

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to