On 6/26/15 12:49 PM, Mike Bayer wrote:


On 6/26/15 12:23 PM, Jonathon Nelson wrote:
Greetings!

I am trying to use alembic to drop and re-create a PL/pgSQL function, and the function has embedded percentile (for use in RAISE but also present in comments, etc...).

When rendering the upgrade as an 'offline' sql upgrade script, the percentiles end up escaped (with percentile):

Thus:

RAISE 'Param foo (%) has some badness to it.', foo;

turns into:

RAISE 'Param foo (%%) has some badness to it.', foo;

I'm creating the function like this:

op.execute( sa.text( sql_function_body ) )

What am I doing wrong?

the compiler for the Psycopg2 backend doubles up percent signs because they aren't otherwise accepted by the DBAPI.

you can have your offline SQL use the generic Postgresql backend instead which won't do this. But it requires a monkeypatch:

Where your env.py says something like:

    context.configure(
        url=url, target_metadata=target_metadata, literal_binds=True)


for the url, do this:

    from sqlalchemy.engine import url
    from sqlalchemy.dialects.postgresql import PGDialect
    u = url.make_url("postgresql://")
    u.get_dialect = PGDialect

    context.configure(
        url=u, target_metadata=target_metadata, literal_binds=True)


Alternatively you can make a compile rule on text() that reverses the escaping:

from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql.expression import TextClause

@compiles(TextClause, "postgresql")
def _reverse_escaping(element, compiler, **kw):
    text = compiler.process_text(element, **kw)
    text = text.replace("%%", "%")
    return text
I have some thoughts on working out this case eventually at https://bitbucket.org/zzzeek/alembic/issue/304/dbapi-agnostic-dialect-use-for-offline













--
You received this message because you are subscribed to the Google Groups "sqlalchemy-alembic" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy-alembic+unsubscr...@googlegroups.com <mailto:sqlalchemy-alembic+unsubscr...@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "sqlalchemy-alembic" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy-alembic+unsubscr...@googlegroups.com <mailto:sqlalchemy-alembic+unsubscr...@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.

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

Reply via email to