Skip to content

Commit 1a893b4

Browse files
masamitsu-murasemiguelgrinberg
authored andcommitted
Support "check" command (Fixes #502)
1 parent a771453 commit 1a893b4

File tree

5 files changed

+27
-1
lines changed

5 files changed

+27
-1
lines changed

docs/index.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ After the extension is initialized, a ``db`` group will be added to the command-
151151
- ``flask db migrate [--message MESSAGE] [--sql] [--head HEAD] [--splice] [--branch-label BRANCH_LABEL] [--version-path VERSION_PATH] [--rev-id REV_ID]``
152152
Equivalent to ``revision --autogenerate``. The migration script is populated with changes detected automatically. The generated script should to be reviewed and edited as not all types of changes can be detected automatically. This command does not make any changes to the database, just creates the revision script.
153153

154+
- ``flask db check``
155+
Checks that a ``migrate`` command would not generate any changes. If pending changes are detected, the command exits with a non-zero status code.
156+
154157
- ``flask db edit <revision>``
155158
Edit a revision script using $EDITOR.
156159

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ python_requires = >=3.6
2727
install_requires =
2828
Flask >= 0.9
2929
Flask-SQLAlchemy >= 1.0
30-
alembic >= 0.7
30+
alembic >= 1.9.0
3131

3232
[options.packages.find]
3333
where = src

src/flask_migrate/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,10 @@ def stamp(directory=None, revision='head', sql=False, tag=None):
257257
migrations"""
258258
config = current_app.extensions['migrate'].migrate.get_config(directory)
259259
command.stamp(config, revision, sql=sql, tag=tag)
260+
261+
262+
@catch_errors
263+
def check(directory=None):
264+
"""Check if there are any new operations to migrate"""
265+
config = current_app.extensions['migrate'].migrate.get_config(directory)
266+
command.check(config)

src/flask_migrate/cli.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from flask_migrate import branches as _branches
1515
from flask_migrate import current as _current
1616
from flask_migrate import stamp as _stamp
17+
from flask_migrate import check as _check
1718

1819

1920
@click.group()
@@ -239,3 +240,12 @@ def stamp(directory, sql, tag, revision):
239240
"""'stamp' the revision table with the given revision; don't run any
240241
migrations"""
241242
_stamp(directory, revision, sql, tag)
243+
244+
245+
@db.command()
246+
@click.option('-d', '--directory', default=None,
247+
help=('Migration script directory (default is "migrations")'))
248+
@with_appcontext
249+
def check(directory):
250+
"""Check if there are any new operations to migrate"""
251+
_check(directory)

tests/test_migrate.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,16 @@ def test_alembic_version(self):
5656
def test_migrate_upgrade(self):
5757
(o, e, s) = run_cmd('app.py', 'flask db init')
5858
self.assertTrue(s == 0)
59+
(o, e, s) = run_cmd('app.py', 'flask db check')
60+
self.assertTrue(s != 0)
5961
(o, e, s) = run_cmd('app.py', 'flask db migrate')
6062
self.assertTrue(s == 0)
63+
(o, e, s) = run_cmd('app.py', 'flask db check')
64+
self.assertTrue(s != 0)
6165
(o, e, s) = run_cmd('app.py', 'flask db upgrade')
6266
self.assertTrue(s == 0)
67+
(o, e, s) = run_cmd('app.py', 'flask db check')
68+
self.assertTrue(s == 0)
6369

6470
from .app import app, db, User
6571
with app.app_context():

0 commit comments

Comments
 (0)