alembic
alembic copied to clipboard
CLI "Approve Migration? (y/n)"
Because of the potentially destructive nature of downgrading, it may be useful for the CLI to verify the migration that's going to take place on the particular database/environment. e.g.
$ alembic downgrade -1
Planned downgrade for environment PROD: 5a79c370e145 -> 42f6b68fe138, setup table ABC
Please type 'yes' to continue with migration.
An example of this would be Terraform's apply
command that shows the planned changes and requires user confirmation with a yes
to actually perfom. To provide compatibility for those using migrations in scripts, a CLI flag for -auto-approve
could also be added.
Thoughts?
I'm -1 as a default behavior or even a built-in behavior as it badly breaks the Unix philosophy, and in fact any kind of database operation is indeed potentially harmful, if someone runs an upgrade and their application servers aren't ready for the schema change, that's just as harmful.
As always, I can support adding a recipe for this to the cookbook section in https://alembic.sqlalchemy.org/en/latest/cookbook.html where one need only add a few lines of code to their env.py:
def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
import sys
if (
context.config.cmd_opts.cmd[0].__name__ == "downgrade"
and input("enter 'Y' to continue:\n") != "Y"
):
sys.exit(0)
I will also support making it easier to get the name of the command being run.
Ah yea fair enough. I agree it makes more sense to be at the user customized cookbook level. That better follows open-closed principle. Thanks for the quick response!