generate top-level python code for an alembic revision from render_item

16 views
Skip to first unread message

Adrian

unread,
May 5, 2020, 5:24:48 PM5/5/20
to sqlalchemy
I have a custom type implementing enums (no idea if there's something better now, but it's used in many places so
replacing it is not an option atm). Currently I'm using render_item to simply import the type and the enum and pass the
enum to the type and it works fine.

However, in the alembic revision I'd prefer to define a copy of the enum how it looked like at generation time since it may
change and in that case running an older revision's upgrade would be broken as it would generate the column based on
a different enum.

In any case, I would like to have the enum definition inside my revision file, either top-level or inside `upgrade`. But for neither
of these options I found any way to do it properly as there doesn't seem to be any way to provide code to be included in
`upgrade` or variables for the mako template context used to render the revision file.

I managed to do it using this **incredibly ugly** workaround, but I really hope there's some better way... so if there is one, please
let me know. Because not adding this abomination in my codebase would be great:

Thanks for any help/suggestions!

Mike Bayer

unread,
May 5, 2020, 6:20:46 PM5/5/20
to noreply-spamdigest via sqlalchemy
it's pretty doable if you add a new ${thing} to your mako template.  getting a value in there requires a trick, there's an example of it here:    https://github.com/sqlalchemy/alembic/issues/686#issuecomment-616828129

which is to put a dictionary inside of another dictionary and pass that as "template_args" when you context.configure(); suppose you send it as template_args={"my_dictionary": {}}.  then anywhere you are, in a render hook, in env.py, whatever, put whatever you want in that dictionary.   then reference in your template as ${my_dictionary['some_value']} .
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
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+...@googlegroups.com.

Adrian

unread,
May 6, 2020, 4:04:33 AM5/6/20
to sqlalchemy
 Works great, thanks!
Reply all
Reply to author
Forward
0 new messages