From 1a893b4fca280f82b1aada6458b7c866c6d3c953 Mon Sep 17 00:00:00 2001 From: Masamitsu MURASE Date: Sun, 8 Jan 2023 22:14:32 +0900 Subject: [PATCH] Support "check" command (Fixes #502) --- docs/index.rst | 3 +++ setup.cfg | 2 +- src/flask_migrate/__init__.py | 7 +++++++ src/flask_migrate/cli.py | 10 ++++++++++ tests/test_migrate.py | 6 ++++++ 5 files changed, 27 insertions(+), 1 deletion(-) diff --git a/docs/index.rst b/docs/index.rst index 75ca5da..4a38989 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -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 `` Edit a revision script using $EDITOR. diff --git a/setup.cfg b/setup.cfg index 798ce54..ef0556e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 diff --git a/src/flask_migrate/__init__.py b/src/flask_migrate/__init__.py index aeefa0b..c61b7f5 100644 --- a/src/flask_migrate/__init__.py +++ b/src/flask_migrate/__init__.py @@ -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) diff --git a/src/flask_migrate/cli.py b/src/flask_migrate/cli.py index 59bacfe..176672c 100644 --- a/src/flask_migrate/cli.py +++ b/src/flask_migrate/cli.py @@ -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() @@ -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) diff --git a/tests/test_migrate.py b/tests/test_migrate.py index 08e60c7..60a464b 100644 --- a/tests/test_migrate.py +++ b/tests/test_migrate.py @@ -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():