Skip to content

Commit

Permalink
Support "check" command (Fixes #502)
Browse files Browse the repository at this point in the history
  • Loading branch information
masamitsu-murase authored and miguelgrinberg committed Jan 8, 2023
1 parent a771453 commit 1a893b4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ After the extension is initialized, a ``db`` group will be added to the command-
- ``flask db migrate [--message MESSAGE] [--sql] [--head HEAD] [--splice] [--branch-label BRANCH_LABEL] [--version-path VERSION_PATH] [--rev-id REV_ID]``
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.

- ``flask db check``
Checks that a ``migrate`` command would not generate any changes. If pending changes are detected, the command exits with a non-zero status code.

- ``flask db edit <revision>``
Edit a revision script using $EDITOR.

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ python_requires = >=3.6
install_requires =
Flask >= 0.9
Flask-SQLAlchemy >= 1.0
alembic >= 0.7
alembic >= 1.9.0

[options.packages.find]
where = src
7 changes: 7 additions & 0 deletions src/flask_migrate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,10 @@ def stamp(directory=None, revision='head', sql=False, tag=None):
migrations"""
config = current_app.extensions['migrate'].migrate.get_config(directory)
command.stamp(config, revision, sql=sql, tag=tag)


@catch_errors
def check(directory=None):
"""Check if there are any new operations to migrate"""
config = current_app.extensions['migrate'].migrate.get_config(directory)
command.check(config)
10 changes: 10 additions & 0 deletions src/flask_migrate/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from flask_migrate import branches as _branches
from flask_migrate import current as _current
from flask_migrate import stamp as _stamp
from flask_migrate import check as _check


@click.group()
Expand Down Expand Up @@ -239,3 +240,12 @@ def stamp(directory, sql, tag, revision):
"""'stamp' the revision table with the given revision; don't run any
migrations"""
_stamp(directory, revision, sql, tag)


@db.command()
@click.option('-d', '--directory', default=None,
help=('Migration script directory (default is "migrations")'))
@with_appcontext
def check(directory):
"""Check if there are any new operations to migrate"""
_check(directory)
6 changes: 6 additions & 0 deletions tests/test_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,16 @@ def test_alembic_version(self):
def test_migrate_upgrade(self):
(o, e, s) = run_cmd('app.py', 'flask db init')
self.assertTrue(s == 0)
(o, e, s) = run_cmd('app.py', 'flask db check')
self.assertTrue(s != 0)
(o, e, s) = run_cmd('app.py', 'flask db migrate')
self.assertTrue(s == 0)
(o, e, s) = run_cmd('app.py', 'flask db check')
self.assertTrue(s != 0)
(o, e, s) = run_cmd('app.py', 'flask db upgrade')
self.assertTrue(s == 0)
(o, e, s) = run_cmd('app.py', 'flask db check')
self.assertTrue(s == 0)

from .app import app, db, User
with app.app_context():
Expand Down

0 comments on commit 1a893b4

Please sign in to comment.