Skip to content

Commit

Permalink
Accept arguments such as --directory via environment variables (Fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Jan 7, 2025
1 parent 0c0e828 commit 12a181c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ Notes:

- 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.
- A directory can also be specified as a ``directory`` argument to the ``Migrate`` constructor, or in the ``FLASK_DB_DIRECTORY`` environment variable.
- 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
10 changes: 10 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ dependencies = [
"alembic >= 1.9.0",
]

[project.optional-dependencies]
dev = [
"tox",
"flake8",
"pytest",
]
docs = [
"sphinx",
]

[project.license]
text = "MIT"

Expand Down
37 changes: 20 additions & 17 deletions src/flask_migrate/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@


@click.group()
@click.option('-d', '--directory', default=None,
help=('Migration script directory (default is "migrations")'))
@click.option('-x', '--x-arg', multiple=True,
help='Additional arguments consumed by custom env.py scripts')
@with_appcontext
def db(x_arg):
def db(directory, x_arg):
"""Perform database migrations."""
g.directory = directory
g.x_arg = x_arg # these will be picked up by Migrate.get_config()


Expand All @@ -47,7 +50,7 @@ def list_templates():
@with_appcontext
def init(directory, multidb, template, package):
"""Creates a new migration repository."""
_init(directory, multidb, template, package)
_init(directory or g.directory, multidb, template, package)


@db.command()
Expand Down Expand Up @@ -76,8 +79,8 @@ def init(directory, multidb, template, package):
def revision(directory, message, autogenerate, sql, head, splice, branch_label,
version_path, rev_id):
"""Create a new revision file."""
_revision(directory, message, autogenerate, sql, head, splice,
branch_label, version_path, rev_id)
_revision(directory or g.directory, message, autogenerate, sql, head,
splice, branch_label, version_path, rev_id)


@db.command()
Expand Down Expand Up @@ -106,8 +109,8 @@ def migrate(directory, message, sql, head, splice, branch_label, version_path,
rev_id, x_arg):
"""Autogenerate a new revision file (Alias for
'revision --autogenerate')"""
_migrate(directory, message, sql, head, splice, branch_label, version_path,
rev_id, x_arg)
_migrate(directory or g.directory, message, sql, head, splice,
branch_label, version_path, rev_id, x_arg or g.x_arg)


@db.command()
Expand All @@ -117,7 +120,7 @@ def migrate(directory, message, sql, head, splice, branch_label, version_path,
@with_appcontext
def edit(directory, revision):
"""Edit a revision file"""
_edit(directory, revision)
_edit(directory or g.directory, revision)


@db.command()
Expand All @@ -133,7 +136,7 @@ def edit(directory, revision):
@with_appcontext
def merge(directory, message, branch_label, rev_id, revisions):
"""Merge two revisions together, creating a new revision file"""
_merge(directory, revisions, message, branch_label, rev_id)
_merge(directory or g.directory, revisions, message, branch_label, rev_id)


@db.command()
Expand All @@ -151,7 +154,7 @@ def merge(directory, message, branch_label, rev_id, revisions):
@with_appcontext
def upgrade(directory, sql, tag, x_arg, revision):
"""Upgrade to a later version"""
_upgrade(directory, revision, sql, tag, x_arg)
_upgrade(directory or g.directory, revision, sql, tag, x_arg or g.x_arg)


@db.command()
Expand All @@ -169,7 +172,7 @@ def upgrade(directory, sql, tag, x_arg, revision):
@with_appcontext
def downgrade(directory, sql, tag, x_arg, revision):
"""Revert to a previous version"""
_downgrade(directory, revision, sql, tag, x_arg)
_downgrade(directory or g.directory, revision, sql, tag, x_arg or g.x_arg)


@db.command()
Expand All @@ -179,7 +182,7 @@ def downgrade(directory, sql, tag, x_arg, revision):
@with_appcontext
def show(directory, revision):
"""Show the revision denoted by the given symbol."""
_show(directory, revision)
_show(directory or g.directory, revision)


@db.command()
Expand All @@ -194,7 +197,7 @@ def show(directory, revision):
@with_appcontext
def history(directory, rev_range, verbose, indicate_current):
"""List changeset scripts in chronological order."""
_history(directory, rev_range, verbose, indicate_current)
_history(directory or g.directory, rev_range, verbose, indicate_current)


@db.command()
Expand All @@ -206,7 +209,7 @@ def history(directory, rev_range, verbose, indicate_current):
@with_appcontext
def heads(directory, verbose, resolve_dependencies):
"""Show current available heads in the script directory"""
_heads(directory, verbose, resolve_dependencies)
_heads(directory or g.directory, verbose, resolve_dependencies)


@db.command()
Expand All @@ -216,7 +219,7 @@ def heads(directory, verbose, resolve_dependencies):
@with_appcontext
def branches(directory, verbose):
"""Show current branch points"""
_branches(directory, verbose)
_branches(directory or g.directory, verbose)


@db.command()
Expand All @@ -226,7 +229,7 @@ def branches(directory, verbose):
@with_appcontext
def current(directory, verbose):
"""Display the current revision for each database."""
_current(directory, verbose)
_current(directory or g.directory, verbose)


@db.command()
Expand All @@ -246,7 +249,7 @@ def current(directory, verbose):
def stamp(directory, sql, tag, revision, purge):
"""'stamp' the revision table with the given revision; don't run any
migrations"""
_stamp(directory, revision, sql, tag, purge)
_stamp(directory or g.directory, revision, sql, tag, purge)


@db.command()
Expand All @@ -255,4 +258,4 @@ def stamp(directory, sql, tag, revision, purge):
@with_appcontext
def check(directory):
"""Check if there are any new operations to migrate"""
_check(directory)
_check(directory or g.directory)

0 comments on commit 12a181c

Please sign in to comment.