alembic icon indicating copy to clipboard operation
alembic copied to clipboard

CLI "Approve Migration? (y/n)"

Open wollerman opened this issue 6 years ago • 2 comments

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?

wollerman avatar Feb 26 '19 17:02 wollerman

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.

zzzeek avatar Feb 26 '19 18:02 zzzeek

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!

wollerman avatar Feb 26 '19 19:02 wollerman