diff --git a/docs/index.rst b/docs/index.rst index 4a38989..ded86f2 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -157,10 +157,10 @@ After the extension is initialized, a ``db`` group will be added to the command- - ``flask db edit `` Edit a revision script using $EDITOR. -- ``flask db upgrade [--sql] [--tag TAG] [--x-arg ARG] `` +- ``flask db upgrade [--sql] [--tag TAG] `` Upgrades the database. If ``revision`` isn't given then ``"head"`` is assumed. -- ``flask db downgrade [--sql] [--tag TAG] [--x-arg ARG] `` +- ``flask db downgrade [--sql] [--tag TAG] `` Downgrades the database. If ``revision`` isn't given then ``-1`` is assumed. - ``flask db stamp [--sql] [--tag TAG] `` @@ -186,7 +186,8 @@ After the extension is initialized, a ``db`` group will be added to the command- Notes: -- All commands also take a ``--directory DIRECTORY`` option that points to the directory containing the migration scripts. If this argument is omitted the directory used is ``migrations``. +- All commands take one or more ``--x-arg ARG=VALUE`` or ``-x ARG=VALUE`` options with custom arguments that can be used in ``env.py``. +- All commands take a ``--directory DIRECTORY`` option that points to the directory containing the migration scripts. If this argument is omitted the directory used is ``migrations``. - The default directory can also be specified as a ``directory`` argument to the ``Migrate`` constructor. - The ``--sql`` option present in several commands performs an 'offline' mode migration. Instead of executing the database commands the SQL statements that need to be executed are printed to the console. - Detailed documentation on these commands can be found in the `Alembic's command reference page `_. diff --git a/src/flask_migrate/__init__.py b/src/flask_migrate/__init__.py index c61b7f5..f0f9301 100644 --- a/src/flask_migrate/__init__.py +++ b/src/flask_migrate/__init__.py @@ -3,7 +3,7 @@ import logging import os import sys -from flask import current_app +from flask import current_app, g from alembic import __version__ as __alembic_version__ from alembic.config import Config as AlembicConfig from alembic import command @@ -92,15 +92,15 @@ def get_config(self, directory=None, x_arg=None, opts=None): for opt in opts or []: setattr(config.cmd_opts, opt, True) if not hasattr(config.cmd_opts, 'x'): + setattr(config.cmd_opts, 'x', []) + for x in g.x_arg: + config.cmd_opts.x.append(x) if x_arg is not None: - setattr(config.cmd_opts, 'x', []) if isinstance(x_arg, list) or isinstance(x_arg, tuple): for x in x_arg: config.cmd_opts.x.append(x) else: config.cmd_opts.x.append(x_arg) - else: - setattr(config.cmd_opts, 'x', None) return self.call_configure_callbacks(config) diff --git a/src/flask_migrate/cli.py b/src/flask_migrate/cli.py index 176672c..5bfd4b5 100644 --- a/src/flask_migrate/cli.py +++ b/src/flask_migrate/cli.py @@ -1,4 +1,5 @@ import click +from flask import g from flask.cli import with_appcontext from flask_migrate import list_templates as _list_templates from flask_migrate import init as _init @@ -18,9 +19,12 @@ @click.group() -def db(): +@click.option('-x', '--x-arg', multiple=True, + help='Additional arguments consumed by custom env.py scripts') +@with_appcontext +def db(x_arg): """Perform database migrations.""" - pass + g.x_arg = x_arg # these will be picked up by Migrate.get_config() @db.command()