Skip to content

Commit

Permalink
Accept -x options for all db commands (Fixes #438)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Sep 19, 2023
1 parent 0ebfd4e commit 6f3f889
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
7 changes: 4 additions & 3 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ After the extension is initialized, a ``db`` group will be added to the command-
- ``flask db edit <revision>``
Edit a revision script using $EDITOR.

- ``flask db upgrade [--sql] [--tag TAG] [--x-arg ARG] <revision>``
- ``flask db upgrade [--sql] [--tag TAG] <revision>``
Upgrades the database. If ``revision`` isn't given then ``"head"`` is assumed.

- ``flask db downgrade [--sql] [--tag TAG] [--x-arg ARG] <revision>``
- ``flask db downgrade [--sql] [--tag TAG] <revision>``
Downgrades the database. If ``revision`` isn't given then ``-1`` is assumed.

- ``flask db stamp [--sql] [--tag TAG] <revision>``
Expand All @@ -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 <http://alembic.zzzcomputing.com/en/latest/api/commands.html>`_.
Expand Down
8 changes: 4 additions & 4 deletions src/flask_migrate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)


Expand Down
8 changes: 6 additions & 2 deletions src/flask_migrate/cli.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand Down

0 comments on commit 6f3f889

Please sign in to comment.