Skip to content

Commit 12a181c

Browse files
Accept arguments such as --directory via environment variables (Fixes #553)
1 parent 0c0e828 commit 12a181c

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ Notes:
188188

189189
- 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``.
190190
- 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``.
191-
- The default directory can also be specified as a ``directory`` argument to the ``Migrate`` constructor.
191+
- A directory can also be specified as a ``directory`` argument to the ``Migrate`` constructor, or in the ``FLASK_DB_DIRECTORY`` environment variable.
192192
- 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.
193193
- Detailed documentation on these commands can be found in the `Alembic's command reference page <http://alembic.zzzcomputing.com/en/latest/api/commands.html>`_.
194194

pyproject.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ dependencies = [
1919
"alembic >= 1.9.0",
2020
]
2121

22+
[project.optional-dependencies]
23+
dev = [
24+
"tox",
25+
"flake8",
26+
"pytest",
27+
]
28+
docs = [
29+
"sphinx",
30+
]
31+
2232
[project.license]
2333
text = "MIT"
2434

src/flask_migrate/cli.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919

2020

2121
@click.group()
22+
@click.option('-d', '--directory', default=None,
23+
help=('Migration script directory (default is "migrations")'))
2224
@click.option('-x', '--x-arg', multiple=True,
2325
help='Additional arguments consumed by custom env.py scripts')
2426
@with_appcontext
25-
def db(x_arg):
27+
def db(directory, x_arg):
2628
"""Perform database migrations."""
29+
g.directory = directory
2730
g.x_arg = x_arg # these will be picked up by Migrate.get_config()
2831

2932

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

5255

5356
@db.command()
@@ -76,8 +79,8 @@ def init(directory, multidb, template, package):
7679
def revision(directory, message, autogenerate, sql, head, splice, branch_label,
7780
version_path, rev_id):
7881
"""Create a new revision file."""
79-
_revision(directory, message, autogenerate, sql, head, splice,
80-
branch_label, version_path, rev_id)
82+
_revision(directory or g.directory, message, autogenerate, sql, head,
83+
splice, branch_label, version_path, rev_id)
8184

8285

8386
@db.command()
@@ -106,8 +109,8 @@ def migrate(directory, message, sql, head, splice, branch_label, version_path,
106109
rev_id, x_arg):
107110
"""Autogenerate a new revision file (Alias for
108111
'revision --autogenerate')"""
109-
_migrate(directory, message, sql, head, splice, branch_label, version_path,
110-
rev_id, x_arg)
112+
_migrate(directory or g.directory, message, sql, head, splice,
113+
branch_label, version_path, rev_id, x_arg or g.x_arg)
111114

112115

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

122125

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

138141

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

156159

157160
@db.command()
@@ -169,7 +172,7 @@ def upgrade(directory, sql, tag, x_arg, revision):
169172
@with_appcontext
170173
def downgrade(directory, sql, tag, x_arg, revision):
171174
"""Revert to a previous version"""
172-
_downgrade(directory, revision, sql, tag, x_arg)
175+
_downgrade(directory or g.directory, revision, sql, tag, x_arg or g.x_arg)
173176

174177

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

184187

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

199202

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

211214

212215
@db.command()
@@ -216,7 +219,7 @@ def heads(directory, verbose, resolve_dependencies):
216219
@with_appcontext
217220
def branches(directory, verbose):
218221
"""Show current branch points"""
219-
_branches(directory, verbose)
222+
_branches(directory or g.directory, verbose)
220223

221224

222225
@db.command()
@@ -226,7 +229,7 @@ def branches(directory, verbose):
226229
@with_appcontext
227230
def current(directory, verbose):
228231
"""Display the current revision for each database."""
229-
_current(directory, verbose)
232+
_current(directory or g.directory, verbose)
230233

231234

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

251254

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

0 commit comments

Comments
 (0)