When using alembic revision --autogenerate is it possible to preview the automatically generated file without saving it?
#1286
-
|
Hello, I came across #936 but I believe my use-case to be different and is similar to how Django does it. I'd like to know if it's possible to run Here are the reasons for my request:
While the empty migration issue can be prevented and one can argue that the unnecessary files can be deleted, I believe this feature would greatly improve the quality of life for users. 🙂 I am unsure if this feature exists, which is why I'm posing a question. If it doesn't, I'd like for this to be a considered a feature request. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 9 replies
-
|
@zzzeek @CaselIT apologies for the ping, just wanted to know if you have given this a look To clarify, this comment was not meant to ask for any sort of commitment / deadline in case this turns out to be a feature request. As long as this issue is under the radar of the maintainers, I am fine with that. |
Beta Was this translation helpful? Give feedback.
-
|
this is not built in and would not be that simple because we have things like post write hooks that look for a file. so the best we could do here would be a recipe that extends on the existing recipe for dont generate empty migrations which adds a render step inside of the custom hook. we have a utility function for this which is render_python_code this is not tested but it makes use of the x argument to add from alembic.autogenerate import render_python_code
from mako.template import Template
from alembic import util
from pathlib import Path
def process_revision_directives(context, revision, directives):
if config.cmd_opts.autogenerate and config.cmd_opts.x == ["print"]:
script = directives[0]
template_vars = {
"upgrades": render_python_code(script.upgrade_ops),
"downgrades": render_python_code(script.downgrade_ops),
"imports": "\n".join(sorted(script.imports)),
"message": "none",
"up_revision": revision,
"down_revision": None,
"create_date": None,
"branch_labels": None,
"depends_on": None,
"comma":util.format_as_comma
}
template = Template(
filename=(Path(context.script.dir) / "script.py.mako").as_posix(),
)
print(template.render(**template_vars))
# erase directives which prevents file from being written
directives[:] = []
def run_migrations_online():
# ...
# connectable = ...
with connectable.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata,
process_revision_directives=process_revision_directives
)
with context.begin_transaction():
context.run_migrations() |
Beta Was this translation helpful? Give feedback.
-
|
The proposed answer doesn't work for me, or maybe it doesn't work with the new version of alembic: If I move |
Beta Was this translation helpful? Give feedback.
this is not built in and would not be that simple because we have things like post write hooks that look for a file.
so the best we could do here would be a recipe that extends on the existing recipe for dont generate empty migrations which adds a render step inside of the custom hook. we have a utility function for this which is render_python_code
this is not tested but it makes use of the x argument to add
alembic revision --autogenerate -x print: