How to use imports
in script.py.mako
#1162
Replies: 2 comments 4 replies
-
well the documented hook for this is in the context of using custom types, where the render_item hook grants access to the context that provides this list of imports. there's a another way to affect imports directly by using a rewriter around from alembic.autogenerate import rewriter
from alembic.operations import ops
writer = rewriter.Rewriter()
@writer.rewrites(ops.MigrationScript)
def add_imports(context, revision, op):
op.imports.add("from myprogram import something_important")
return [op] you add this to the run in env.py like this: def run_migrations_online() -> None:
connectable = ...
with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata,
process_revision_directives=writer,
)
with context.begin_transaction():
context.run_migrations() i just tested to confirm this is working. |
Beta Was this translation helpful? Give feedback.
-
@zzzeek Thank you for your answer! I'm looking for a simpler way, I found that just add However, this requires upgrading the source code. I don't know if it will have other bad effects. It will be easier to use if it works. https://github.com/sqlalchemy/alembic/blob/rel_1_9_2/alembic/command.py#L116 |
Beta Was this translation helpful? Give feedback.
-
I checked the document and issue and found two useful messages:
How to use the
imports
inscript.py.mako
to dynamically add custom import instead of manually editingscript.py.mako
.This is just a proposal that will help automate the migration.
Beta Was this translation helpful? Give feedback.
All reactions