From b9c9d35744a08f4f62084ce6e3ddf30d21431dc7 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Sun, 13 Nov 2022 19:55:37 +0000 Subject: [PATCH] Option to rename "db" command group --- docs/index.rst | 4 ++++ setup.cfg | 4 ---- src/flask_migrate/__init__.py | 11 ++++++++--- tests/app_compare_type1.py | 2 +- tests/app_compare_type2.py | 2 +- tests/test_migrate.py | 8 ++++---- 6 files changed, 18 insertions(+), 13 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 762bd46..75ca5da 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -65,6 +65,10 @@ To see all the commands that are available run this command:: Note that the application script must be set in the ``FLASK_APP`` environment variable for all the above commands to work, as required by the ``flask`` command. +If the ``db`` command group name is inconvenient, it can be changed to a different with the ``command`` argument passed to the ``Migrate`` class:: + + migrate = Migrate(app, db, command='migrate') + Alembic Configuration Options ----------------------------- diff --git a/setup.cfg b/setup.cfg index 2053048..98560cb 100644 --- a/setup.cfg +++ b/setup.cfg @@ -31,7 +31,3 @@ install_requires = [options.packages.find] where = src - -[options.entry_points] -flask.commands = - db = flask_migrate.cli:db diff --git a/src/flask_migrate/__init__.py b/src/flask_migrate/__init__.py index b96149d..aeefa0b 100644 --- a/src/flask_migrate/__init__.py +++ b/src/flask_migrate/__init__.py @@ -42,10 +42,11 @@ def get_template_directory(self): class Migrate(object): - def __init__(self, app=None, db=None, directory='migrations', + def __init__(self, app=None, db=None, directory='migrations', command='db', compare_type=True, render_as_batch=True, **kwargs): self.configure_callbacks = [] self.db = db + self.command = command self.directory = str(directory) self.alembic_ctx_kwargs = kwargs self.alembic_ctx_kwargs['compare_type'] = compare_type @@ -53,9 +54,10 @@ def __init__(self, app=None, db=None, directory='migrations', if app is not None and db is not None: self.init_app(app, db, directory) - def init_app(self, app, db=None, directory=None, compare_type=None, - render_as_batch=None, **kwargs): + def init_app(self, app, db=None, directory=None, command=None, + compare_type=None, render_as_batch=None, **kwargs): self.db = db or self.db + self.command = command or self.command self.directory = str(directory or self.directory) self.alembic_ctx_kwargs.update(kwargs) if compare_type is not None: @@ -67,6 +69,9 @@ def init_app(self, app, db=None, directory=None, compare_type=None, app.extensions['migrate'] = _MigrateConfig( self, self.db, **self.alembic_ctx_kwargs) + from flask_migrate.cli import db as db_cli_group + app.cli.add_command(db_cli_group, name=self.command) + def configure(self, f): self.configure_callbacks.append(f) return f diff --git a/tests/app_compare_type1.py b/tests/app_compare_type1.py index e8a96b9..06933c1 100755 --- a/tests/app_compare_type1.py +++ b/tests/app_compare_type1.py @@ -11,7 +11,7 @@ app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) -migrate = Migrate(app, db) +migrate = Migrate(app, db, command='database') class User(db.Model): diff --git a/tests/app_compare_type2.py b/tests/app_compare_type2.py index 7de66fc..5893974 100755 --- a/tests/app_compare_type2.py +++ b/tests/app_compare_type2.py @@ -11,7 +11,7 @@ app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) -migrate = Migrate(app, db) +migrate = Migrate(app, db, command='database') class User(db.Model): diff --git a/tests/test_migrate.py b/tests/test_migrate.py index 68109e6..08e60c7 100644 --- a/tests/test_migrate.py +++ b/tests/test_migrate.py @@ -93,13 +93,13 @@ def test_custom_directory_path(self): db.session.commit() def test_compare_type(self): - (o, e, s) = run_cmd('app_compare_type1.py', 'flask db init') + (o, e, s) = run_cmd('app_compare_type1.py', 'flask database init') self.assertTrue(s == 0) - (o, e, s) = run_cmd('app_compare_type1.py', 'flask db migrate') + (o, e, s) = run_cmd('app_compare_type1.py', 'flask database migrate') self.assertTrue(s == 0) - (o, e, s) = run_cmd('app_compare_type1.py', 'flask db upgrade') + (o, e, s) = run_cmd('app_compare_type1.py', 'flask database upgrade') self.assertTrue(s == 0) - (o, e, s) = run_cmd('app_compare_type2.py', 'flask db migrate') + (o, e, s) = run_cmd('app_compare_type2.py', 'flask database migrate') self.assertTrue(s == 0) self.assertTrue(b'Detected type change from VARCHAR(length=128) ' b'to String(length=10)' in e)